在 CentOS 上“禁止 Apache 目录”通常指禁止目录浏览(防止用户直接看到目录列表),也可能指彻底禁止某个目录被访问。下面分别说明。
编辑配置文件:
vi /etc/httpd/conf/httpd.conf
找到:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
把 Indexes 去掉:
Options FollowSymLinks
或者显式关闭:
Options -Indexes
保存后重启 Apache:
systemctl restart httpd
<Directory "/var/www/html/uploads">
Options -Indexes
AllowOverride None
Require all granted
</Directory>
确保 Apache 允许:
AllowOverride Options
在目录中创建 .htaccess:
Options -Indexes
<Directory "/var/www/html/admin">
Require all denied
</Directory>
访问时会返回:
403 Forbidden
在目标目录中创建 .htaccess:
Require all denied
禁止访问敏感文件:
<FilesMatch "\.(env|sql|bak)$">
Require all denied
</FilesMatch>
curl -I http://localhost/uploads/
或浏览器访问目录,应看到:
403 Forbidden
而不是文件列表。
如果你说的是 SELinux 限制 / 防火墙 / 禁止 Apache 启动目录,可以告诉我具体场景,我可以给更精确的命令。