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
/var/log/custom_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
}
(默認配置為每日輪轉,保留52周日志,壓縮舊日志)重啟服務生效
sudo systemctl restart apache2
驗證配置
tail -f /var/log/apache2/access.log
或 error.log
說明:
${APACHE_LOG_DIR}
默認指向/var/log/apache2
,可通過修改主配置文件中的APACHE_LOG_DIR
變量調整。LogFormat
中定義格式字符串,再在CustomLog
中引用。