温馨提示×

centos php日志怎样找错

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

在 CentOS 上排查 PHP 错误,通常是从日志位置 → 日志内容 → 常见错误类型一步步来。下面给你一套实用方法。


一、先确认 PHP 运行方式

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

php -v

再看是:

  • PHP-FPM(最常见,配合 Nginx/Apache)
  • Apache mod_php
  • 命令行 PHP(CLI)

二、PHP 错误日志在哪里

1️⃣ PHP-FPM 方式(CentOS + Nginx 常见)

查看 PHP-FPM 配置:

php-fpm -i | grep error_log
# 或
grep error_log /etc/php-fpm.conf
grep error_log /etc/php-fpm.d/www.conf

常见位置:

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

实时查看:

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

2️⃣ Apache + PHP(mod_php)

Apache 错误日志里通常包含 PHP 错误:

/var/log/httpd/error_log

查看:

tail -f /var/log/httpd/error_log

3️⃣ PHP 自身 error_log 配置

查看 php.ini:

php --ini

常见文件:

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

搜索:

grep error_log /etc/php.ini

如果看到:

log_errors = On
error_log = /var/log/php_errors.log

那就是这个文件。


4️⃣ 网站代码里指定的日志

有些程序(WordPress、Laravel 等)自己写日志:

/var/www/html/xxx/wp-content/debug.log
/var/www/html/xxx/storage/logs/laravel.log

三、如果“看不到错误”怎么办

1️⃣ 临时开启错误显示(开发环境)

在 php.ini:

display_errors = On
error_reporting = E_ALL

重启服务:

systemctl restart php-fpm
# 或
systemctl restart httpd

⚠️ 生产环境不建议长期开启


2️⃣ 在 PHP 文件里临时调试

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

四、常见 PHP 错误 & 含义

日志内容 含义
PHP Fatal error 致命错误,脚本终止
PHP Warning 警告,不终止
PHP Notice 提示,通常变量未定义
Allowed memory size exhausted 内存不足
Maximum execution time 超时
Permission denied 文件/目录权限问题

五、快速排查流程(推荐)

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

# 2. 看 Web 服务日志
tail -f /var/log/nginx/error.log
tail -f /var/log/httpd/error_log

# 3. 看应用日志
ls /var/www/xxx/logs

六、常见坑

  • SELinux 导致写不了日志
getenforce

如果是 Enforcing,可临时:

setenforce 0
  • 目录没权限
chown -R nginx:nginx /var/www
chmod -R 755 /var/www

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

  • CentOS 版本(7 / 8 / Stream)
  • Nginx 还是 Apache
  • PHP 版本

我可以直接帮你定位具体日志文件。

0 踩