在 Ubuntu 上,使用 Apache2 时,日志一般分为 访问日志 和 错误日志。下面给你一份常用说明。
Ubuntu 下 Apache2 默认日志目录:
/var/log/apache2/
常见日志文件:
/var/log/apache2/access.log # 访问日志
/var/log/apache2/error.log # 错误日志
如果你启用了虚拟主机(vhost),通常每个站点会有独立日志:
/var/log/apache2/example.com-access.log
/var/log/apache2/example.com-error.log
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 "404" /var/log/apache2/access.log
sudo grep "PHP" /var/log/apache2/error.log
/etc/apache2/apache2.conf
通常在:
/etc/apache2/sites-available/000-default.conf
示例:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
修改后重启 Apache:
sudo systemctl restart apache2
默认 combined 格式示例:
192.168.1.10 - - [01/Jan/2026:10:00:00 +0800] "GET /index.html HTTP/1.1" 200 2326 "https://example.com/" "Mozilla/5.0"
字段含义:
Ubuntu 默认使用 logrotate 管理 Apache 日志:
配置文件:
/etc/logrotate.d/apache2
一般会自动:
手动执行:
sudo logrotate -f /etc/logrotate.d/apache2
sudo journalctl -u apache2
sudo cat /var/log/apache2/error.log
确保日志目录权限正确:
sudo chown -R www-data:adm /var/log/apache2
如果你有具体需求(比如 只看某个 IP、统计访问量、PHP 报错、HTTPS 日志、Docker 中的 Apache),可以告诉我,我给你更针对性的命令。