温馨提示×

怎样排查centos apache故障

小樊
39
2026-10-23 12:34:17
栏目: 智能运维

在 CentOS 上排查 Apache(httpd)故障,通常可以按下面系统化步骤来进行,从“能不能启动”到“为什么报错”逐步定位。


一、确认 Apache 是否运行

systemctl status httpd

或(老版本):

service httpd status

常见状态:

  • active (running):服务正常
  • failed:启动失败(看下文日志)
  • inactive:未启动

尝试启动:

systemctl start httpd

若失败,直接看错误:

systemctl status httpd -l
journalctl -xe

二、查看 Apache 错误日志(最重要)

1. 错误日志位置

CentOS 默认:

/var/log/httpd/error_log

实时查看:

tail -f /var/log/httpd/error_log

2. 访问日志(看请求是否到达)

/var/log/httpd/access_log

三、检查配置文件语法

httpd -t

或:

apachectl configtest

常见错误:

  • 端口被占用
  • 配置文件语法错误
  • DocumentRoot 不存在或无权限

四、检查端口与监听

ss -lntp | grep :80
# 或
netstat -lntp | grep httpd

如果被 nginx 占用:

systemctl stop nginx

五、检查防火墙 & SELinux(CentOS 特有重点)

1. 防火墙

firewall-cmd --list-all

放行 80/443:

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

2. SELinux(非常常见)

查看是否开启:

getenforce

临时关闭测试:

setenforce 0

若关闭后正常 → SELinux 权限问题:

ls -Z /var/www/html
restorecon -Rv /var/www/html

六、检查网站目录权限

Apache 默认用户:

ps aux | grep httpd
# 通常是 apache

目录示例:

chown -R apache:apache /var/www/html
chmod -R 755 /var/www/html

七、虚拟主机问题排查

查看虚拟主机:

httpd -S

检查:

  • ServerName 是否正确
  • DocumentRoot 是否存在
  • <Directory> 是否允许访问

八、PHP / 模块相关故障

查看已加载模块:

httpd -M

PHP 不解析:

yum install php php-cli
systemctl restart httpd

九、快速排查清单(总结)

✅ 服务是否启动
✅ 错误日志内容
✅ 配置语法是否正确
✅ 端口是否被占用
✅ 防火墙 / SELinux
✅ 目录权限
✅ 虚拟主机配置


如果你愿意,可以把 具体报错信息或 error_log 内容贴出来,我可以直接帮你定位是哪一类问题。

0 踩