在 Ubuntu 上,Apache2 的日志管理主要涉及日志位置、日志类型、查看方式、轮转(rotate)和自定义配置。下面按实用角度说明。
Ubuntu 中 Apache2 日志通常在:
/var/log/apache2/
常见文件:
access.log:访问日志error.log:错误日志other_vhosts_access.log:虚拟主机访问日志(如启用)ssl_access.log / ssl_error.log:SSL 相关日志(如启用 HTTPS)sudo tail -f /var/log/apache2/access.log
sudo tail -f /var/log/apache2/error.log
sudo tail -n 100 /var/log/apache2/error.log
sudo grep "500" /var/log/apache2/access.log
127.0.0.1 - - [12/Jan/2026:10:00:00 +0800] "GET /index.html HTTP/1.1" 200 2326
含义:
[Sun Jan 12 10:01:00.123456 2026] [php:error] [pid 1234] script '/var/www/html/test.php' not found
在 /etc/apache2/sites-available/xxx.conf 中:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
ErrorLog ${APACHE_LOG_DIR}/example_error.log
CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>
修改后:
sudo a2ensite xxx
sudo systemctl reload apache2
Ubuntu 使用 logrotate:
配置文件:
/etc/logrotate.d/apache2
通常默认已配置:
手动测试:
sudo logrotate -d /etc/logrotate.d/apache2
强制执行:
sudo logrotate -f /etc/logrotate.d/apache2
sudo tail -f /var/log/apache2/error.log
看 access.log 中请求路径是否正确
Permission denied: /var/www/html
→ 检查目录权限和 www-data 用户
Q:没有 access.log?
可能未启用 CustomLog,检查虚拟主机配置。
Q:日志不更新?
sudo systemctl restart apache2
如果你有具体需求(比如:
可以告诉我,我给你具体配置。