nginx和php-fpm环境记录php错误日志

转载自 http://www.jb51.net/article/49645.htm
nginx与apache不一样,在apache中可以直接指定php的错误日志,那样在php执行中的错误信息就直接输入到php的错误日志中,可以方便查询。
在nginx中事情就变成了这样:nginx只对页面的访问做access记录日志。不会有php的error log 信息。或者记录如下的很模糊的错误信息

2016/10/18 15:12:53 [error] 3232#0: *4688 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 121.121.121.121, server: dz.snsnb.com, request: "GET /admin.php?m=content&c=create_html&a=public_index&pc_hash=46yC97 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "xxx.xxx.xxx", referrer: "http://xxx.xxx.xxx/admin.php?m=admin"

nginx把对php的请求发给php-fpm fastcgi进程来处理,默认的php-fpm只会输出php-fpm的错误信息,在php-fpm的errors log里也看不到php的errorlog。
原因是php-fpm的配置文件php-fpm.conf中默认是关闭worker进程的错误输出,直接把他们重定向到/dev/null,所以我们在nginx的error log 和php-fpm的errorlog都看不到php的错误日志。
所以我们要进行如下的设置就能查看到nginx下php-fpm不记录php错误日志的方法:

1,修改php-fpm.conf中的配置,如果没有请增加:(这一步做完,php的错误记录就会记录在php-fpm的日志中)

[global]
; Note: the default prefix is /usr/local/php/var
error_log = log/php_error_log
[www]
catch_workers_output = yes

2.修改php.ini中配置,没有则增加:(这一步无效,可能是与我的环境有关,不纠结了,第二步就已经可以看错误日志了)

log_errors = On
error_log = "/usr/local/php/var/log/error_log"
error_reporting=E_ALL&~E_NOTICE

3.重启php-fpm

当PHP执行错误时就能看到错误日志在”/usr/local/lnmp/php/var/log/php_error_log”中了
如果出现:

[root@localhost etc]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm [17-Apr-2014 18:40:52] ERROR: [/usr/local/php/etc/php-fpm.conf:5] unknown entry 'catch_workers_
[17-Apr-2014 18:40:52] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'
[17-Apr-2014 18:40:52] ERROR: FPM initialization failed
 failed

那请在第一步的时候,认真将配置写入相对应的组中,不然就出现上面的:

ERROR: [/usr/local/php/etc/php-fpm.conf:5] unknown entry ‘catch_workers_output'

标签: php, nginx, php-fpm

添加新评论