温馨提示×

centos php日志怎样定位错误

小樊
36
2026-09-06 06:18:39
栏目: 编程语言

在 CentOS 上定位 PHP 错误,一般按下面几个步骤来做:


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

1. 查看 php.ini 配置

php --ini

常见路径:

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

查看以下配置:

display_errors = Off        ; 生产环境一般关闭
log_errors = On
error_log = /var/log/php/php_error.log

如果 error_log 没设置,错误可能会写到:

  • Web 服务器日志(Apache / Nginx)
  • 系统日志(journalctl)

二、常见 PHP 日志位置(CentOS)

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

2. Apache + mod_php

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

3. Nginx + PHP-FPM

Nginx 本身不跑 PHP,PHP 错误在 php-fpm 日志:

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

Nginx 错误:

/var/log/nginx/error.log

三、实时查看错误(排错最常用)

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

或:

tail -f /var/log/httpd/error_log

访问出问题的页面,观察日志输出。


四、开启详细错误(临时排错)

临时修改 php.ini

display_errors = On
error_reporting = E_ALL

重启服务:

systemctl restart php-fpm
# 或
systemctl restart httpd

⚠️ 生产环境排完错要改回 Off


五、使用 journalctl(systemd)

如果是 PHP-FPM:

journalctl -u php-fpm -f

Apache:

journalctl -u httpd -f

六、常见错误类型快速定位

错误现象 可能位置
500 错误 php-fpm / httpd error_log
空白页 display_errors 关了,看日志
超时 php-fpm slow log
权限问题 Permission denied 在日志中

七、开启 PHP 慢日志(性能问题)

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

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

  • CentOS 版本
  • PHP 运行方式(Apache / Nginx + PHP-FPM)
  • 具体报错现象

我可以帮你精准定位。

0