背景
在linode上买了最低配的主机,将自己的个人博客站点搭建在上面,博客是用 WordPress 搭建的,对应的在服务器上部署了 Nginx + MySQL + php-fpm。
具体过程可参见之前的文章:
但最近一段时间站点总是莫名奇妙的离线,Jetpack 插件隔三差五给我发送监控邮件,告诉我我的站点没有响应了。之前几次在机器上看发现是 mariadb 数据库进程挂掉了,一直以为是我的数据库配置有问题,导致系统kill掉了它。所以每次都只是简单的重启了mariadb服务,也没有深究。后来将数据库的连接数调低,内存使用量也调低,但是还是会出现这个问题。才觉得这个不是数据库的问题,看下 top,发现php-fpm进程一大堆,内存基本都是被他们这些吃掉的,于是开始修改 php-fpm 的相关配置,修改后果然没有再出现之前的问题,top看起来负载也低了很多。
这里记录下这个过程。
参数优化
需要优化这样几个参数,pm,pm.max_children,pm.start_servers,pm.min_spare_servers,pm.max_spare_servers,pm.process_idle_timeout。
分别解释下这几个参数的含义:
- pm
Process Manager,进程管理器,不同的选项会使用不同的策略管理子进程数量。可选的值有三个:
- static
静态方式管理,这种模式下,只有 pm.max_children 会起作用,其他参数会被忽略。
-
dynmic
动态方式管理,这种模式下,pm.max_children,pm.start_servers,pm.min_spare_servers ,pm.max_spare_servers这四个参数共同起作用,来控制决定子进程。
-
ondemand
按需启动,这个模式下, pm.max_children,pm.process_idle_timeout 这两个参数共同起作用。
- static
-
pm.max_children
这个参数指定了当pm设置为“static”时要创建的子进程数,以及pm设置为“dynamic”或“ondemand”时的最大子进程数。
-
pm.start_servers
该参数指定了主进程启动时创建的子进程数。这个参数的默认值是 min_spare_servers + (max_spare_servers – min_spare_servers) / 2。
-
pm.min_spare_servers
该参数指定了最小的空闲进程数量。如果“空闲”进程的数量小于此数量,则将创建一些子进程。
-
pm.max_spare_servers
该参数指定了最大的空闲进程数量。如果空闲进程的数量大于此数量,则会 kill 一些子进程。
-
pm.process_idle_timeout
该参数指定了空闲进程可存活的最大秒数。
static or dynamic ?
我的linode 主机配置比较低,1CPU,1RAM, 25G存储。小内存的服务器适合 dynamic 模式,动态模式下灵活分配进程,省内存。而 static 模式适合内存比较大的主机,因为动态的创建回收对服务器资源也是一种消耗。
一般设置时,可按一个 php-fpm 进程 20M 算,进程数量还是根据自己主机的内存进行设置。
至于 ondemand 这个模式,没有研究过具体的使用场景。有时间研究下再来补充。
附上www.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
; 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