在 CentOS 上调试 Apache(通常是 httpd 服务),一般从服务状态、日志、配置语法、端口、权限、SELinux、防火墙几个方面入手。下面给你一套实战调试流程,按步骤来基本能解决 90% 的问题。
rpm -qa | grep httpd
# 或
which httpd
systemctl status httpd
systemctl start httpd
systemctl enable httpd
如果启动失败,看报错:
journalctl -xe
httpd -t
# 或
apachectl configtest
✅ 输出 Syntax OK 才正常
❌ 有错误会告诉你哪一行配置文件有问题
tail -f /var/log/httpd/error_log
常见错误:
Permission denied → 文件/目录权限问题DocumentRoot must be a directory → 路径不对SELinux 相关拒绝tail -f /var/log/httpd/access_log
ss -lntp | grep httpd
# 或
netstat -lntp | grep httpd
curl http://localhost
如果 curl 能通,但浏览器不能 → 防火墙或 SELinux 问题
firewall-cmd --list-all
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
getenforce
setenforce 0
如果关了就能访问 → SELinux 问题
# 网站目录打标签
semanage fcontext -a -t httpd_sys_content_t "/var/www/html(/.*)?"
restorecon -Rv /var/www/html
查看拒绝记录:
ausearch -m avc -ts recent
Apache 默认用户:
ps aux | grep httpd
通常是 apache
确保:
chown -R apache:apache /var/www/html
chmod -R 755 /var/www/html
httpd -S
<VirtualHost *:80>
ServerName test.local
DocumentRoot /var/www/test
<Directory /var/www/test>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
httpd -X
⚠️ 前台运行,只适合调试,不支持并发
| 现象 | 原因 |
|---|---|
| 403 Forbidden | 权限 / SELinux |
| 404 | DocumentRoot 错 |
| 服务起不来 | 配置语法错 |
| 本地能访问外网不能 | 防火墙 |
| PHP 不解析 | 未装 php 模块 |
如果你愿意,可以把:
发出来,我可以直接帮你定位问题。