在 CentOS 上调试 Apache(httpd),通常从 日志、配置、进程、端口、权限 等方面入手。下面按最常见问题 → 对应调试方法给你一个实用清单。
systemctl status httpd
常见状态:
active (running) ✅failed ❌(后面看日志)tail -f /var/log/httpd/error_log
或查看历史错误:
less /var/log/httpd/error_log
常见错误示例:
Permission deniedAH00526: Syntax errorAddress already in useFile does not existtail -f /var/log/httpd/access_log
用来确认请求是否到达 Apache。
httpd -t
或:
apachectl configtest
✅ 输出示例:
Syntax OK
❌ 若报错,会提示哪一行出错,这是排查重点。
systemctl restart httpd
或只重载配置(不断连接):
systemctl reload httpd
ss -lntp | grep httpd
或:
netstat -lntp | grep httpd
正常应为:
LISTEN 0 128 :::80
LISTEN 0 128 :::443
lsof -i :80
firewall-cmd --list-all
如未放行:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
getenforce
Enforcing → 可能拦截Permissive → 只记录不拦截Disabled → 关闭tail -f /var/log/audit/audit.log | grep httpd
setenforce 0
✅ 如果关闭后 Apache 正常 → 问题在 SELinux
setsebool -P httpd_read_user_content 1
setsebool -P httpd_can_network_connect 1
或修改目录上下文:
semanage fcontext -a -t httpd_sys_content_t "/data/www(/.*)?"
restorecon -Rv /data/www
ls /etc/httpd/conf.d/
检查是否有语法错误:
httpd -t
示例虚拟主机:
<VirtualHost *:80>
ServerName test.local
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
php -v
测试 PHP 是否解析:
echo "<?php phpinfo();" > /var/www/html/info.php
浏览器访问:
http://服务器IP/info.php
编辑:
vim /etc/httpd/conf/httpd.conf
修改日志级别:
LogLevel debug
重启:
systemctl restart httpd
Apache 无法访问
↓
systemctl status httpd
↓
httpd -t
↓
tail -f error_log
↓
防火墙 / SELinux
↓
端口监听
| 错误 | 原因 |
|---|---|
| 403 Forbidden | 权限 / SELinux |
| 404 Not Found | DocumentRoot 错误 |
| Address already in use | 端口被占用 |
| AH00526 | 配置语法错误 |
| 浏览器无法访问 | 防火墙 |
如果你愿意,可以把 报错日志贴出来,我可以直接帮你精确分析。