温馨提示×

centos php日志如何排查故障

小樊
33
2026-09-09 02:58:41
栏目: 编程语言

在 CentOS 上排查 PHP 故障,核心思路是找到日志 → 看错误 → 定位代码/配置 → 验证修复。下面按常见场景一步步说。


一、先确认 PHP 运行方式

不同方式,日志位置不一样:

php -v
which php
ps aux | grep php

常见三种:

  1. PHP-FPM(最常见,配合 Nginx/Apache)
  2. Apache mod_php
  3. CLI 命令行 PHP

二、PHP 错误日志在哪里

1️⃣ PHP-FPM 日志

查看 PHP-FPM 配置:

php-fpm -t
# 或
rpm -ql php-fpm | grep pool

通常路径:

/var/log/php-fpm/error.log
/var/log/php-fpm/www-error.log

查看:

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

2️⃣ php.ini 中的错误日志

先找 php.ini:

php --ini

查看关键配置:

log_errors = On
error_log = /var/log/php_errors.log
display_errors = Off   # 生产环境应为 Off

查看日志:

tail -f /var/log/php_errors.log

3️⃣ Web 服务器日志(非常重要)

Nginx

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

Apache

tail -f /var/log/httpd/error_log
tail -f /var/log/httpd/access_log

4️⃣ 应用自身日志(如 Laravel / ThinkPHP)

常见位置:

/var/www/html/storage/logs/
/var/www/html/runtime/log/

三、常见 PHP 故障 & 排查方法

✅ 1. 页面 500 错误

看 PHP-FPM / Nginx error.log 典型错误:

  • PHP Fatal error
  • Allowed memory size exhausted
  • Call to undefined function

✅ 2. PHP 不报错但页面空白

临时开启错误显示(调试用):

ini_set('display_errors', 1);
error_reporting(E_ALL);

或改 php.ini:

display_errors = On

✅ 3. PHP-FPM 无法启动

systemctl status php-fpm
journalctl -xe

常见原因:

  • 端口被占用(9000)
  • 配置语法错误
  • 用户权限问题(nginx 无法访问 php-fpm sock)

✅ 4. 权限问题(很常见)

ls -l /var/log/php-fpm
ps aux | grep php-fpm

确保:

  • 日志目录可写
  • php-fpm 用户(如 apache / nginx)正确

四、实战排查流程(推荐)

# 1. 看实时错误
tail -f /var/log/php-fpm/error.log
tail -f /var/log/nginx/error.log

# 2. 复现问题
curl http://localhost/test.php

# 3. 根据错误定位代码
grep -R "错误信息" /var/www/html

# 4. 修复后重启
systemctl restart php-fpm

五、常用排查命令合集

php -l file.php        # 语法检查
php -i | grep error    # 查看错误配置
strace -p <pid>        # 深入调试(高级)

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

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

我可以直接帮你定位问题

0