CentOS 上 Apache 故障排查步骤
一 快速定位
sudo systemctl status httpd -l --no-pager,若未运行可 sudo systemctl start httpd;查看启动失败原因用 sudo journalctl -xeu httpd。sudo tail -f /var/log/httpd/error_log。sudo apachectl configtest(返回 Syntax OK 再继续)。sudo ss -tulpen | grep -E ':80|:443'(或 netstat -tuln | grep ':80\|:443')。sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload;如使用 iptables 可临时 systemctl stop firewalld 做排查(排障后请恢复)。sudo chown -R apache:apache /var/www/html && sudo find /var/www/html -type d -exec chmod 755 {} \; -o -type f -exec chmod 644 {} \;。二 常见症状与处理
systemctl status httpd 与 journalctl -xeu httpd,再用 apachectl configtest 定位语法;若提示端口被占用,用 ss -tulpen | grep ':80\|:443' 找到 PID 并决定停止或调整 Listen 端口;修复后 sudo systemctl restart httpd。DirectoryIndex 存在;临时测试可 sudo setenforce 0(若解决,改为正确的 SELinux 策略而非长期关闭);检查目录权限与属主;排查 .htaccess 指令合法性。DocumentRoot、Alias 或 RewriteRule。apachectl configtest 与脚本日志定位;确保脚本解释器与文件权限正确。configtest 再重启。三 日志与定位技巧
sudo tail -f -n 100 /var/log/httpd/error_log 实时查看;用 grep "AH00112\|AH01630\|client denied\|Premature end of script headers" /var/log/httpd/error_log 精准筛选常见错误。四 安全与加固建议
setenforce 0 验证,但应优先使用正确的 SELinux 布尔值或类型(如 httpd_read/write 等)修复,避免长期关闭。firewall-cmd --reload 验证。五 一键排查命令清单
sudo systemctl status httpd -l --no-pager
sudo journalctl -xeu httpd
sudo tail -f /var/log/httpd/error_log
sudo apachectl configtest
sudo ss -tulpen | grep -E ':80|:443'
sudo firewall-cmd --list-all
sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload
sudo chown -R apache:apache /var/www/html
sudo find /var/www/html -type d -exec chmod 755 {} \; -o -type f -exec chmod 644 {} \;
# 如怀疑 SELinux,仅作排查:sudo setenforce 0(排障后恢复为 enforcing)
以上步骤覆盖了 服务状态、日志、配置语法、端口占用、防火墙、权限与 SELinux 等关键点,可快速定位并修复大多数 CentOS 上的 Apache(httpd) 故障。