CentOS Apache故障排查
导读:CentOS 上 Apache 故障排查清单 一 快速定位流程 确认服务状态与监听端口 查看服务:systemctl status httpd(应为 active (running ) 查看端口:ss -tulnp | grep -E...
CentOS 上 Apache 故障排查清单
一 快速定位流程
- 确认服务状态与监听端口
- 查看服务:systemctl status httpd(应为 active (running))
- 查看端口:ss -tulnp | grep -E ‘:(80|443)’ 或 netstat -nltp | grep -E ‘80|443’
- 查看进程:ps aux | grep httpd
- 校验配置语法与查看 systemd 日志
- 语法检查:apachectl configtest(输出 Syntax OK 再重启)
- 启动失败原因:journalctl -u httpd.service --since today -e
- 查看应用日志
- 错误日志:tail -f /var/log/httpd/error_log(定位 4xx/5xx、权限、模块报错)
- 访问日志:tail -f /var/log/httpd/access_log(配合错误日志分析请求与响应)
- 外部连通性
- 本机:curl -I http://127.0.0.1 或 https://localhost
- 远程:curl -Iv http://服务器IP 或 https://域名(关注 HTTP/1.1 200/301/302 与证书链)
- 防火墙与安全组
- 云上实例:在控制台安全组放行 TCP 80/443 入方向
- 系统防火墙:firewall-cmd --list-all;必要时放行并重载:firewall-cmd --permanent --add-service=http --add-service=https & & firewall-cmd --reload
- 配置文件与虚拟主机
- 主配置与包含目录:/etc/httpd/conf/httpd.conf、/etc/httpd/conf.d/*.conf
- 检查关键指令:Listen、DocumentRoot、、DirectoryIndex、VirtualHost
二 常见症状与处理要点
| 症状 | 优先检查 | 快速修复 |
|---|---|---|
| 无法访问(页面打不开) | 服务是否运行、80/443 是否监听、云安全组与防火墙 | systemctl start httpd;ss -tulnp |
| 403 Forbidden | 目录权限、 是否 Deny、默认首页是否存在 | chmod 755 /var/www/html;chmod 644 文件;确认 DirectoryIndex;去除错误的 Deny from all |
| 404 Not Found | DocumentRoot 与文件路径、虚拟主机配置 | 核对 VirtualHost 的 DocumentRoot 与真实文件路径;确认文件存在 |
| 500/502/503 | 错误日志、.htaccess 伪静态、权限、后端(如 PHP-FPM) | 查 /var/log/httpd/error_log;临时重命名 .htaccess 验证;修正目录权限;检查后端进程与健康 |
| 启动失败 | 配置语法、端口冲突、SELinux | apachectl configtest;ss -tulnp 查占用并释放或改端口;必要时 setenforce 0 验证 SELinux 因素 |
| 访问卡慢 | 带宽/CPU/磁盘、并发进程数、MPM 模式 | top/vmstat/iostat;ps aux |
| PHP 页面空白或报错 | PHP 错误日志与显示开关 | 在 php.ini 中设置 display_errors=Off、error_reporting=E_ALL;重启 httpd 后复现并查错 |
| 端口被占用 | 谁在占用 80/443 | ss -tulnp |
三 日志与定位技巧
- 错误日志位置与级别
- 路径:/var/log/httpd/error_log;可用 ErrorLog 指令自定义
- 级别:通过 LogLevel 调整(如 error、warn、info),默认多为 error
- 实时查看与检索
- 实时跟踪:tail -f /var/log/httpd/error_log
- 关键字检索:grep “404|refused|denied” /var/log/httpd/error_log
- 读懂典型错误行
- 示例:[Fri Aug 18 22:36:26 2000] [error] [client 192.168.1.6] File does not exist: /usr/local/apache/…(指示缺失资源路径)
- 访问日志配合分析
- 路径:/var/log/httpd/access_log;用于核对请求方法、UA、返回码与来源 IP,辅助定位 4xx/5xx 成因
四 配置语法与端口冲突检查
- 配置语法
- 执行:apachectl configtest;若非 Syntax OK,按提示文件与行号修复,再重启
- 端口冲突
- 查占用:ss -tulnp | grep ‘:80|:443’ 或 lsof -i :80
- 处置:停止占用进程或修改 Apache 的 Listen 端口,随后重启服务
- 防火墙放行
- 推荐:firewall-cmd --permanent --add-service=http --add-service=https & & firewall-cmd --reload
- 临时测试可停用 firewalld,但上线务必恢复并仅放行必要端口
五 SELinux 与权限要点
- 权限基线
- 目录:chmod 755 /var/www/html;文件:chmod 644
- 属主:确保运行用户(常见为 apache)对 DocumentRoot 具备读取与执行权限
- SELinux 场景
- 临时验证:setenforce 0;若恢复正常,说明与 SELinux 策略相关
- 规范修复(示例):为自定义目录设置 httpd 可读上下文并恢复
- semanage fcontext -a -t httpd_sys_content_t “/data/www(/.*)?”
- restorecon -Rv “/data/www”
- 不建议长期关闭 SELinux,应调整策略或上下文以符合最小权限原则
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: CentOS Apache故障排查
本文地址: https://pptw.com/jishu/774493.html
