Ubuntu Apache日志配置主要通过编辑配置文件实现,具体方法如下:
访问日志配置
sudo nano /etc/apache2/apache2.conf
CustomLog
指令指定路径和格式,如:CustomLog ${APACHE_LOG_DIR}/access.log combined
combined
为常用格式,包含IP、请求时间、状态码等信息)错误日志配置
ErrorLog
指令,如:ErrorLog ${APACHE_LOG_DIR}/error.log
虚拟主机日志配置
/etc/apache2/sites-available/example.com.conf
):<VirtualHost *:80>
ServerName example.com
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>
sudo a2ensite example.com.conf
日志轮转配置(可选)
/etc/logrotate.d/apache2
,设置日志轮转规则,例如:/var/log/apache2/*.log {
daily
rotate 52
compress
missingok
postrotate
systemctl reload apache2
endscript
}
(默认配置通常已包含合理规则,可按需调整)重启服务生效
sudo systemctl restart apache2
验证配置
tail -f /var/log/apache2/access.log
或 error.log
说明:
${APACHE_LOG_DIR}
默认指向/var/log/apache2
,可通过修改主配置文件中的APACHE_LOG_DIR
变量调整。LogFormat
指令定义,如需记录特定请求(如/admin
路径),可结合SetEnvIf
指令实现。