在 Ubuntu 上监控 PHP 日志,常见做法分为 查看错误日志、实时跟踪日志、以及使用工具自动监控告警。下面按实用程度给你一套方案。
php --ini
或
php -i | grep error_log
常见位置:
/var/log/php/php-error.log/var/log/apache2/error.log(Apache)/var/log/nginx/error.log(Nginx + PHP-FPM)ls /var/log/php*/
# 如 /var/log/php8.1-fpm.log
tail -f /var/log/php/php-error.log
多个日志一起看:
tail -f /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/apache2/error.log
tail -f /var/log/nginx/error.log
tail -f /var/log/php8.1-fpm.log
sudo apt install logwatch
sudo logwatch --detail High --service php
sudo apt install fail2ban
配置示例(监控 PHP 错误):
[php-errors]
enabled = true
logpath = /var/log/php/php-error.log
maxretry = 10
sudo apt install monit
可监控 PHP-FPM 是否存活、日志异常等。
[program:php-worker]
command=php /var/www/app/worker.php
stdout_logfile=/var/log/php/worker.log
适合多台服务器统一监控 PHP 日志。
php.ini:
log_errors = On
error_log = /var/log/php/php-error.log
display_errors = Off
改完重启:
sudo systemctl restart php8.1-fpm
# 查看最近 100 行
tail -n 100 /var/log/php/php-error.log
# 按时间查
grep "2026-01-21" /var/log/php/php-error.log
# 查 Fatal Error
grep -i "fatal" /var/log/php/php-error.log
如果你是:
可以告诉我具体场景,我可以给你更精准的监控方案。