nginx+fpm记录php的error log

42 篇文章 0 订阅
39 篇文章 0 订阅

前言

将架构从apache+mod_php迁移到nginx+fpm上,会发现如果php脚本出错,nginx的eror_log里不会有记录,只在access_log里记录了500状态码。出现的原因:nginx将php的脚本执行通过fastcgi转到fpm上,所以出错的信息当然不会记录在nginx的error_log里了,但是对于测试环境下程序员的调试这是个悲剧啊,所以我们需要配置fpm的错误日志


php5-fpm.conf

php5-fpm的错误日志是在php5-fpm.conf全局配置中进行设置,查看一下php5-fpm.conf日志:

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
;pid = run/php-fpm.pid
pid = /var/run/php5-fpm.pid

; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written
; in a local file.
; Note: the default prefix is /var
; Default Value: log/php-fpm.log
; error_log = log/php-fpm.log
#配置error_log,设置error_log的存放位置
error_log = /var/log/fpm/php5-fpm.error.log

; syslog_facility is used to specify what type of program is logging the
; message. This lets syslogd specify that messages from different facilities
; will be handled differently.
; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON)
; Default Value: daemon
;syslog.facility = daemon

; syslog_ident is prepended to every message. If you have multiple FPM
; instances running on the same server, you can change the default value
; which must suit common needs.
; Default Value: php-fpm
;syslog.ident = php-fpm

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
#错误级别(生产环境下应该提高错误级别,推荐error)
log_level = notice

#表示在emergency_restart_interval所设时间内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过emergency_restart_threshold个,php-fpm就会优雅重启
emergency_restart_threshold = 60
emergency_restart_interval = 60s

; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
;process_control_timeout = 0

; The maximum number of processes FPM will fork. This has been design to control
; the global number of processes when using dynamic PM within a lot of pools.
; Use it with caution.
; Note: A value of 0 indicates no limit
; Default Value: 0
; process.max = 128

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
#后台执行fpm,设置为yes
daemonize = yes

pool.d目录下配置文件(例如:www.conf)

(1)保证配置文件中一个参数的开启,参数定义看英文注释很清楚:

; Redirect worker stdout and stderr into main error log. If not set, stdout and
; stderr will be redirected to /dev/null according to FastCGI specs.
; Note: on highloaded environement, this can cause some delay in the page
; process time (several ms).
; Default Value: no
catch_workers_output = yes



(2)同时可以开启access_log和slow_log

access.log = /var/log/fpm/php5-fpm.access.log

; The access log format.
; The following syntax is allowed
;  %%: the '%' character
;  %C: %CPU used by the request
;      it can accept the following format:
;      - %{user}C for user CPU only
;      - %{system}C for system CPU only
;      - %{total}C  for user + system CPU (default)
;  %d: time taken to serve the request
;      it can accept the following format:
;      - %{seconds}d (default)
;      - %{miliseconds}d
;      - %{mili}d
;      - %{microseconds}d
;      - %{micro}d
;  %e: an environment variable (same as $_ENV or $_SERVER)
;      it must be associated with embraces to specify the name of the env
;      variable. Some exemples:
;      - server specifics like: %{REQUEST_METHOD}e or %{SERVER_PROTOCOL}e
;      - HTTP headers like: %{HTTP_HOST}e or %{HTTP_USER_AGENT}e
;  %f: script filename
;  %l: content-length of the request (for POST request only)
;  %m: request method
;  %M: peak of memory allocated by PHP
;      it can accept the following format:
;      - %{bytes}M (default)
;      - %{kilobytes}M
;      - %{kilo}M
;      - %{megabytes}M
;      - %{mega}M
;  %n: pool name
;  %o: ouput header
;      it must be associated with embraces to specify the name of the header:
;      - %{Content-Type}o
;      - %{X-Powered-By}o
;      - %{Transfert-Encoding}o
;      - ....
;  %p: PID of the child that serviced the request
;  %P: PID of the parent of the child that serviced the request
;  %q: the query string 
;  %Q: the '?' character if query string exists
;  %r: the request URI (without the query string, see %q and %Q)
;  %R: remote IP address
;  %s: status (response code)
;  %t: server time the request was received
;      it can accept a strftime(3) format:
;      %d/%b/%Y:%H:%M:%S %z (default)
;  %T: time the log has been written (the request has finished)
;      it can accept a strftime(3) format:
;      %d/%b/%Y:%H:%M:%S %z (default)
;  %u: remote user
;
; Default: "%R - %u %t \"%m %r\" %s"
;access.format = %R - %u %t "%m %r%Q%q" %s %f %{mili}d %{kilo}M %C%%
 
; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
slowlog = /var/log/fpm/php5-fpm.log.slow
 
; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_slowlog_timeout = 3

fpm日志按天分割脚本

#!/bin/bash - 

#1.fpm日志存放路径
php5_fpm_path="/var/log/php5-fpm"

path_array=($php5_fpm_path)


#2.日志标识前缀数组
icontact_fpm_sign="test"
test_fpm_sign="php5-fpm"

prefix_array=($icontact_fpm_sign $test_fpm_sign)


#3.日志success or slow 标识后缀数组
bool_array=("access" "slow" "error")


#4.nginx日志切割备份后缀
postfix=`date +%Y%m%d`".log"


#5.备份当前的日志文件,重命名改为日期后缀
for path in ${path_array[*]}
do
	for prefix in ${prefix_array[*]}
	do
		for bool in ${bool_array[*]}
		do
			file=$path/$prefix.$bool.log
			backfile=$path/$prefix.$bool.$postfix
			if [ -e $file ]
			then
				mv $file 	$backfile
			fi
		done
	done
done


#6.查找nginx进程号,让其产生新的日志文件
fpm_pid=`ps -aux |grep -E 'php-fpm: master process'|grep -v 'grep'|awk '{print $2}'`
#USR1:Reopen log files,刷新nginx日志文件
kill -USR1 $fpm_pid


经验和bug分享

问题

为了减少线上服务器的错误信息输出,我将php-fpm.conf的Log_level = error,但是这样配置后我在php5-fpm的错误日志里再也看不到错误信息了。


原因定位

可以看一下我将Log_level = notice后的错误日志信息截图:




可以发现fpm将php的不管是warning或者是error的错误全部定位成了notice级别,所以线上想看fpm的错误日志只能将错误级别定位成notice了





  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我知道你的问题了。针对你的问题,我可以给你一些简单的部署步骤: 1.安装nginxPHP 在CentOS 6.8中安装nginxPHP的方式如下: ``` sudo yum install nginx sudo yum install php-fpm ``` 2.配置nginx 在配置nginx之前,你需要备份一下默认的nginx配置文件。备份命令如下: ``` sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak ``` 然后,你可以通过编辑/etc/nginx/nginx.conf文件来配置nginx。 下面是一个简单的nginx配置示例,可以将它添加到/etc/nginx/nginx.conf文件中: ``` user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } ``` 在这个示例配置中,我们将nginx的根目录设置为/usr/share/nginx/html,并且将index.php添加到默认文档列表中。同时,我们还配置了一个location块,用于处理PHP文件。 3.配置PHP 在配置PHP之前,你需要备份一下默认的php-fpm配置文件。备份命令如下: ``` sudo cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.bak ``` 然后,你可以通过编辑/etc/php-fpm.d/www.conf文件来配置PHP。 下面是一个简单的PHP配置示例,可以将它添加到/etc/php-fpm.d/www.conf文件中: ``` [www] user = nginx group = nginx listen = /var/run/php-fpm/php-fpm.sock listen.owner = nginx listen.group = nginx pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 ``` 在这个示例配置中,我们将PHP-FPM的用户和组设置为nginx,并将PHP-FPM的监听套接字设置为/var/run/php-fpm/php-fpm.sock。 4.重启服务 完成配置后,你需要重启nginxphp-fpm服务,以使配置生效。重启命令如下: ``` sudo systemctl restart nginx sudo systemctl restart php-fpm ``` 到这里,就完成了nginx+PHP的简单部署。同时,为了进一步提升性能,你可以使用Nginx+PHP+FastCGI加速模式。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值