Apache2 Ubuntu 配置调试实操指南
一 快速定位流程
二 配置文件结构与启用机制
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>
三 常见错误与修复对照表
| 症状 | 快速检查 | 修复建议 |
|---|---|---|
| 403 Forbidden | 查看 error.log;检查目录权限与访问控制 | 在对应 |
| Address already in use | **netstat -tulpen | grep ‘:80’** |
| Syntax OK 但页面异常 | 访问日志与错误日志同时跟踪 | 用 tail -f 观察请求与报错;核对 DocumentRoot、.htaccess、模块加载顺序 |
| 站点未生效 | apache2ctl configtest;ls /etc/apache2/sites-enabled/ | 使用 a2ensite 启用站点并 reload;确认 ServerName 与 VirtualHost 匹配 |
| 用户目录访问被拒 | 检查 UserDir 模块与目录权限 | 启用 userdir 模块,确保 /home/user 与网站目录权限正确(如 711/755) |
| 中文乱码 | 查看响应头与页面 meta | 启用或调整 charset.conf:sudo a2enconf charset 并取消注释 AddDefaultCharset UTF-8,然后 reload |
四 进阶调试与运维
<Location /server-status>
SetHandler server-status
Require local
</Location>
五 一键排障清单