php-fpm导致服务器内存高

一、问题描述

启动php-fpm进程数有30多个,内存吃满了,整个服务器16G的内存都不够用

#查看已经有多少个php-fpm进程用来处理tcp请求
netstat -anp|grep "php-fpm"|grep "tcp"|grep "pool"|wc -l

二、php-fpm优化

调整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三种工作模式说明

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.
1、dynamic动态模式(小内存服务器使用)
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 8
  • 会初始化创建一部分worker,在运行过程中,动态调整worker数量,最大worker数受pm.max_children和process.max限制。
    当空闲进程数小于min_spare_servers时,创建新的子进程,总子进程数小于等于pm.max_children,小于等于process.max
    当空闲进程数大于max_spare_servers,会杀死启动时间最长的子进程
    如果子进程(idle状态)数大于max_children,会打印warning日志,结束处理
    process小于max_children ,计算一个num,启动num个worker
  • 优点:动态扩容,不浪费系统资源
  • 缺点:所有worker都在工作,新的请求到来需要等待创建worker进程,最长等待1s(内部存在一个1s的定时器,去查看,创建进程),频繁启停进程消耗cpu,请求数稳定,不需要频繁销毁
  • 动态建立进程个数:N+20% 到 M/m之间(N是cpu核数,M是内存,m是每个php进程内存数)
  • 注意:进程数不是越多越好,进程数增多会增加进程管理开销以及进程上下文切换,并发执行数也不会超过cpu核数,cpu密集型,pm.max_children不能超过cpu内核数,如果不是可以将pm.max_children的值设置大于cpu核数

比如说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
2、static静态模式(大内存服务器使用,如8G以上)

启动固定大小数量的worker,也有1s的定时器,用于统计进程的一些状态信息,例如空闲worker个数,活动worker个数

pm = static  
pm.max_children = 10 #必须配置这个参数
  • 优点:不用动态判断负载,提升性能

  • 缺点:如果配置成static,只需要考虑max_children数量,数量取决于cpu的个数和应用的响应时间,一次启动固定大小进程浪费系统资源

  • 静态进程个数:M/(m*1.2)(pm.max_requests, 设置最大请求数,达到这个数量以后,会自动长期worker进程,繁殖内存意外增长)

  • 对于内存稍微小点的服务器:指定静态的进程数量更加有利于服务器的稳定。这样可以保证php-fpm只获取够用的内存,将不多的内存分配给其他应用去使用,会使系统的运行更加畅通。

3、 ondemand工作模式(适合微小的内存,如2G以下,不推荐使用此模式):
  • php-fpm启动的时候不会启动worker进程,按需启动worker,有链接进来后,才会启动
  • 连接到来时(只有链接,不没有数据也会创建,telnet也会创建),创建新worker进程,worker进程数的创建收max_children设置限制,也受限于全局的process.max设置(三种模式都受限此,下文中有全局配置项讲解),如果空闲时间超过了process_idle_timeout的设置就会销毁worker进程
  • 优点:按流量需求创建,不浪费系统资源,
  • 缺点:因为php-fpm是短连接的,如果每次请求都先建立连接,大流量场景下会使得master进程变得繁忙,浪费cpu,不适合大流量模式

其他参数说明

# 全局设置
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   # 脚本执行超过多久就记录到日志文件

猜你喜欢

转载自blog.csdn.net/change_can/article/details/114526165