启动php-fpm进程数有30多个,内存吃满了,整个服务器16G的内存都不够用
#查看已经有多少个php-fpm进程用来处理tcp请求
netstat -anp|grep "php-fpm"|grep "tcp"|grep "pool"|wc -l
调整php-fpm.conf配置文件(8C 16G 内存的服务器)
pm = static #无论什么情况,都会保持一个固定的PHP进程数量
pm.max_children = 20
重启php-fpm服务,即可生效
systemctl restart php-fpm
php-fpm.conf参数说明:
pm = dynamic #控制子进程的方式,选项有static和dynamic
pm.max_children:#静态方式下开启的php-fpm进程数量
pm.start_servers:#动态方式下的起始php-fpm进程数量
pm.min_spare_servers:#动态方式下的最小php-fpm进程数量
pm.max_spare_servers:#动态方式下的最大php-fpm进程数量
pm.max_requests:#php-fpm子进程能处理的最大请求数,一个php-fpm子进程执行多少次之后重启该进程
php_admin_value[memory_limit] = 64M #根据服务器配置随便改:32M,64M,128M,256M,512M
- 如果pm设置为 static,那么其实只有pm.max_children这个参数生效。系统会开启设置数量的php-fpm进程。
- 如果pm设置为 dynamic,那么pm.max_children参数失效,后面3个参数生效。系统会在php-fpm运行开始 的时候启动pm.start_servers个php-fpm进程,然后根据系统的需求动态在pm.min_spare_servers和pm.max_spare_servers之间调整php-fpm进程数
- 如果pm设置为 ondemand,那么系统按照需求创建和销毁进程,可以让系统决定何时启动新的进程
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. With this process management, there will be
; always at least 1 children.
; 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.
; ondemand - no children are created at startup. Children will be forked when
; new requests will connect. The following parameter are used:
; pm.max_children - the maximum number of children that
; can be alive at the same time.
; pm.process_idle_timeout - The number of seconds after which
; an idle process will be killed.
; Note: This value is mandatory.
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 8
比如说512M的VPS,加入分配给php-fpm最大250M,建议pm.max_spare_servers设置为250/30 ,约为8。至于pm.min_spare_servers,则建议根据服务器的负载情况来设置,如果服务器上只部署php环境的话,比较合适的值在2~5之间
pm.start_servers缺省值计算公式:
pm.start_servers=min_spare_servers + (max_spare_servers - min_spare_servers) / 2
启动固定大小数量的worker,也有1s的定时器,用于统计进程的一些状态信息,例如空闲worker个数,活动worker个数
pm = static
pm.max_children = 10 #必须配置这个参数
优点:不用动态判断负载,提升性能
缺点:如果配置成static,只需要考虑max_children数量,数量取决于cpu的个数和应用的响应时间,一次启动固定大小进程浪费系统资源
静态进程个数:M/(m*1.2)(pm.max_requests, 设置最大请求数,达到这个数量以后,会自动长期worker进程,繁殖内存意外增长)
对于内存稍微小点的服务器:指定静态的进程数量更加有利于服务器的稳定。这样可以保证php-fpm只获取够用的内存,将不多的内存分配给其他应用去使用,会使系统的运行更加畅通。
# 全局设置
process.max = 10 #最大进程数
# www.conf
user = nobody # 进程发起的用户和用户组,nobody为任意用户,user必须设置,group不用
group = www
listen = [::]:9000
catch_workers_output = yes # worker进程的标准输出和错误输出会重定向到错误日志中,没有设置会输出到/dev/null
;slowlog = xxx # 默认关闭,慢日志路径
;request_slowlog_timeout = 0 # 脚本执行超过多久就记录到日志文件