在 Debian 上,Apache(通常是 Apache2)的日志配置主要涉及 错误日志(ErrorLog) 和 访问日志(CustomLog / access log),配置文件一般在 /etc/apache2/ 目录下。
下面按常见场景给你说明如何配置。
Debian 下 Apache 日志默认在:
/var/log/apache2/
├── access.log # 访问日志
├── error.log # 错误日志
├── other_vhosts_access.log
Apache 主配置:
/etc/apache2/apache2.conf
虚拟主机配置:
/etc/apache2/sites-available/
/etc/apache2/sites-enabled/
模块配置(如日志格式):
/etc/apache2/mods-available/
/etc/apache2/mods-enabled/
在 apache2.conf 或虚拟主机中:
ErrorLog ${APACHE_LOG_DIR}/error.log
日志级别(可选):
LogLevel warn
常见级别:
debuginfonoticewarn(默认)errorcritCustomLog ${APACHE_LOG_DIR}/access.log combined
combined 是内置日志格式,内容类似:
IP - - [时间] "请求" 状态码 字节 "Referer" "User-Agent"
示例(/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>
启用站点:
a2ensite example.conf
systemctl reload apache2
查看默认格式:
cat /etc/apache2/apache2.conf | grep LogFormat
自定义示例:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" myfmt
CustomLog ${APACHE_LOG_DIR}/access.log myfmt
apache2-logrotate(默认)Debian 已自带:
/etc/logrotate.d/apache2
默认每天或每周轮转,保留 14 天。
查看:
cat /etc/logrotate.d/apache2
rotatelogs(按大小/时间)CustomLog "|/usr/bin/rotatelogs /var/log/apache2/access_%Y%m%d.log 86400" combined
apache2ctl configtest
systemctl restart apache2
chown -R www-data:adm /var/log/apache2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
如果你愿意,可以告诉我:
我可以给你更精确的配置示例。