php-fpm 配置

当 HTTP 请求返回 502 错误时,其中一个原因可能是服务器的配置出现了问题。

nginx_502
图中这个错误信息是指 nginx 出现了错误。通过查看日志信息可以定位到参数设置有问题。

php-fpm 配置

php-fpm 有自己的配置文件 php-fpm.conf ,一般位于 /data/app/php/etc 路径下的。下面是配置文件片段信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes when pm is set to 'dynamic' or 'ondemand'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI. The below defaults are based on a server without much resources. Don't
; forget to tweak pm.* to fit your needs.
; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
;pm.max_children = 200
pm.max_children = 2000
; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
;pm.start_servers = 50
pm.start_servers = 400
; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
;pm.min_spare_servers = 10
pm.min_spare_servers = 20

它也有日志文件 php-fpm.log ,一般位于 /data/log/php 中。下面是日志文件片段。

1
2
3
4
5
6
[14-Jun-2016 14:43:02] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 57 total children
[14-Jun-2016 14:43:03] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 0 idle, and 65 total children
[14-Jun-2016 14:43:04] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 1 idle, and 75 total children
[14-Jun-2016 14:43:05] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 84 total children
[14-Jun-2016 14:43:06] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 94 total children
[14-Jun-2016 14:43:07] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 104 total children

日志中的WARNING信息表明需要对配置文件中的一些参数进行调整。

参数调整策略

参数可以设置,但它不是随便修改的。

1
2
3
[01-Aug-2016 11:33:46] ALERT: [pool www] pm.min_spare_servers(20) and pm.max_spare_servers(2000) cannot be greater than pm.max_children(1500)
[01-Aug-2016 11:33:46] ERROR: failed to post process the configuration
[01-Aug-2016 11:33:46] ERROR: FPM initialization failed

这段错误提示信息说 pm.min_spare_serverspm.max_spare_servers 两个参数不能比 pm.max_children 大。否则, php-fpm 将无法启动。

php-fpm 启动与重启

修改好配置文件后,需要对 php-fpm 进行重启,重新加载配置文件。可以使用以下命令

1
sudo kill USR2 `cat php-fpm.pid`

重启。
如果配置文件有问题(如前文举例出的一个小问题),导致重启失败。那么,可以使用以下命令

1
sudo ./php-fpm

来启动 php-fpm
php-fpm.pid 一般位于 /var/run 路径下。php-fpm 一般位于 /usr/local/bin 路径下。
命令执行完后,可以通过 ps 命令来查看 php-fpm 是否在运行。

题外话

不知道微博出现 502 错误是哪里有问题。
微博 502

参考

  1. php-fpm参数优化
  2. php-fpm 启动参数及重要配置详解