php-fpm.conf中pm.max_children多大合适,报错you may need to increase pm.start_servers, or pm.min/max_spare_se

php-fpm.conf中pm.max_children多大合适,报错you may need to increase pm.start_servers, or pm.min/max_spare_se

大家好,又见面了,我是全栈君。

web使用的是nginx+php的架构,网站上线还没多久,所以优化方面基本只是做了些初始的配置。
查看php-fpm.log发现有警告,这些警告和网站的挂了个时间基本吻合。我就从这里开始入手。
先开始也是找了些文档,但是第二天还是出现问题。后来查看配置文件并翻译了下(百度),英文底子不好。pm模块类似apache的模块,是分静态和动态的。
 
网上说的很多调整都是基于动态居多,但是并没说么定义这个模块。所以大家用动态和静态还是要仔细看看配置文件

pm = static 
;此处静态
 
; 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 = 300
 
 
 
; 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
 
 
 
; 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 = 20
 
 
 
; 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 = 500
 
 
 
; 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 = 10240

 红色字段就是定义方式的,定义好这个再去根据服务器情况设置参数
 
假如使用静态 pm.max_children这个参数会起作用,其余不会。动态反之。
 
2G内存pm.max_children大概开启50左右,按照实际情况来调优,这个是很必要的。

pm.max_children = 50 --允许创建的最大子进程数
pm.start_servers = 15 --起始进程数(服务启动后初始进程数量)
pm.min_spare_servers = 15 --最小空闲进程数(清理空闲进程后的保留数量)
pm.max_spare_servers = 35 --最大空闲进程数(当空闲进程达到此值时清理)

我的php-fpm.conf设置如下:

[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice

[www]
listen = /tmp/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 200
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 80
request_terminate_timeout = 1200
request_slowlog_timeout = 0
slowlog = var/log/slow.log

php-fpm.log警告内容如下:

[07-Sep-2020 16:11:45] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 14 idle, and 178 total children
[07-Sep-2020 16:12: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 19 idle, and 191 total children
[07-Sep-2020 16:12:38] WARNING: [pool www] server reached pm.max_children setting (200), consider raising it
[07-Sep-2020 19:50:37] NOTICE: Terminating ...
[07-Sep-2020 19:51:48] NOTICE: fpm is running, pid 1271
[07-Sep-2020 19:51:48] NOTICE: ready to handle connections
[18-Sep-2020 17:38:01] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 14 idle, and 69 total children
[18-Sep-2020 17:38:02] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 14 idle, and 75 total children
[18-Sep-2020 17:38:16] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 9 idle, and 90 total children
[18-Sep-2020 17:38:17] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 8 idle, and 98 total children
[18-Sep-2020 17:38:22] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 5 idle, and 117 total children
[18-Sep-2020 17:38:23] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 18 idle, and 125 total children
[18-Sep-2020 17:38:24] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 15 idle, and 127 total children
[18-Sep-2020 17:38:25] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 19 idle, and 132 total children
[18-Sep-2020 17:38:26] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 8 idle, and 133 total children
[18-Sep-2020 17:38:27] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 6 idle, and 145 total children
[18-Sep-2020 17:38:34] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 15 idle, and 166 total children
[18-Sep-2020 17:38:35] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 17 idle, and 171 total children
[18-Sep-2020 17:38:45] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 15 idle, and 181 total children
[18-Sep-2020 17:38:46] 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 186 total children
[18-Sep-2020 17:38:47] WARNING: [pool www] server reached pm.max_children setting (200), consider raising it


[my_app]
;FastCGI/PHP-FPM使用UNIX Sockets
listen = /data/my_app/tmp/php.sock
listen.backlog = 300
user = www
group = www
pm = dynamic
;估算pm.max_children = (MAX_MEMORY - 500MB) / 20MB 
pm.max_children = 100
;推荐为最大的pm.max_children的%10
pm.start_servers = 10    ; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 1000
pm.status_path = /php_status

request_terminate_timeout = 0
request_slowlog_timeout = 0
slowlog = /data/my_app/logs/slow.log

补充:查看PHP-FPM内存占用的几个有用小命令,记录如下:

1.查看每个FPM的内存占用:

ps -ylC php-fpm --sort:rss
ps -ylC php-fpm --sort:rss|wc -l

当然,在后后面加 | wc -l可查看系统当前FPM总进程数,我的目前在8个左右。

php-fpm.conf中pm.max_children多大合适,报错you may need to increase pm.start_servers, or pm.min/max_spare_se

PHP官方的建议设置值:可以用于web服务器的内存量 / 最大子进程数

pm.max_children = Total RAM dedicated to the web server / Max child process size

2.查看FPM在你的机子上的平均内存占用:

ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

3. php-fpm进程数设置多少合适

在我们做php高并发下的优化的时候,涉及到一个问题,就是fpm进程数设置多少合适,在有的文章中推荐设置为n或者n×2(n为cpu数量)
有的文档中推荐按照可用内存/30m (30m为fpm每个进程内存)
两种说法其实都没有问题,但是都忽略了一个基本的事实就是具体情况具体分析

基础知识

本段摘自 https://yq.aliyun.com/articles/33422

需要了解的参数是:pm、pm.max_children、pm.start_servers、pm.min_spare_servers、pm.max_spare_servers。
pm:表示使用那种方式,有两个值可以选择,就是static(静态)或者dynamic(动态)。
在更老一些的版本中,dynamic被称作apache-like。这个要注意看配置文件的说明。
下面5个参数的意思分别为:

pm = dynamic 如何控制子进程,选项有static和dynamic    
pm.max_children:静态方式下开启的php-fpm进程数量    
pm.max_requests:php-fpm子进程能处理的最大请求数    
pm.start_servers:动态方式下的起始php-fpm进程数量
pm.min_spare_servers:动态方式下的最小php-fpm进程数
pm.max_spare_servers:动态方式下的最大php-fpm进程数量

区别:
如果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进程数。

在高并发场景(我们公司是一万并发测试)中,一般都是采用的static方式,因为一般来说我们的服务器都是有严格的业务分离,fpm服务器就是用来作为fpm服务器使用,除去还有一个nginx之外,不会去考虑有其他的服务器占用资源。

具体问题具体分析

在php的运行中,无非是两种场景

  1. 大运算
  2. 高io

1 大运算的场景,即 php程序需要用大量的cpu资源来进行数据计算之类的操作,在这种场景下,fpm进程可以设置为cpu数量的一倍或者两倍
2 高io场景,php的使用场景中(最起码是本电商场景中)基本上属于高io,因为程序花了大量的时间在等待redis返回等待数据库返回。高io场景下,因为cpu大多处在wa状态下,所以可以尽量的加大fpm进程数,所以这个时候使用内存/30m是更为合理的

经过我们自己真实压测,大量redis和mysql读写的io密集情况下,16G的内存,fpm我们设置为400个的时候qps比fpm 16个 32个要好不少

参考:https://zhuanlan.zhihu.com/p/94627701

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/111679.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 计算机组成原理 时钟周期_什么是指令周期机器周期和时钟周期

    计算机组成原理 时钟周期_什么是指令周期机器周期和时钟周期时钟周期:一个时钟脉冲所需要的时间。在计算机组成原理中又叫T周期或节拍脉冲。是CPU和其他单片机的基本时间单位。它可以表示为时钟晶振频率(1秒钟的时钟脉冲数)的倒数(也就是1s/时钟脉冲数,比如1/12MHz),对CPU来说,在一个时钟周期内,CPU仅完成一个最基本的动作。时钟脉冲是计算机的基本工作脉冲,控制着计算机的工作节奏。时钟频率越高,时钟周期就越短,工作速度也就越快。时钟周期在CPU的描述…

    2022年10月13日
    0
  • 有效管理自己知识,多总结和分享——2018七月份的尾巴

    知道却做不到,等于不知道! –笔记侠 知道却讲不出来,也等于不知道! –dufy知道 ≠ 能做到 | 知道 ≠ 能讲出 ?昨天听了一个音频,里面说:“知道很多道理 却依然过不好这一生”!知道了这么多道理,能真正做到有多少呢?知道了这么多道理,真正用自己的理解或者自己的话能表达的道理又有多少呢?我在反思自己,希望与你共勉!分享:好的学习方法 — 费曼技巧…

    2022年2月27日
    40
  • golang2021激活码【永久激活】[通俗易懂]

    (golang2021激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    764
  • Sphinx + Coreseek 实现中文分词搜索

    Sphinx + Coreseek 实现中文分词搜索

    2022年3月6日
    37
  • Pycharm改成中文模式

    Pycharm改成中文模式Pycharm作为一款IDE,经常作为python编译器。很多人在用pycharm时都是英文格式,现在,本文推荐一种可以将其改成中文模式的方法:1.首先,打开pychram中的“File”,找到“Setting”;2.在“Plugins”中找到汉化插件并安装,这样就可以设置成中文。3.如果想改回英文,可以在“已安装”中取消应用插件即可。…

    2022年8月29日
    4
  • java 事务嵌套_Java事务以及嵌套事务[通俗易懂]

    java 事务嵌套_Java事务以及嵌套事务[通俗易懂]最近遇到事务的处理,嵌套事务,自己研究,整理一下。1先看结论1、在Java事务中,事务的嵌套,如果有事务成功,那么则都成功,否则都不会成功。2、如果事务中存在异常,只要对异常进行捕获和处理,都为执行成功,否则都不会执行成功。2Propagation取值REQUIRED(默认值):在有transaction状态下执行;如当前没有transaction,则创建新的transaction;SUPPO…

    2022年10月10日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号