PS:整理一下Nginx使用的php-fpm的两种进程管理方式及优化!
-
例:php5.3.8 php-fpm.conf 进程管理配置信息:
# vim /usr/local/php/etc/php-fpm.conf ; Choose how the process manager will control the number of child processes. ; Possible Values: ; static - a fixed number (pm.max_children) of child processes; ; dynamic - the number of child processes are set dynamically based on the ; following directives: ; pm.max_children - the maximum number of children that can ; be alive at the same time. ; pm.start_servers - the number of children created on startup. ; pm.min_spare_servers - the minimum number of children in 'idle' ; state (waiting to process). If the number ; of 'idle' processes is less than this ; number then some children will be created. ; pm.max_spare_servers - the maximum number of children in 'idle' ; state (waiting to process). If the number ; of 'idle' processes is greater than this ; number then some children will be killed. ; Note: This value is mandatory. pm = dynamic
由上面一段文字可知,对于进程的管理存在两种风格——static和dynamic
如果设置成static,进程数自始至终都是pm.max_children指定的数量,pm.start_servers,pm.min_spare_servers,pm.max_spare_servers配置将没有作用。
如果设置成dynamic,则进程数是动态的,最开始是pm.start_servers指定的数量,如果请求较多,则会自动增加,但不超过pm.max_children指定的数量,同时保证空闲的进程数不小于pm.min_spare_servers,如果进程数较多,也会进行相应清理,保证多余的进程数不多于pm.max_spare_servers
这两种不同的进程管理方式,可以根据服务器的实际需求来进行调整。 -
参数解释:
; The number of child processes to be created when pm is set to 'static' and the ; maximum number of child processes to be created when pm is set to 'dynamic'. ; 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. ; Note: Used when pm is set to either 'static' or 'dynamic' ; Note: This value is mandatory. pm.max_children = 50 ; 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 = 20 ; 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 = 5 ; The desired maximum number of idle server processes. ; Note: Used only when pm is set to 'dynamic' ; Note: Mandatory when pm is set to 'dynamic' pm.max_spare_servers = 35
风格:static 此时查看进程数固定为13个
#假设设置的参数 pm = static pm.max_children=13 [root@localhost]# ps -axu | grep php-fpm root 4423 0.0 0.4 295680 4288 ? Ss 13:27 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) www 4424 0.0 0.4 295680 3984 ? S 13:27 0:00 php-fpm: pool www www 4425 0.0 0.4 295680 3984 ? S 13:27 0:00 php-fpm: pool www www 4426 0.0 0.4 295680 3984 ? S 13:27 0:00 php-fpm: pool www www 4427 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4428 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4429 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4430 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4431 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4432 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4433 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4434 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4435 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4436 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www
dynamic风格下必须遵守两个法则
1.pm.min_spare_servers<=pm.start_servers<=pm.max_spare_servers
2.pm.max_children>=pm.max_spare_servers#风格:dynamic pm = dynamic pm.max_children=20 pm.start_servers=5 pm.min_spare_servers=5 pm.max_spare_servers=20 [root@localhost]# ps -axu | grep php-fpm root 4423 0.0 0.4 295680 4288 ? Ss 13:27 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) www 4424 0.0 0.4 295680 3984 ? S 13:27 0:00 php-fpm: pool www www 4425 0.0 0.4 295680 3984 ? S 13:27 0:00 php-fpm: pool www www 4426 0.0 0.4 295680 3984 ? S 13:27 0:00 php-fpm: pool www www 4427 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www www 4428 0.0 0.4 295680 3988 ? S 13:27 0:00 php-fpm: pool www
-
进程参数设定值方式及优化
那么,对于我们的服务器,选择哪种执行方式比较好呢?事实上,跟Apache一样,运行的PHP程序在执行完成后,或多或少会有内存泄露的问题。这也是为什么开始的时候一个php-fpm进程只占用3M左右内存,运行一段时间后就会上升到20-30M的原因了。
对于内存大的服务器(比如8G以上)来说,指定静态的max_children实际上更为妥当,因为这样不需要进行额外的进程数目控制,会提高效率。因为频繁开关php-fpm进程也会有时滞,所以内存够大的情况下开静态效果会更好。数量也可以根据 内存/30M 得到,比如8GB内存可以设置为200,那么php-fpm耗费的内存就能控制在 4G-6G的样子。如果内存稍微小点,比如1G,那么指定静态的进程数量更加有利于服务器的稳定。这样可以保证php-fpm只获取够用的内存,将不多的内存分配给其他应用去使用,会使系统的运行更加畅通。
对于小内存的服务器来说,比如256M内存的VPS,即使按照一个20M的内存量来算,10个php-cgi进程就将耗掉200M内存,那系统的崩溃就应该很正常了。因此应该尽量地控制php-fpm进程的数量,大体明确其他应用占用的内存后,给它指定一个静态的小数量,会让系统更加平稳一些。或者使用动态方式,因为动态方式会结束掉多余的进程,可以回收释放一些内存,所以推荐在内存较少的服务器或VPS上使用。具体最大数量根据 内存/20M 得到。比如说512M的VPS,建议pm.max_spare_servers设置为20。至于pm.min_spare_servers,则建议根据服务器的负载情况来设置,比较合适的值在5~10之间。