在 Ubuntu 上调试 Apache(通常是 Apache2)可以从服务状态、日志、配置、端口、权限、模块几个方面入手。下面是一套常用且实用的调试流程。
systemctl status apache2
或
service apache2 status
启动 / 重启 / 停止:
sudo systemctl start apache2
sudo systemctl restart apache2
sudo systemctl stop apache2
sudo tail -f /var/log/apache2/error.log
常见错误:
Permission deniedDocumentRoot does not existAH00558: Could not reliably determine server's fully qualified domain namesudo tail -f /var/log/apache2/access.log
sudo apache2ctl configtest
或
sudo apache2 -t
如果提示:
AH00558: ...
Syntax OK
说明语法没问题。
/etc/apache2/apache2.conf
/etc/apache2/sites-enabled/
/etc/apache2/mods-enabled/
查看某个虚拟主机:
sudo nano /etc/apache2/sites-enabled/000-default.conf
sudo netstat -tulnp | grep apache
或
sudo ss -tulnp | grep apache
默认是:
80
443
如果 80 被占用(如 nginx):
sudo lsof -i :80
假设:
DocumentRoot /var/www/html
确保:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
ps aux | grep apache
通常是 www-data
apache2ctl -M
sudo a2enmod rewrite
sudo systemctl restart apache2
sudo a2dismod rewrite
sudo a2ensite example.conf
sudo systemctl reload apache2
sudo a2dissite example.conf
在 /etc/hosts 添加:
127.0.0.1 example.local
sudo apt install php libapache2-mod-php
测试:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
访问:
http://localhost/info.php
| 错误 | 原因 |
|---|---|
| 403 Forbidden | 权限 / Directory 配置 |
| 404 Not Found | DocumentRoot 错 |
| 500 Internal | PHP / .htaccess 错误 |
| 服务起不来 | 端口占用 / 配置错 |
✅ 先 configtest
✅ 再看 error.log
✅ 确认端口和权限
✅ 逐条启用配置排查
如果你有具体报错信息(比如某条 error.log 内容),可以贴出来,我可以帮你精确分析。