在 Ubuntu 上使用 Apache2 出现 403 Forbidden 错误,通常是因为权限问题或配置问题。下面按最常见原因逐一说明和解决方式。
Apache 运行的用户通常是 www-data。
检查权限:
ls -l /var/www/html
解决:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
如果是单个文件:
chmod 644 index.html
查看站点配置:
sudo nano /etc/apache2/sites-enabled/000-default.conf
确认:
DocumentRoot /var/www/html
并且该目录真实存在。
Apache 2.4 中,没有显式允许就会 403。
示例正确配置:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
修改后重启:
sudo systemctl restart apache2
如果目录中没有:
且配置中未允许目录列表:
Options -Indexes
就会 403。
解决方式:
index.html如果使用了 .htaccess:
AllowOverride None检查:
AllowOverride All
Ubuntu 默认用 AppArmor:
sudo aa-status
如异常可临时测试:
sudo systemctl stop apparmor
配置中可能写了:
Require ip 192.168.1.100
你不在允许列表中。
查看错误日志(最重要):
sudo tail -n 50 /var/log/apache2/error.log
通常能直接看到原因,例如:
client denied by server configurationPermission denied✅ 目录权限不是 755
✅ 文件权限不是 644
✅ Directory 没写 Require all granted
✅ 没有 index 文件
如果你愿意,可以把 error.log 报错内容 或 站点配置文件 发出来,我可以直接帮你定位是哪一条导致的 403。