温馨提示×

centos php日志如何定位

小樊
34
2026-09-03 01:46:24
栏目: 编程语言

在 CentOS 上定位 PHP 日志,通常分三种情况:PHP 自身错误日志、Web 服务器(Nginx/Apache)日志、以及应用框架日志。下面按最常见场景说明。


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

1. 查看 php.ini 配置

php --ini

常见路径:

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

查看错误日志相关配置:

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

关键参数说明:

log_errors = On
error_log = /var/log/php/php_error.log

如果 error_log 为空,PHP 会交给 Web 服务器处理。


二、Nginx + PHP-FPM(最常见)

1. PHP-FPM 错误日志

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

一般位置:

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

查看:

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

2. PHP-FPM pool 日志(www.conf)

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

如果 catch_workers_output = yes,PHP 错误会进 FPM 日志。

3. Nginx 错误日志(非常重要)

grep "error_log" /etc/nginx/nginx.conf

常见路径:

/var/log/nginx/error.log

查看:

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

4. Nginx 访问日志(看请求是否到达 PHP)

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

三、Apache + PHP(mod_php / php-fpm)

Apache 错误日志

tail -f /var/log/httpd/error_log

Apache 访问日志

tail -f /var/log/httpd/access_log

四、快速定位 PHP 问题的常用命令

实时查看错误

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

搜索关键词

grep -i "php" /var/log/nginx/error.log
grep -i "fatal" /var/log/php-fpm/error.log

测试 PHP 是否报错

php -l file.php      # 语法检查
php file.php         # 命令行执行看错误

五、如果“完全看不到日志”

  1. 确认 log_errors = On
  2. 确认日志文件存在且 PHP 进程有写权限
    ls -l /var/log/php/
    
  3. 重启服务
    systemctl restart php-fpm
    systemctl restart nginx
    

六、总结(最常用路径)

场景 日志位置
PHP-FPM /var/log/php-fpm/error.log
Nginx /var/log/nginx/error.log
Apache /var/log/httpd/error_log
PHP 配置 php.ini → error_log

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

  • CentOS 版本
  • 用的是 Nginx 还是 Apache
  • PHP 运行方式(php-fpm / mod_php)

我可以给你精确的一条命令定位方案

0