在 Ubuntu 上验证 Apache2 配置是否正确的常用方法和步骤如下:
使用 Apache 自带的检查命令:
sudo apache2ctl configtest
或:
sudo apache2ctl -t
Syntax OK
AH00526: Syntax error on line 12 of /etc/apache2/sites-enabled/example.conf
这行会直接告诉你 哪一行出错、哪个文件出错,非常有用。
systemctl status apache2
期望看到:
active (running)
如果不是,可重启:
sudo systemctl restart apache2
查看已启用的虚拟主机:
ls /etc/apache2/sites-enabled/
如果没有你要的配置:
sudo a2ensite your-site.conf
sudo systemctl reload apache2
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
</VirtualHost>
curl -I http://example.com
或用本地 hosts 测试:
curl http://localhost
Apache 默认监听 80 / 443:
sudo ss -tlnp | grep apache
或:
sudo netstat -tlnp | grep apache
输出示例:
LISTEN 0 128 0.0.0.0:80
LISTEN 0 128 0.0.0.0:443
sudo tail -f /var/log/apache2/error.log
每次重启 Apache 后观察是否有报错。
| 检查项 | 命令 |
|---|---|
| 配置语法 | apache2ctl configtest |
| 服务状态 | systemctl status apache2 |
| 端口监听 | ss -tlnp |
| 启用站点 | a2ensite |
| 启用模块 | a2enmod ssl rewrite |
| 错误日志 | /var/log/apache2/error.log |
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
解决:
sudo lsof -i :80
sudo kill <pid>
sudo a2enmod rewrite
sudo systemctl restart apache2
如果你愿意,可以把 你的 Apache 配置文件内容 或 报错信息贴出来,我可以帮你逐行检查 ✅