温馨提示×

centos php日志如何排查

小樊
44
2026-10-20 15:13:41
栏目: 编程语言

在 CentOS 上排查 PHP 日志,通常可以从 PHP 自身错误日志、Web 服务器(Nginx/Apache)日志、PHP-FPM 日志 三个方向入手。下面按实战步骤说明。


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

1. 查看 php.ini 配置

php --ini

或(常见路径):

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

关键配置项:

display_errors = Off        # 生产环境建议 Off
log_errors = On
error_log = /var/log/php/php_errors.log
error_reporting = E_ALL

如果没有 error_log,PHP 会:

  • CLI:输出到终端
  • FPM:写入 php-fpm 日志

修改后重启:

systemctl restart php-fpm

二、常见 PHP 运行方式及日志位置

1. PHP-FPM(最常见)

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

查看:

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

如找不到:

php-fpm -i | grep error_log

2. Nginx + PHP

Nginx 错误日志:

/var/log/nginx/error.log

Nginx 访问日志:

/var/log/nginx/access.log

常见 PHP 报错现象:

  • FastCGI sent in stderr: "PHP message: ..."
  • connect() failed (111: Connection refused) while connecting to upstream

3. Apache + PHP

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

三、常用排查命令

实时查看日志

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

过滤错误

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

查看最近 100 行

tail -n 100 /var/log/nginx/error.log

四、常见 PHP 问题及日志表现

1. 500 错误

  • 看 php-fpm 或 nginx error.log
  • 常见:
    • 语法错误
    • 内存溢出
    • 超时

2. 空白页面

  • display_errors=Off 且无日志
  • 临时开启:
display_errors = On

3. 超时

PHP Fatal error: Maximum execution time of 30 seconds exceeded

解决:

max_execution_time = 120

4. 内存不足

Allowed memory size of 134217728 bytes exhausted

解决:

memory_limit = 256M

五、快速自检清单

# 1. PHP 是否报错
php -l index.php

# 2. PHP 配置
php -i | grep error

# 3. 服务状态
systemctl status php-fpm
systemctl status nginx

# 4. 权限问题
ls -l /var/log/php-fpm/

如果你能告诉我:

  • CentOS 版本
  • PHP 版本
  • Nginx / Apache
  • 具体报错信息

我可以帮你精准定位问题。

0 踩