在 Ubuntu 上调试 Apache2 可以按下面几个层次来做,从“看日志 → 查配置 → 查端口/权限 → 查模块/环境”逐步排查。
sudo systemctl status apache2
常见状态:
active (running):服务正常inactive / failed:服务没起来重启 / 重载:
sudo systemctl restart apache2
sudo systemctl reload apache2
sudo tail -n 50 /var/log/apache2/error.log
或实时查看:
sudo tail -f /var/log/apache2/error.log
常见错误:
Permission deniedFile does not existAH00558: Could not reliably determine server's fully qualified domain nameAddress already in usesudo apache2ctl configtest
或:
sudo apache2 -t
✅ 返回 Syntax OK 表示配置没问题
❌ 有错误会直接提示哪一行有问题
ls /etc/apache2/sites-enabled/
查看某个站点:
cat /etc/apache2/sites-enabled/000-default.conf
常见错误:
DocumentRoot 路径不存在ServerName 写错Apache 默认使用 80 / 443
sudo netstat -tulnp | grep :80
sudo ss -tulnp | grep :80
如果端口被占用(如 nginx):
sudo systemctl stop nginx
ps aux | grep apache
通常是:
www-datasudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
⚠️ 不要随便 chmod 777
查看已启用模块:
apache2ctl -M
启用模块示例:
sudo a2enmod rewrite
sudo a2enmod ssl
sudo systemctl restart apache2
禁用模块:
sudo a2dismod rewrite
sudo a2ensite your-site.conf
sudo systemctl reload apache2
禁用:
sudo a2dissite your-site.conf
curl http://localhost
或:
curl -I http://localhost
| 问题 | 解决方案 |
|---|---|
| 403 Forbidden | 目录权限 / <Directory> 配置 |
| 404 Not Found | DocumentRoot 路径错误 |
| 500 Internal Error | PHP 错误 / .htaccess |
| 无法访问 | ufw 防火墙 |
| 端口冲突 | nginx / 其他程序占用 |
防火墙检查:
sudo ufw status
sudo ufw allow 80
sudo ufw allow 443
sudo tail -f /var/log/apache2/error.log
或开启 PHP 错误显示(开发环境):
ini_set('display_errors', 1);
error_reporting(E_ALL);
你可以直接把 error.log 报错内容 或 访问现象 发给我。