在 Ubuntu 上定位 Apache 故障,通常遵循“看状态 → 看日志 → 看配置 → 看系统资源”的思路。下面给你一套比较系统的排查方法。
systemctl status apache2
重点看:
active (running) 还是 failedsudo systemctl start apache2
如果启动失败,继续看日志。
sudo tail -n 50 /var/log/apache2/error.log
常见错误:
sudo tail -n 50 /var/log/apache2/access.log
sudo apache2ctl configtest
常见输出:
Syntax OKAH00526: Syntax error(会指出文件和行号)ls /etc/apache2/sites-enabled/
确认你的站点配置文件存在且正确。
sudo netstat -tlnp | grep apache
# 或
sudo ss -tlnp | grep apache
正常应看到:
:80
:443
sudo lsof -i :80
sudo lsof -i :443
sudo ufw status
若开启,需允许:
sudo ufw allow 80
sudo ufw allow 443
常见目录:
/var/www/html
确保:
ls -ld /var/www/html
ps aux | grep apache
getenforce
如果是 Enforcing,可能是权限问题根源。
ls /etc/apache2/mods-enabled/
php -v
若使用 PHP:
php module 已启用index.php 在 DirectoryIndexfree -h
top
df -h
日志写满磁盘是常见故障原因。
| 现象 | 优先检查 |
|---|---|
| 无法启动 | configtest + error.log |
| 403 | 目录权限 / DocumentRoot |
| 404 | 站点配置 / Rewrite |
| 502/崩溃 | 内存 / PHP / 模块 |
| 访问慢 | 日志 / 资源 / DB |
如果你愿意,可以直接把具体的报错信息(如 systemctl status 或 error.log 内容)贴出来,我可以帮你精准定位。