温馨提示×

centos php日志怎么排查

小樊
47
2026-09-18 06:32:29
栏目: 编程语言

在 CentOS 上排查 PHP 日志,一般分 PHP 本身错误日志、Web 服务器(Nginx/Apache)日志、以及 应用框架日志 三类。下面按实战流程讲。


一、先确认 PHP 错误日志在哪里

1. 查看 php.ini 配置

php --ini

或

php -i | grep php.ini

常见路径:

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

查看关键配置:

grep -E "log_errors|error_log|display_errors" /etc/php.ini

示例:

log_errors = On
error_log = /var/log/php/php_errors.log
display_errors = Off

如果没有 error_log,PHP 会写到:

  • Apache:/var/log/httpd/error_log
  • Nginx + php-fpm:php-fpm 日志

二、PHP-FPM 日志(最常见)

1. 找到 php-fpm 配置

php-fpm -t

通常:

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

查看:

grep -E "error_log|slowlog|catch_workers_output" /etc/php-fpm.d/www.conf

常见日志位置:

error_log = /var/log/php-fpm/error.log
slowlog = /var/log/php-fpm/slow.log
catch_workers_output = yes

2. 实时查看 PHP 错误

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

三、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

四、常见 PHP 问题排查思路

1. 页面空白 / 500 错误

tail -100 /var/log/php-fpm/error.log
tail -100 /var/log/nginx/error.log

重点看:

  • PHP Fatal error
  • Allowed memory size
  • Timeout

2. PHP 执行慢

开启 slowlog:

slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 2s

查看:

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

3. 权限问题

ls -l /var/log/php-fpm/

确保 php-fpm 用户(如 www-data / nginx / apache)可写。


五、临时调试(不推荐生产长期开)

在 php.ini:

display_errors = On
error_reporting = E_ALL

或代码里:

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

六、快速排查命令汇总

# PHP 配置
php -i | grep error

# PHP-FPM 日志
tail -f /var/log/php-fpm/error.log

# Nginx
tail -f /var/log/nginx/error.log

# Apache
tail -f /var/log/httpd/error_log

如果你能告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • Web 服务(Nginx / Apache)
  • PHP 运行方式(mod_php / php-fpm)

我可以给你 更精确的日志路径和示例。

0 踩