在 Ubuntu 上监控 PHP 日志,常见做法取决于 PHP 的运行方式(CLI / PHP-FPM / Apache mod_php) 以及你想要的 实时监控程度。下面按场景给你一套实用方案。
日志通常在:
/var/log/php*/fpm.log
/var/log/php*/fpm-www.log
PHP 错误日志(php.ini 中配置):
error_log = /var/log/php/php-error.log
查看 php.ini 位置:
php --ini
# 或
php-fpm -i | grep error_log
错误一般写在 Apache 日志中:
/var/log/apache2/error.log
CLI 一般:
error_log 指定的文件tail -f /var/log/php/php-error.log
多个文件一起看:
tail -f /var/log/php/*.log
tail -f /var/log/php*/fpm.log
# PHP-FPM
journalctl -u php8.1-fpm -f
# Apache
journalctl -u apache2 -f
安装:
sudo apt install multitail
使用:
multitail /var/log/php/php-error.log /var/log/nginx/error.log
tail -f /var/log/php/php-error.log | grep -i error
或高亮:
tail -f /var/log/php/php-error.log | grep --color=auto -i 'fatal\|error\|warning'
sudo apt install logwatch
适合监控 PHP 报错暴露的扫描行为:
sudo apt install fail2ban
日志没内容?
php.inilog_errors = On
error_reporting = E_ALL
sudo systemctl restart php8.1-fpm
权限问题
sudo chown www-data:www-data /var/log/php
✅ 开发环境:
tail -f /var/log/php/php-error.log
✅ 生产环境:
journalctl -u php8.1-fpm -f
+ logwatch 每日报告
如果你愿意,可以告诉我:
我可以给你一套精准配置方案。