在 Ubuntu 上配置好 Apache2 后,可以从本地和服务端两个层面进行测试。下面按常见步骤给你一个完整、实用的测试清单。
sudo systemctl status apache2
✅ 正常状态应显示:
Active: active (running)
如果未启动:
sudo systemctl start apache2
设置开机自启:
sudo systemctl enable apache2
curl http://localhost
或指定 IP:
curl http://127.0.0.1
✅ 看到 HTML 内容说明 Apache 已正常响应。
在服务器本机浏览器访问:
http://localhost
或
http://服务器IP
sudo ss -tlnp | grep apache
默认应监听:
80
443
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw reload
查看状态:
sudo ufw status
sudo apache2ctl configtest
✅ 正常输出:
Syntax OK
假设你的虚拟主机配置是:
/etc/apache2/sites-available/example.conf
启用站点:
sudo a2ensite example.conf
sudo systemctl reload apache2
在浏览器访问:
http://example.com
(如本地测试,可修改 /etc/hosts)
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
浏览器访问:
http://服务器IP/info.php
✅ 看到 PHP 信息页面说明 PHP + Apache 正常。
⚠️ 测试完成后建议删除:
sudo rm /var/www/html/info.php
sudo tail -f /var/log/apache2/error.log
访问页面时观察是否有报错。
curl -k https://服务器IP
或使用:
sudo apache2ctl -S
确认 443 端口已启用。
| 检查项 | 命令 |
|---|---|
| Apache 是否运行 | systemctl status apache2 |
| 配置是否正确 | apache2ctl configtest |
| 端口是否监听 | ss -tlnp |
| 防火墙是否放行 | ufw status |
| 页面是否返回 | curl http://localhost |
| 错误日志 | tail -f /var/log/apache2/error.log |
如果你愿意,可以把你的 Apache 配置文件内容 或 报错信息贴出来,我可以帮你精确排查。