在 Ubuntu 的 Apache 中,**禁用目录浏览(目录列表)**通常是通过修改 Apache 配置来实现的。下面是几种常见、推荐的做法。
Ubuntu 中站点配置一般在:
/etc/apache2/sites-available/000-default.conf
或你自己的虚拟主机文件,例如:
/etc/apache2/sites-available/your-site.conf
<Directory> 中关闭 Indexes找到类似以下内容:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
去掉 Indexes:
<Directory /var/www/html>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
✅ -Indexes 表示禁用目录浏览
如果你的站点允许 .htaccess(通常需 AllowOverride All):
在网站根目录创建或编辑 .htaccess:
Options -Indexes
然后确保 Apache 配置中允许:
AllowOverride All
编辑 Apache 主配置:
/etc/apache2/apache2.conf
修改:
<Directory /var/www/>
Options -Indexes
AllowOverride None
Require all granted
</Directory>
修改完成后,重启或重载 Apache:
sudo systemctl reload apache2
或
sudo systemctl restart apache2
访问一个没有 index 文件的目录,例如:
http://your-server/ip/test/
如果看到:
403 Forbidden
说明目录浏览已成功禁用 ✅
Indexes:开启目录列表-Indexes:关闭目录列表test.jpg)如果你希望进一步禁止访问某些文件或隐藏敏感内容,也可以结合:
Require all denied<Files>.htpasswd)需要我帮你写一份完整的安全配置示例吗?