centos php日志怎么分析

2026-07-09编程开发308038

CentOS 上分析 PHP 日志,一般分三类:PHP 错误日志、PHP-FPM 日志、Web 服务器(Nginx/Apache)日志。下面按常见场景一步步说明。


一、找到 PHP 日志位置

1️⃣ PHP 错误日志(最常见)

查看 php.ini 配置

php -i | grep "Loaded Configuration File"

常见路径:

/etc/php.ini
/etc/php.d/*.ini

查找日志路径:

grep -R "error_log" /etc/php.ini /etc/php.d/

常见配置项:

log_errors = On
error_log = /var/log/php/error.log
error_reporting = E_ALL

✅ 如果没配置 error_log,PHP 错误可能写入 Web 服务日志


2️⃣ PHP-FPM 日志(PHP-FPM 模式)

查看 fpm 配置

php-fpm -t

常见路径:

/etc/php-fpm.conf
/etc/php-fpm.d/www.conf

查找日志:

grep -R "error_log" /etc/php-fpm.conf /etc/php-fpm.d/

常见配置:

error_log = /var/log/php-fpm/error.log

3️⃣ Nginx + PHP 场景

Nginx 错误日志

/var/log/nginx/error.log

Nginx 访问日志

/var/log/nginx/access.log

如果 PHP 报错但页面空白,通常在这里能看到 FastCGI 错误


4️⃣ Apache + PHP 场景

/var/log/httpd/error_log
/var/log/httpd/access_log

二、常用日志分析命令(重点)

1️⃣ 实时查看日志

tail -f /var/log/php/error.log

2️⃣ 查找 PHP 错误

grep -i "error\|exception\|fatal" /var/log/php/error.log

3️⃣ 按时间过滤

grep "2025-01-20" /var/log/php/error.log

4️⃣ 统计错误出现次数

awk '{print $1}' /var/log/php/error.log | sort | uniq -c | sort -nr

5️⃣ 查看 Nginx + PHP 报错

tail -f /var/log/nginx/error.log | grep php

常见错误:

  • FastCGI sent in stderr
  • PHP Fatal error
  • Allowed memory size exhausted

三、常见 PHP 错误分析示例

❌ Fatal error

PHP Fatal error: Allowed memory size exhausted

✅ 解决:

memory_limit = 256M

❌ 文件权限问题

failed to open stream: Permission denied

✅ 解决:

chown -R nginx:nginx /var/www/html
chmod -R 755 /var/www/html

❌ PHP-FPM 无法启动

systemctl status php-fpm
journalctl -xe

四、推荐使用工具(进阶)

1️⃣ 日志分析工具

goaccess /var/log/nginx/access.log

2️⃣ 多日志同时查看

tail -f /var/log/php/error.log /var/log/nginx/error.log

五、快速排查流程(建议收藏)

  1. 确定 PHP 运行模式(FPM / Apache mod_php)
  2. 找到对应的 error_log
  3. tail -f 实时复现问题
  4. 搜索 Fatal / Warning / Error
  5. 结合 Nginx/Apache 错误日志

如果你愿意,可以告诉我:

  • ✅ CentOS 版本(7 / 8 / Stream)
  • ✅ PHP 版本
  • ✅ Web 服务(Nginx / Apache)
  • ✅ 具体报错内容

我可以 直接帮你定位问题

《centos php日志怎么分析.doc》

下载本文的Word格式文档,以方便收藏与打印。