在Debian上使用LNMP(Linux, Nginx, MySQL/MariaDB, PHP)环境时,日志管理是一项重要的任务,它有助于监控系统性能、排查问题以及识别潜在的安全风险。以下是一些有效的Debian LNMP日志管理技巧:
Nginx日志:
tail -f /var/log/nginx/access.log 命令实时查看访问日志。tail -f /var/log/nginx/error.log 命令实时查看错误日志。PHP-FPM日志:
/var/log/php-fpm/ 目录下,文件名通常包含进程ID和日期。tail -f /var/log/php-fpm/www-data-*.log 命令实时查看PHP-FPM的日志。logrotate 工具进行日志轮转,防止日志文件过大。logrotate 是Linux系统自带的日志管理工具,可以自动切割、压缩、邮件通知等。logrotate 规则文件,例如 /etc/logrotate.d/nginx,定义日志轮转的时间和格式。/etc/nginx/nginx.conf,在 http 块中定义日志格式:http {
log_format main 'remote_addr - remote_user [time_local] ' ' "request" status body_bytes_sent ' ' "http_referer" " http_user_agent" ' ;
...
}
server 或 location 块中使用 access_log 和 error_log 指令记录日志。sudo systemctl restart nginx、sudo systemctl restart php7.0-fpm。sudo systemctl status nginx、sudo systemctl status php7.0-fpm。logrotate 工具进行日志轮转。通过上述方法,你可以在Debian上有效地管理LNMP环境的日志,确保系统安全和性能优化。