温馨提示×

ubuntu apache2怎么禁止目录浏览

小樊
43
2026-09-26 09:16:18
栏目: 智能运维

在 Ubuntu 的 Apache2 中,**禁止目录浏览(目录列表)**通常通过修改 Apache 配置或 .htaccess 来实现。


方法一:修改 Apache 虚拟主机配置(推荐)

  1. 打开站点配置文件
sudo nano /etc/apache2/sites-available/000-default.conf

(或你实际的虚拟主机配置文件)

  1. 在 <Directory> 块中添加或修改:
<Directory /var/www/html>
    Options -Indexes
</Directory>
  1. 保存后重启 Apache:
sudo systemctl restart apache2

方法二:使用 .htaccess 文件

如果你的网站目录允许使用 .htaccess:

  1. 在网站根目录创建或编辑 .htaccess:
nano /var/www/html/.htaccess
  1. 添加以下内容:
Options -Indexes
  1. 确保 Apache 允许 .htaccess 生效
    在虚拟主机配置中需要有:
AllowOverride All
  1. 重启 Apache:
sudo systemctl restart apache2

方法三:全局关闭目录浏览(不推荐,影响所有站点)

sudo nano /etc/apache2/apache2.conf

找到类似:

<Directory /var/www/>
    Options Indexes FollowSymLinks

改为:

Options -Indexes FollowSymLinks

然后重启 Apache。


验证是否成功

访问一个没有 index.html / index.php 的目录,如果看到:

Forbidden
You don't have permission to access this resource.

说明目录浏览已成功禁止 ✅


如果你愿意,我可以帮你检查具体的 Apache 配置文件内容。

0 踩