在 Ubuntu 上用 Apache 出现 403 Forbidden,通常是权限或配置问题。下面按最常见原因给你梳理一下,基本能覆盖 90% 的情况。
Apache 运行的用户是 www-data
ps aux | grep apache
确保:
www-data 可读示例:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
⚠️ 注意:
755644Apache 默认找:
index.html
index.php
如果目录里没有,而且没开目录浏览,就会 403
解决:
index.html
或Options +Indexes
检查你的站点配置:
sudo nano /etc/apache2/sites-enabled/000-default.conf
常见错误:
<Directory /var/www/html>
Require all denied
</Directory>
✅ 正确应该是:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
改完重启:
sudo systemctl restart apache2
如果你用了 .htaccess,里面可能有:
Deny from all
或者:
Require all denied
解决:
Ubuntu 默认用 AppArmor,一般不是主因,但:
sudo aa-status
如果 Apache 被限制,可尝试:
sudo systemctl restart apparmor
(极少需要改)
比如:
http://ip/secret/
但该目录不存在或权限不足
# 1. 看错误日志(最重要)
sudo tail -n 50 /var/log/apache2/error.log
# 2. 看权限
ls -ld /var/www/html
ls -l /var/www/html
# 3. 看配置
sudo apache2ctl configtest
❌ 权限问题:
AH00035: access to / denied (filesystem path '/var/www/html') because search permission is missing
❌ 配置拒绝:
client denied by server configuration
你可以贴出:
我可以直接告诉你哪一行错了、怎么改。