在 Ubuntu 上用 Apache 出现 403 Forbidden,最常见原因就那么几类,我按「从高到低概率」给你梳理,并附上排查方法。
Apache 运行用户一般是 www-data,如果网站目录它读不了,就会 403。
namei -l /var/www/html/yourdir
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
⚠️ 注意:
755644Require all grantedApache 2.4 默认是拒绝访问的。
sudo nano /etc/apache2/sites-available/000-default.conf
确保有类似:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
改完后:
sudo apache2ctl configtest
sudo systemctl restart apache2
如果目录里没有:
index.html
index.php
而配置里没有:
Options Indexes
就会 403。
index.html
或如果你启用了 .htaccess:
AllowOverride All
但 .htaccess 里有:
Deny from all
Require all denied
也会直接 403。
Ubuntu 默认用 AppArmor,一般不会拦,但如果你改了网站目录(比如 /home/xxx):
sudo aa-status
如果用 home 目录:
sudo chmod 755 /home/youruser
比如:
<Files *.md>
Require all denied
</Files>
# 1. 看错误日志
sudo tail -n 50 /var/log/apache2/error.log
# 2. 看配置语法
sudo apache2ctl -S
# 3. 看权限
ls -ld /var/www/html
你可以贴出:
/var/log/apache2/error.log 的相关报错我可以直接告诉你是哪一条导致的 403。