Apache 故障排查 #

故障排查概述 #

排查流程 #

text
┌─────────────────────────────────────────────────────────────┐
│                    故障排查流程                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  1. 确认问题                                                │
│     ├── 问题现象                                           │
│     ├── 发生时间                                           │
│     └── 影响范围                                           │
│                                                             │
│  2. 收集信息                                                │
│     ├── 错误日志                                           │
│     ├── 访问日志                                           │
│     ├── 系统资源                                           │
│     └── 配置文件                                           │
│                                                             │
│  3. 分析原因                                                │
│     ├── 配置错误                                           │
│     ├── 权限问题                                           │
│     ├── 资源不足                                           │
│     └── 网络问题                                           │
│                                                             │
│  4. 解决问题                                                │
│     ├── 修复配置                                           │
│     ├── 调整参数                                           │
│     ├── 增加资源                                           │
│     └── 重启服务                                           │
│                                                             │
│  5. 验证结果                                                │
│     ├── 测试功能                                           │
│     ├── 监控状态                                           │
│     └── 记录文档                                           │
│                                                             │
└─────────────────────────────────────────────────────────────┘

常用诊断命令 #

服务状态检查 #

bash
# 查看服务状态
sudo systemctl status apache2    # Ubuntu/Debian
sudo systemctl status httpd      # CentOS/RHEL

# 查看进程
ps aux | grep apache2
ps aux | grep httpd

# 查看端口
sudo netstat -tlnp | grep :80
sudo ss -tlnp | grep :80

# 查看连接数
sudo netstat -an | grep :80 | wc -l

配置检查 #

bash
# 测试配置语法
sudo apache2ctl configtest    # Ubuntu/Debian
sudo apachectl configtest     # CentOS/RHEL
sudo httpd -t                 # 通用

# 查看虚拟主机配置
sudo apache2ctl -S

# 查看已加载模块
sudo apache2ctl -M

# 查看编译参数
sudo apache2ctl -V

日志查看 #

bash
# 实时查看错误日志
sudo tail -f /var/log/apache2/error.log

# 实时查看访问日志
sudo tail -f /var/log/apache2/access.log

# 查看最近错误
sudo tail -n 100 /var/log/apache2/error.log

# 搜索特定错误
sudo grep "error" /var/log/apache2/error.log
sudo grep "Permission denied" /var/log/apache2/error.log

# 按时间过滤
sudo grep "Mar 29" /var/log/apache2/error.log

启动失败问题 #

端口被占用 #

bash
# 错误信息
# (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80

# 查找占用进程
sudo lsof -i :80
sudo netstat -tlnp | grep :80

# 解决方法
# 停止占用服务
sudo systemctl stop nginx

# 或修改 Apache 端口
# 编辑 ports.conf 或 httpd.conf
Listen 8080

配置语法错误 #

bash
# 错误信息
# AH00526: Syntax error on line XX of /etc/apache2/sites-enabled/example.conf

# 测试配置
sudo apache2ctl configtest

# 查看详细错误
sudo apache2ctl -t -D DUMP_VHOSTS

# 解决方法
# 检查配置文件
# - 拼写错误
# - 缺少引号
# - 标签未闭合
# - 指令参数错误

模块加载失败 #

bash
# 错误信息
# Cannot load modules/mod_example.so into server

# 检查模块文件
ls /usr/lib/apache2/modules/

# 检查模块配置
cat /etc/apache2/mods-enabled/example.load

# 解决方法
# 禁用问题模块
sudo a2dismod example
sudo systemctl restart apache2

权限问题 #

bash
# 错误信息
# Permission denied: AH00072

# 检查文件权限
ls -la /var/log/apache2/
ls -la /var/www/html/

# 检查目录权限
namei -l /var/www/html/

# 解决方法
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

配置问题 #

虚拟主机不生效 #

bash
# 检查虚拟主机配置
sudo apache2ctl -S

# 检查站点是否启用
ls /etc/apache2/sites-enabled/

# 解决方法
# 启用站点
sudo a2ensite example.conf

# 重载配置
sudo systemctl reload apache2

.htaccess 不生效 #

apache
# 检查 AllowOverride 设置
<Directory /var/www/html>
    AllowOverride All    # 必须设置为 All 或特定选项
</Directory>

# 检查是否加载 rewrite 模块
sudo apache2ctl -M | grep rewrite

# 解决方法
sudo a2enmod rewrite
sudo systemctl restart apache2

重定向循环 #

bash
# 错误信息
# ERR_TOO_MANY_REDIRECTS

# 检查重定向配置
# 查看虚拟主机配置中的 Redirect 和 RewriteRule

# 解决方法
# 检查重定向条件
# 避免循环重定向
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

访问问题 #

403 Forbidden #

bash
# 错误信息
# AH00035: access to / denied

# 检查目录权限
<Directory /var/www/html>
    Require all granted    # 允许访问
</Directory>

# 检查文件权限
ls -la /var/www/html/

# 检查 SELinux(CentOS)
getenforce
ls -Z /var/www/html/

# 解决方法
# 设置正确权限
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

# SELinux 上下文
sudo chcon -R -t httpd_sys_content_t /var/www/html

404 Not Found #

bash
# 检查文件是否存在
ls -la /var/www/html/index.html

# 检查 DocumentRoot
grep DocumentRoot /etc/apache2/sites-enabled/*

# 检查 DirectoryIndex
grep DirectoryIndex /etc/apache2/apache2.conf

# 解决方法
# 创建缺失文件
# 检查 URL 路径
# 配置默认首页
DirectoryIndex index.html index.php

500 Internal Server Error #

bash
# 查看错误日志
sudo tail -n 50 /var/log/apache2/error.log

# 常见原因
# - PHP 语法错误
# - .htaccess 配置错误
# - 权限问题
# - 模块加载失败

# 解决方法
# 根据错误日志修复具体问题

性能问题 #

响应缓慢 #

bash
# 检查服务器负载
top
htop

# 检查内存使用
free -h

# 检查磁盘 I/O
iostat -x 1

# 检查网络
netstat -s

# 检查 Apache 进程
ps aux | grep apache2 | wc -l

# 解决方法
# 优化 MPM 配置
# 启用缓存
# 启用压缩
# 检查后端服务

连接超时 #

bash
# 检查超时配置
grep Timeout /etc/apache2/apache2.conf

# 检查连接数
sudo netstat -an | grep :80 | wc -l

# 解决方法
# 增加超时时间
Timeout 300

# 优化 MPM 配置
<IfModule mpm_event_module>
    MaxRequestWorkers 500
</IfModule>

内存不足 #

bash
# 检查内存使用
free -h
ps aux --sort=-%mem | head

# 检查 Apache 进程内存
ps aux | grep apache2

# 解决方法
# 减少 MaxRequestWorkers
# 限制每个进程内存
# 启用 MaxConnectionsPerChild
MaxConnectionsPerChild 1000

SSL 问题 #

证书错误 #

bash
# 错误信息
# SSL_ERROR_BAD_CERT_DOMAIN

# 检查证书
openssl x509 -in /etc/ssl/certs/example.com.crt -text -noout

# 检查证书链
openssl s_client -connect example.com:443

# 解决方法
# 更新证书
# 检查证书链配置
SSLCertificateFile /etc/ssl/certs/example.com.crt
SSLCertificateKeyFile /etc/ssl/private/example.com.key
SSLCertificateChainFile /etc/ssl/certs/chain.crt

HTTPS 重定向问题 #

bash
# 检查重定向配置
# 确保 HTTP 重定向到 HTTPS

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

PHP 问题 #

PHP 文件下载而非执行 #

bash
# 检查 PHP 模块
sudo apache2ctl -M | grep php

# 检查处理器配置
<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

# 解决方法
# 安装 PHP 模块
sudo apt install libapache2-mod-php

# 或使用 PHP-FPM
sudo apt install php-fpm
sudo a2enmod proxy_fcgi

PHP 错误不显示 #

php
// 检查 PHP 配置
php -i | grep display_errors

// 临时启用错误显示
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

调试技巧 #

启用详细日志 #

apache
# 增加日志级别
LogLevel debug

# 特定模块日志
LogLevel debug ssl:trace3
LogLevel debug rewrite:trace6

使用 curl 测试 #

bash
# 测试 HTTP 头
curl -I http://example.com

# 测试 HTTPS
curl -k https://example.com

# 测试特定 Host
curl -H "Host: example.com" http://192.168.1.100/

# 测试 POST 请求
curl -X POST -d "data=value" http://example.com/api

# 显示详细信息
curl -v http://example.com

使用 telnet 测试 #

bash
# 测试连接
telnet example.com 80

# 发送请求
GET / HTTP/1.1
Host: example.com

问题速查表 #

text
┌─────────────────────────────────────────────────────────────┐
│                    常见问题速查表                            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  启动失败                                                   │
│  ├── 端口被占用 ──> lsof -i :80                            │
│  ├── 配置错误 ──> apache2ctl configtest                    │
│  ├── 模块缺失 ──> apache2ctl -M                            │
│  └── 权限问题 ──> 检查文件权限                             │
│                                                             │
│  访问问题                                                   │
│  ├── 403 ──> 检查 Require 和文件权限                       │
│  ├── 404 ──> 检查 DocumentRoot 和文件存在                  │
│  ├── 500 ──> 查看错误日志                                  │
│  └── 重定向循环 ──> 检查 RewriteRule                       │
│                                                             │
│  性能问题                                                   │
│  ├── 响应慢 ──> 检查负载和资源                             │
│  ├── 超时 ──> 增加 Timeout 配置                            │
│  └── 内存不足 ──> 减少 MaxRequestWorkers                   │
│                                                             │
│  SSL 问题                                                   │
│  ├── 证书错误 ──> openssl s_client                         │
│  └── HTTPS 不工作 ──> 检查 443 端口和 SSL 配置             │
│                                                             │
└─────────────────────────────────────────────────────────────┘

恭喜你完成了 Apache 完全指南的学习!现在你已经掌握了从基础到高级的 Apache Web 服务器知识。

最后更新:2026-03-29