在 Debian 上配置 Apache 日志,主要涉及 错误日志(ErrorLog) 和 访问日志(CustomLog) 的设置,通常通过 Apache 的配置文件完成。下面按常见场景说明。
Debian 下 Apache 通常为 apache2:
/etc/apache2/apache2.conf/etc/apache2/sites-available//var/log/apache2//etc/apache2/mods-available/常用日志文件:
/var/log/apache2/access.log/var/log/apache2/error.logErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog ${APACHE_LOG_DIR}/access.log common
说明:
ErrorLog:错误日志路径CustomLog:访问日志路径和格式common / combined 是内置日志格式编辑站点配置,例如:
sudo nano /etc/apache2/sites-available/example.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 example.conf
sudo systemctl reload apache2
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
包含:
Debian 使用 logrotate 管理 Apache 日志:
配置文件:
/etc/logrotate.d/apache2
默认已配置,按周或按大小切割,并自动 reload Apache。
如需修改:
sudo nano /etc/logrotate.d/apache2
示例片段:
/var/log/apache2/*.log {
weekly
missingok
rotate 14
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
systemctl reload apache2 > /dev/null 2>&1
endscript
}
已在上面虚拟主机配置中说明。
SetEnvIf Request_URI "\.(jpg|css|js|png)$" static
CustomLog ${APACHE_LOG_DIR}/access.log combined env=!static
<VirtualHost *:443>
...
CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined
</VirtualHost>
查看日志:
sudo tail -f /var/log/apache2/error.log
检查配置:
sudo apache2ctl configtest
重载 Apache:
sudo systemctl reload apache2
如果你有具体需求(如:按天切割、JSON 日志、Docker 环境、特定模块日志),可以告诉我,我给你更针对性的配置。