CentOS Apache2 连接问题排查手册
一 快速定位流程
curl -I http://localhost/ 或 wget --spider http://localhost/。若本机可访问而外网不可达,多为网络或访问控制问题。sudo systemctl status httpd、sudo systemctl start httpd、sudo systemctl enable httpd。sudo netstat -tulpen | grep -E ':(80|443)'。若无监听,多为服务未启动或配置错误。sudo firewall-cmd --permanent --add-service=http、sudo firewall-cmd --permanent --add-service=https、sudo firewall-cmd --reload。nc -vz 服务器IP 80、telnet 服务器IP 443。若被拒绝或无响应,回到端口监听与防火墙继续排查。sudo tail -f /var/log/httpd/error_log。二 常见症状与对应处理
| 症状 | 快速判断 | 处理要点 |
|---|---|---|
| ERR_CONNECTION_REFUSED | 端口未监听或被拒绝 | 确认 httpd 运行;netstat 检查 80/443 监听;排查端口冲突;临时关闭防火墙验证;必要时更换端口并放行 |
| ERR_CONNECTION_TIMED_OUT | 防火墙/安全组/网络不通 | 放行防火墙端口;检查云安全组与路由;客户端 ping/traceroute 定位网络链路 |
| 403 Forbidden | 目录/文件权限或配置拒绝 | 检查目录权限与 httpd 运行用户(常见为 apache);确认 Allow/Deny、Require all granted 等访问控制;SELinux 布尔值(如 httpd_read_user_content) |
| 404 Not Found | 资源不存在或 DocumentRoot 错误 | 核对 DocumentRoot 与请求路径;检查 Alias/Directory 配置;确认文件存在 |
| 500 Internal Server Error | 配置语法/脚本错误/后端异常 | apachectl configtest 校验配置;查看 error_log 具体报错;检查 CGI/PHP/FastCGI 与依赖服务 |
| 503 Service Unavailable | 服务过载/进程崩溃/后端不可用 | 检查 error_log 与 journalctl -xe;查看子进程数与资源;确认反向代理或上游服务状态 |
| 以上症状与处理要点可配合日志与监听检查快速缩小范围并修复。 |
三 配置与权限检查
sudo apachectl configtest;主配置通常为 /etc/httpd/conf/httpd.conf,并包含 /etc/httpd/conf.d/*.conf。<VirtualHost> 端口一致;确保站点配置启用且端口未被占用。sudo chown -R apache:apache /var/www/html,sudo chmod -R 755 /var/www/html。sudo setenforce 0;定位策略:getsebool httpd_read_user_content;必要时调整布尔或上下文而非永久关闭。四 日志与高级定位
sudo tail -f /var/log/httpd/error_log,配合 grep/awk 分析错误类型与频次。sudo journalctl -u httpd -xe。mod_status 查看 Server Status 与 uptimesudo systemctl restart httpd 后访问 http://服务器IP/server-status(按需限制来源 IP)。netstat -an | grep :80 | sort | uniq -c 观察 TIME_WAIT/CLOSE_WAIT 堆积top、free -m、df -h 排查内存、CPU、磁盘与句柄限制。