在 CentOS 上开启 PHP 日志,通常分为 PHP-FPM 和 PHP 错误日志 两部分。下面按常见场景一步步说明(以 CentOS 7/8 + PHP-FPM 为例)。
php -i | grep "Loaded Configuration File"
常见路径:
/etc/php.ini
/etc/php/7.x/cli/php.ini
/etc/php/7.x/fpm/php.ini
如果是 PHP-FPM,一定要改 FPM 对应的 php.ini。
编辑配置文件:
vim /etc/php.ini
确保以下配置存在并取消注释:
error_reporting = E_ALL
display_errors = Off
log_errors = On
error_log = /var/log/php/php_error.log
说明:
display_errors = Off:不在页面显示错误(生产环境)log_errors = On:开启日志记录error_log:指定日志文件路径mkdir -p /var/log/php
chown -R apache:apache /var/log/php # Apache
# 或
chown -R nginx:nginx /var/log/php # Nginx + PHP-FPM
find /etc -name "php-fpm.conf"
常见路径:
/etc/php-fpm.conf
/etc/php-fpm.d/www.conf
vim /etc/php-fpm.d/www.conf
确保以下配置:
catch_workers_output = yes
php_admin_value[error_log] = /var/log/php/php-fpm-error.log
php_admin_value[log_errors] = On
编辑:
vim /etc/php-fpm.conf
确认:
error_log = /var/log/php/php-fpm.log
log_level = notice
systemctl restart php-fpm
systemctl restart nginx # 或 httpd
<?php
error_log("PHP log test");
undefined_function();
访问该页面后查看日志:
tail -f /var/log/php/php_error.log
| 类型 | 路径 |
|---|---|
| PHP 错误日志 | /var/log/php/php_error.log |
| PHP-FPM 日志 | /var/log/php/php-fpm.log |
| Nginx 错误日志 | /var/log/nginx/error.log |
| Apache 错误日志 | /var/log/httpd/error_log |
如果日志写不进去:
setenforce 0 # 临时关闭(测试用)
永久解决:
semanage fcontext -a -t httpd_log_t "/var/log/php(/.*)?"
restorecon -Rv /var/log/php
如果你愿意,可以告诉我:
我可以给你 完全对应你环境的配置示例。