温馨提示×

PHP日志如何开启CentOS

小樊
40
2026-08-04 06:14:49
栏目: 编程语言

CentOS 上开启 PHP 日志,一般分为 PHP-FPMPHP 错误日志 两部分。下面按常见场景给你一个完整说明(适用于 CentOS 7/8、Rocky、Alma 也类似)。


一、确认 PHP 运行方式

先确认你用的是 PHP-FPM 还是 Apache mod_php

php -v
ps -ef | grep php

常见:

  • PHP-FPMphp-fpm:x.x
  • Apache + mod_phphttpd 进程

二、开启 PHP 错误日志(通用)

1️⃣ 修改 php.ini

找到 php.ini 文件位置:

php --ini

常见路径:

/etc/php.ini
/etc/php/7.x/cli/php.ini
/etc/php/7.x/fpm/php.ini

编辑:

vim /etc/php.ini

2️⃣ 开启错误日志

确保以下配置存在并取消注释:

display_errors = Off
log_errors = On
error_reporting = E_ALL
error_log = /var/log/php/php_error.log

生产环境display_errors = Off
开发环境display_errors = On


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

1️⃣ PHP-FPM 错误日志

编辑 PHP-FPM 配置文件:

vim /etc/php-fpm.conf

或:

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

设置:

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

catch_workers_output = yes 非常重要,否则 PHP 错误不会记录到 FPM 日志。


2️⃣ 创建日志目录并设置权限

mkdir -p /var/log/php-fpm
chown -R apache:apache /var/log/php-fpm
# 或 nginx
chown -R nginx:nginx /var/log/php-fpm

四、重启服务

PHP-FPM

systemctl restart php-fpm

Apache

systemctl restart httpd

Nginx

systemctl restart nginx

五、查看 PHP 日志

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

六、Apache 特有(mod_php)

如果是 Apache + mod_php:

vim /etc/httpd/conf/httpd.conf

添加:

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

七、SELinux 问题(CentOS 常见)

如果日志写不进去,可能是 SELinux:

setenforce 0

临时关闭测试,如果正常,再配置 SELinux:

semanage fcontext -a -t httpd_log_t "/var/log/php(/.*)?"
restorecon -Rv /var/log/php

八、快速检查清单 ✅

  • [ ] log_errors = On
  • [ ] error_log 路径存在
  • [ ] 目录权限正确
  • [ ] PHP-FPM 重启
  • [ ] SELinux 未拦截

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

  • ✅ PHP 版本
  • ✅ Apache / Nginx
  • ✅ CentOS 7 还是 8

我可以给你 精确到路径 的配置示例。

0