在 CentOS 上监控 PHP 日志,主要取决于你是用的 PHP-FPM 还是 Apache + mod_php,以及日志存放位置。下面按常见场景给你一份实用指南。
php-fpm -i | grep "error_log"
或查看配置文件:
grep -R "error_log" /etc/php-fpm.d/
grep -R "error_log" /etc/php.ini
常见路径:
/var/log/php-fpm/error.log
/var/log/php-fpm/www-error.log
PHP 错误通常写入 Apache 错误日志:
/var/log/httpd/error_log
如 Laravel、ThinkPHP:
/var/www/html/storage/logs/
/var/www/html/runtime/log/
tail -f /var/log/php-fpm/error.log
多个日志一起看:
tail -f /var/log/php-fpm/error.log /var/log/httpd/error_log
grep -i "fatal\|error\|warning" /var/log/php-fpm/error.log
grep "2026-01-21" /var/log/php-fpm/error.log
journalctl -u php-fpm -f
如果 PHP-FPM 启动失败、重启异常,非常有用。
编辑 php.ini:
vi /etc/php.ini
确保以下配置存在:
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
error_reporting = E_ALL
重启 PHP-FPM:
systemctl restart php-fpm
/etc/logrotate.d/php-fpm
示例:
/var/log/php-fpm/*.log {
daily
rotate 7
missingok
notifempty
compress
delaycompress
postrotate
systemctl reload php-fpm > /dev/null 2>&1 || true
endscript
}
将 PHP 日志统一收集到系统日志。
| 工具 | 作用 |
|---|---|
| Prometheus + Grafana | 日志告警 |
| ELK (Elasticsearch + Logstash + Kibana) | 企业级日志分析 |
| Fail2ban | 监控异常访问 |
| Zabbix | PHP 错误告警 |
| 错误 | 可能原因 |
|---|---|
| PHP Fatal error | 代码错误、扩展缺失 |
| Allowed memory size exhausted | 内存不足 |
| Maximum execution time | 超时 |
| Permission denied | 目录权限 |
# 查看 PHP 日志
tail -f /var/log/php-fpm/error.log
# 查看 Apache 日志
tail -f /var/log/httpd/error_log
# 查看 PHP-FPM 状态
systemctl status php-fpm
# 查看 PHP 错误是否开启
php -i | grep log_errors
如果你愿意,可以告诉我:
我可以帮你定制一套 PHP 日志监控 + 告警方案。