测试 CentOS 上 Apache(httpd)配置,一般分为 配置语法检查、服务状态检查、端口监听检查、本地/远程访问测试、日志检查 等几个步骤。下面按常用顺序说明。
CentOS 7/8 常用命令:
httpd -t
或(systemd 方式):
apachectl configtest
✅ 正常输出示例:
Syntax OK
❌ 如果有错误,会提示具体配置文件和行号。
systemctl status httpd
看不到完整信息可以加:
systemctl status httpd -l
✅ 正常状态应为:
active (running)
如未启动:
systemctl start httpd
systemctl enable httpd
Apache 默认监听 80(HTTP)、443(HTTPS)。
ss -lntp | grep httpd
或:
netstat -lntp | grep httpd
期望看到:
LISTEN 0 128 :::80
curl http://localhost
或指定 IP:
curl http://127.0.0.1
✅ 返回 HTML 内容说明 Apache 正常工作。
curl -H "Host: www.example.com" http://服务器IP
在浏览器中访问:
http://服务器IP
或(如果已配置域名):
http://你的域名
⚠ 如果无法访问,继续向下排查。
查看已开放端口:
firewall-cmd --list-ports
放行 HTTP/HTTPS:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
查看状态:
getenforce
如果是 Enforcing,可临时测试:
setenforce 0
如果关闭后正常,说明是 SELinux 问题。
✅ 正确做法(推荐):
setsebool -P httpd_can_network_connect 1
或允许访问目录:
semanage fcontext -a -t httpd_sys_content_t "/data/www(/.*)?"
restorecon -Rv /data/www
tail -f /var/log/httpd/error_log
tail -f /var/log/httpd/access_log
访问失败时,错误日志最重要。
curl -k https://服务器IP
或:
openssl s_client -connect 服务器IP:443
httpd -t 显示 Syntax OKsystemctl status httpd 正常如果你愿意,可以把以下信息发我,我可以帮你精准定位问题:
httpd -t 输出systemctl status httpd