在LNMP(Linux, Nginx, MySQL, PHP)環境下進行日志管理,主要包括以下幾個方面:
access_log
指令指定日志文件的路徑和格式。例如:access_log /var/log/nginx/access.log;
php.ini
文件進行配置??梢栽O置日志級別、日志文件路徑等。例如:error_reporting = E_ALL
display_errors = Off
log_errors = On
error_log = /var/log/php-fpm/www-error.log
error_reporting()
函數動態設置日志級別。my.cnf
或my.ini
)來配置這些日志的生成和存儲路徑。slow_query_log = Off
logrotate
工具來實現日志輪轉。logrotate
的示例:/var/log/nginx/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
這個配置表示每天輪轉一次Nginx日志,保留最近7天的日志文件,并壓縮舊的日志文件。GoAccess
和ELK
(Elasticsearch, Logstash, Kibana)棧。GoAccess
分析Nginx日志的步驟:
GoAccess
:wget https://tar.goaccess.io/goaccess-1.5.6.tar.gz
tar -xzvf goaccess-1.5.6.tar.gz
cd goaccess-1.5.6/
./configure --enable-utf8 --enable-geoipmmdb
make
sudo make install
goaccess -a -d -f /var/log/nginx/access.log --log-format '%h %[%d:%t %] "%r" %s %b "%R" "%u" %' --date-format %d/%b/%Y --time-format %T -o /var/www/html/goaccess.html
/dev/null
可以節省磁盤空間,但可能會丟失重要信息。建議在確保需要這些日志的情況下再關閉它們。通過以上方法,可以在LNMP環境下有效地進行日志管理,確保系統安全和穩定運行。