CentOS 上 Apache 错误排查与修复指南
一 快速定位
sudo systemctl status httpdsudo tail -f /var/log/httpd/error_logsudo apachectl configtest(返回 Syntax OK 再继续)sudo ss -tulpen | grep -E ':80|:443'sudo systemctl restart httpd。二 常见错误与对应修复
systemctl status httpd 显示端口冲突。sudo ss -tulpen | grep -E ':80|:443'sudo systemctl restart httpdapachectl configtest 报错,或 systemctl status httpd 提示配置问题。configtest 的提示定位到具体文件与行号,修正语法后再次 configtest,通过再重启。sudo chown -R apache:apache /var/www/html && sudo chmod -R 755 /var/www/html.htaccess,确认目录允许 AllowOverride(如 AllowOverride All)。sudo setenforce 0(仅用于排查,不建议长期关闭)semanage/restorecon 调整策略或布尔值,恢复为 enforcing 模式。sudo firewall-cmd --list-allsudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload三 高频场景速查表
| 症状 | 优先命令 | 处理要点 |
|---|---|---|
| 服务起不来 | systemctl status httpd、tail -f /var/log/httpd/error_log |
先看日志末几行定位错误类型 |
| 启动报端口占用 | `ss -tulpen | grep -E ':80 |
| 配置改完仍报错 | apachectl configtest |
按提示修复语法,再重启 |
| 外部访问不了 | firewall-cmd --list-all |
放行 http/https 并重载防火墙 |
| 访问被拒绝 | ls -ld /var/www/html、getenforce |
校正目录属主为 apache:apache、权限 755;SELinux 临时置 permissive 验证 |
| 页面不存在 | tail -f /var/log/httpd/error_log |
核对 DocumentRoot 与文件路径是否存在 |
四 修复后验证与建议
sudo systemctl status httpd;ss -tulpen | grep -E ':80|:443'curl -I http://服务器IP/ 或浏览器访问tail -f /var/log/httpd/error_log 一段时间,确认无新错误