在 Ubuntu 上测试 Apache(通常是 apache2)配置,主要从配置语法检查、服务状态、访问测试、日志排查几个方面入手。下面按实用顺序说明。
sudo apache2ctl configtest
或
sudo apache2 -t
✅ 正常结果:
Syntax OK
❌ 如果有错误,会指出文件和行号,例如:
AH00526: Syntax error on line 12 of /etc/apache2/sites-enabled/example.conf
systemctl status apache2
或
sudo service apache2 status
启动 / 重启 / 停止:
sudo systemctl start apache2
sudo systemctl restart apache2
sudo systemctl stop apache2
curl -I http://localhost
或指定域名:
curl -I http://your-domain.com
常见返回:
200 OK:配置正常403 Forbidden:权限或 Directory 配置问题404 Not Found:DocumentRoot 或路径错误http://localhosthttp://服务器IPls /etc/apache2/sites-enabled/
curl -I --resolve yourdomain.com:80:127.0.0.1 http://yourdomain.com
sudo tail -f /var/log/apache2/error.log
sudo tail -f /var/log/apache2/access.log
例如 rewrite:
sudo a2enmod rewrite
sudo systemctl restart apache2
查看已启用模块:
apache2ctl -M
Directory 权限Options Indexessudo apache2ctl configtest
systemctl status apache2
curl -I http://localhost
sudo tail -n 50 /var/log/apache2/error.log
如果你愿意,可以把:
发出来,我可以帮你精确分析配置问题。