下面按由浅入深的顺序,给你一套在 Ubuntu 上排查 apache2 配置的实用方法,适合大多数“网站打不开 / 403 / 404 / 500 / 配置不生效”的问题。
systemctl status apache2
常见状态:
active (running) ✅failed ❌(看日志)sudo systemctl start apache2
sudo systemctl enable apache2
sudo apache2ctl configtest
Syntax OK ✅⚠️ 改完配置一定要执行这个命令
sudo tail -n 50 /var/log/apache2/error.log
常见错误含义:
Permission denied → 文件/目录权限问题File does not exist → 路径配置错误AH00558 → 主机名未设置(一般无害)client denied by server configuration → 权限或 <Directory> 配置问题Ubuntu Apache 结构:
/etc/apache2/
├── apache2.conf
├── ports.conf
├── sites-available/
├── sites-enabled/
├── mods-available/
├── mods-enabled/
✅ 实际生效的是:
/etc/apache2/sites-enabled//etc/apache2/mods-enabled/查看启用的站点:
ls -l /etc/apache2/sites-enabled/
启用站点:
sudo a2ensite your-site.conf
禁用站点:
sudo a2dissite 000-default.conf
改完重启:
sudo systemctl restart apache2
grep -R "Listen" /etc/apache2/
常见:
Listen 80
Listen 443
检查端口占用:
sudo ss -tulnp | grep 80
示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
<Directory /var/www/example>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
检查:
apache2ctl -S
会列出:
sudo chown -R www-data:www-data /var/www/example
sudo chmod -R 755 /var/www/example
getenforce
如果是 Enforcing,需要额外处理(Ubuntu 默认用 AppArmor)
确认模块开启:
sudo a2enmod rewrite
sudo systemctl restart apache2
确认:
AllowOverride All
sudo a2enmod php8.1 # 看你的版本
sudo systemctl restart apache2
检查:
php -v
configtestsystemctl status apache2tail error.logapache2ctl -Ssites-enabled如果你愿意,可以把:
贴出来,我可以直接帮你定位问题。