在 CentOS + Apache 环境下出现 403 Forbidden,通常是权限或配置问题。下面按最常见原因 → 排查顺序来说明。
Apache 运行的用户(通常是 apache 或 www-data)无法读取文件。
✅ 正确示例:
chown -R apache:apache /var/www/html
chmod -R 755 /var/www/html
检查:
ps aux | grep httpd
看 User 是谁(CentOS 一般是 apache)
即使权限是对的,SELinux 也会直接 403
查看是否开启:
getenforce
如果是 Enforcing:
✅ 修复:
chcon -R -t httpd_sys_content_t /var/www/html
或临时关闭测试:
setenforce 0
Apache 找不到或无权访问配置中的目录。
检查:
grep -R DocumentRoot /etc/httpd/
示例:
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
Require all granted
</Directory>
⚠️ 如果 <Directory> 没加 Require all granted,直接 403
目录存在,但里面没有:
解决:
echo "ok" > /var/www/html/index.html
或允许目录浏览(不推荐):
Options +Indexes
如果启用了:
AllowOverride None
但目录中有 .htaccess,可能 403。
解决:
AllowOverride All
配置中可能写了:
Require ip 192.168.1.100
你不在白名单 → 403
# 1. 看错误日志
tail -f /var/log/httpd/error_log
# 2. 看权限
ls -l /var/www/html
# 3. 看 SELinux
getenforce
# 4. 看 Apache 配置
httpd -S
| 日志内容 | 原因 |
|---|---|
Permission denied |
文件权限 |
SELinux policy |
SELinux |
client denied by server configuration |
Apache 配置 |
如果你愿意,可以贴出:
error_log 报错我可以直接帮你精准定位是哪一条导致的 403。