Linux平台PHP5.4设置FPM线程数量的方法
本示例介绍在Linux环境下针对PHP 5.4版本配置FastCGI进程管理器(FPM)线程数量的具体操作。内容旨在为开发者提供实用的参考方案。
完成PHP 5.4的安装程序后,默认的FPM配置文件路径通常设定为/usr/local/php/etc/php-fpm.conf.default。
>cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
>vim /usr/local/php/etc/php-fpm.conf
打开配置文件后,输入“/www”进行全文搜索,定位到包含www字样的POOL配置区块。
pm = dynamic
; 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 = 5
; 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 = 2
; 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 = 1
; 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 = 3
; The number of seconds after which an idle process will be killed.
; Note: Used only when pm is set to 'ondemand'
; Default Value: 10s
;pm.process_idle_timeout = 10s;
; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
;pm.max_requests = 500
以下是对关键参数的详细解读:
pm参数用于定义子进程的管理策略,可选值为static或dynamic,系统默认采用dynamic模式。若选定static模式,则必须通过pm.max_children参数指定固定的子进程总数。
当选择dynamic模式时,子进程的数量由后续的一组动态参数共同决定:
| pm.max_children | 子进程最大数 |
| pm.start_servers | 启动时的进程数 |
| pm.min_spare_servers | 保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程 |
| pm.max_spare_servers | 保证空闲进程数最大值,如果空闲进程大于此值,则进行清理。对于专用服务器,pm可以设置为static。 |
| pm.max_requests | 设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 '0′ 则一直接受请求. 设置为500就可以了(默认0)。 |
将相关数值调整为如下配置:
pm.max_children = 32
pm.start_servers = 16
pm.min_spare_servers = 8
pm.max_spare_servers = 32
pm.max_requests = 500
执行:wq命令保存更改并退出VIM编辑器。
>/usr/local/php/sbin/php-fpm -t
NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
使用命令检测配置文件语法是否正确,确认无误后,终止当前正在运行的FPM进程。
>/usr/local/php/sbin/php-fpm
重新启动FPM服务。
期望上述说明能对从事PHP程序设计的读者产生实际帮助。