Home>Article>Backend Development> About process number management of php-fpm
This article mainly introduces the process number management of php-fpm, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
PHP-FPM
Let’s first understand some noun concepts:
CGI
isCommon Gateway Interface (Common Network Management Protocol)
, used to allow interactive programs and Web The protocol for server communication. It is responsible for processing URL requests, starting a process, taking the data sent by the client as input, and the web server collects the output of the program and adds appropriate headers, and then sends it back to the client.
FastCGI
is an enhanced version of the protocol based onCGI
. Instead of creating a new process to serve requests, it uses persistent processes and created child processes to handle a series of Processes, these processes are managed by the FastCGI server, with less overhead and higher efficiency.
PHP-FPM
is an implementation ofPHP
FastCGI Process Manager (FastCGI Process Manager)
, used to replacePHP FastCGI
Most of the additional features are suitable for high-load websites. Supported functions such as:
Advanced process management function for smooth stop/start
Slow logging script
Dynamic/static sub-process generation
Configuration file based on php.ini
PHP-FPM
It has been integrated into the PHP source code after 5.4, providing a better PHP process management method, which can effectively control memory and processes, and smoothly reload PHP configuration. If you need to use it, just bring the-enable-fpm
parameter when./configure
, and usePHP-FPM
to controlFastCGI
Process:
// 支持start/stop/quit/restart/reload/logrotate参数 // quit/reload是平滑终止和平滑重新加载,即等现有的服务完成 ./php-fpm --start
PHP-FPM
ConfigurationPHP-FPM
The configuration file isphp-fpm.conf
, In this configuration file we need to know some parameters. All the sub-processes below refer to thephp-fpm
process, which can be viewed on the terminal viaps aux | grep php
.
Displayphp-fpm: pool www
represents the work sub-process (actually processing the request)
Displayphp-fpm: process master
represents the master main process (responsible for managing the work sub-process)
Look firstPHP-FPM
The most important global configuration part:
emergency_restart_threshold
If the number of parameter settings is received within theemergency_restart_interval
set timeSIGSEGV
orSIGBUS
exit signal,FPM
will restart. The default value is 0, which means turning off this function.
emergency_restart_interval
Set the smooth restart interval to help solve the problem of shared memory usage in the accelerator. Available units ares (default)/m/h/d
, and the default value is 0, which means off.
process.max
FPM
The maximum number of child processes that can be created, it is using multiplepm = dynamic
When configuring thephp-fpm pool
process pool, control the global number of child processes. The default value is 0, which means no limit.
The rest of the configuration of PHP-FPM
is an area namedPool Definitions
. The configuration settings in this area are eachPHP-FPM
Process pool, the process pool is a series of related sub-processes. This part starts with[process pool name]
, such as[www]
.
It can be explained thatps aux | grep php
showsphp-fpm: pool www
.
#pm
##pmrefers to
process manager, which specifies how the process manager controls the number of child processes. It is required and supports 3 values:
static: Use a fixed number of child processes, specified by
pm.max_children
dynamic: Dynamically adjust the number of child processes based on the following parameters. There must be at least one child process
pm.max_chidren: The maximum number of child processes that can survive at the same time
pm.start_servers: The number of child processes created at startup, default The value is
min_spare_servers max_spare_servers - min_spare_servers) / 2
: The minimum number of idle child processes, if not enough, new The child processes will be automatically created
: The maximum number of idle child processes. If exceeded, some child processes will be killed
: The child process will not be created at startup and will only be created when a new request arrives. The following two parameters will be used:
t The idle timeout of the child process. If no new requests can be served after the timeout, it will be killed
pm.max_requests
每一个子进程的最大请求服务数量,如果超过了这个值,该子进程会被自动重启。在解决第三方库的内存泄漏问题时,这个参数会很有用。默认值为0,指子进程可以持续不断的服务请求。
PHP-FPM
配置优化PHP-FPM
管理的方式是一个master
主进程,多个pool
进程池,多个worker
子进程。其中每个进程池监听一个socket
套接字。具体的图示:
其中的worker
子进程实际处理连接请求,master
主进程负责管理子进程:
1. `master`进程,设置1s定时器,通过`socket`文件监听 2. 在`pm=dynamic`时,如果`idle worker`数量`pm.max_spare_servers`,杀死多余的空闲子进程 4. 在`pm=ondemand`时,如果`idle worker`空闲时间>`pm.process_idle_timeout`,杀死该空闲进程 5. 当连接到达时,检测如果`worker`数量>`pm.max_children`,打印`warning`日志,退出;如果无异常,使用`idle worker`服务,或者新建`worker`服务
我们为了避免PHP-FPM
主进程由于某些糟糕的PHP代码挂掉,需要设置重启的全局配置:
; 如果在1min内有10个子进程被中断失效,重启主进程 emergency_restart_threshold = 10 emergency_restart_interval = 1m
每一个子进程同时只能服务一次连接,所以控制同时存在多少个进程数就很重要,如果过少会导致很多不必要的重建和销毁的开销,如果过多又会占用过多的内存,影响其他服务使用。
我们应该测试自己的PHP进程使用多少内存,一般来说刚启动时是8M左右,运行一段时间由于内存泄漏和缓存会上涨到30M左右,所以你需要根据自己的预期内存大小设定进程的数量。同时根据进程池的数量来看一个进程管理器的子进程数量限制。
$ps auxf | grep php | grep -v grep work 26829 0.0 0.0 715976 4712 ? Ss Jul11 0:00 php-fpm: master process (./etc/php-fpm.conf) work 21889 0.0 0.0 729076 29668 ? S 03:12 0:20 \_ php-fpm: pool www work 21273 0.0 0.0 728928 31380 ? S 03:25 0:21 \_ php-fpm: pool www work 15114 0.0 0.0 728052 29084 ? S 03:40 0:19 \_ php-fpm: pool www work 17072 0.0 0.0 728800 34240 ? S 03:54 0:22 \_ php-fpm: pool www work 22763 0.0 0.0 727904 20352 ? S 11:29 0:04 \_ php-fpm: pool www work 38545 0.0 0.0 727796 19484 ? S 12:34 0:01 \_ php-fpm: pool www // 共占用的内存数量 $ps auxf | grep php | grep -v grep | grep -v master | awk '{sum+=$6} END {print sum}' 162712 // 所有的子进程数量 $ ps auxf | grep php | grep -v grep | grep -v master | wc -l 6
可以看到第6列,每一个子进程的内存占用大概在19-34M之间(单位为KB)。平均的内存占用为162712KB/6 = 27.1M
。
$ free -g total used free shared buffers cached Mem: 157 141 15 0 4 123 -/+ buffers/cache: 13 143 Swap: 0 0 0
可以看出我的服务器总得内存大小是157G(-g采用了G的单位)。
此时如果我们分配全部的内存给PHP-FPM
使用,那么进程数可以限制在157000/27 = 5814
,但是由于我的服务器同时服务了很多内容,所以我们可以向下调整到512个进程数:
process.max = 512 pm = dynamic pm.max_children = 512 pm.start_servers = 16 pm.min_spare_servers = 8 pm.max_spare_serveres = 30
由于糟糕的插件和库,内存泄漏时有发生,所以我们需要对每一个子进程服务的请求数量做限制,防止无限制的内存泄漏:
pm.max_requests = 1000
如果上面的配置都按照你的实际需求和环境配置好了,不要忘记重启PHP-FPM
服务。
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
The above is the detailed content of About process number management of php-fpm. For more information, please follow other related articles on the PHP Chinese website!