This article mainly introduced about the PHP-FPM process number management, has a certain reference value, now share to everyone, the need for friends can refer to
PHP-FPM
Let's start by understanding some noun concepts:
CGI
is the Common Gateway Interface(通用网管协议)
protocol used to communicate with the interactive program and Web server. It handles requests for URLs, initiates a process, takes the data sent by the client as input, collects the output of the program by the Web server, adds the appropriate headers, and then sends it back to the client.
FastCGI
is based on CGI
an enhanced version of the protocol, unlike creating new processes to service requests, using persistent processes and child processes created to process a series of processes that are managed by the FASTCGI server and are less expensive and more efficient.
PHP-FPM
is PHP
implemented FastCGI Process Manager(FastCGI进程管理器)
, and most of PHP FastCGI
the additional features for replacement are available for high-payload sites. Supported features such as:
Smooth stop/Start advanced process management features
Slow log Record script
Dynamic/static child process generation
PHP.ini-based configuration files
PHP-FPM
After 5.4 has been integrated into the PHP source code, to provide a better way to manage the PHP process, can effectively control memory and process, smooth overloaded PHP configuration. If you need to use, ./configure
take the parameters at the time -enable-fpm
, use PHP-FPM
to control the FastCGI
process:
Support for Start/stop/quit/restart/reload/logrotate parameters//quit/reload is smooth termination and smooth reload, that is, waiting for existing services to complete./PHP-FPM--start
PHP-FPM
Configuration
PHP-FPM
The configuration file is php-fpm.conf
, in this configuration file we need to understand some parameters. All of the following sub-processes refer to the php-fpm
process, which can be seen through the terminal ps aux | grep php
.
Global configuration
Let's look at the PHP-FPM
most important global configuration section:
emergency_restart_threshold
If the emergency_restart_interval
number of times SIGSEGV
or exits of the parameter is received within the set time, the signal is restarted SIGBUS
FPM
. The default value is 0, which means that the feature is turned off.
emergency_restart_interval
Setting the interval between smooth restarts helps to solve the problem of shared memory usage in the accelerator. Available units s(默认)/m/h/d
, the default value is 0, which means close.
process.max
FPM
The maximum number of child processes that can be created, which pm = dynamic
controls the php-fpm pool
global number of child processes when using multiple configured process pools. The default value is 0, which means no limit.
Process Pool Configuration
PHP-FPM
The rest of the configuration is a Pool Definitions
zone named, the configuration of this zone is set per PHP-FPM
process pool, and the process pool is a series of related child processes. This part begins [进程池名称]
with, for example [www]
.
At this point you can explain ps aux | grep php
what is shown in the See php-fpm: pool www
.
pm
pm
Refers to process manager
specifying how the Process manager controls the number of child processes that are required and supports 3 values:
static
: Use a fixed number of child processes, pm.max_children
specified by the
dynamic
: Adjusts the number of child processes dynamically based on the following parameters, at least one child process
pm.max_chidren
: Maximum number of child processes that can survive at the same time
pm.start_servers
: The number of child processes created at startup, the default value ismin_spare_servers + max_spare_servers - min_spare_servers) / 2
pm.min_spare_servers
: Minimum number of child processes in idle state, if insufficient, new child processes are created automatically
pm.max_spare_servers
: Maximum number of child processes in idle state, if exceeded, some child processes will be killed
ondemand
: A child process is not created at startup and is created when a new request arrives. The following two parameters are used:
pm.max_requests
The maximum number of request services per child process, if this value is exceeded, the child process is automatically restarted. This parameter is useful when troubleshooting a memory leak problem with a third-party library. The default value is 0, which refers to a continuous service request by a child process.
PHP-FPM
Configuration Optimizations
PHP-FPM
The way to manage is a master
master process, multiple pool
process pools, and multiple worker
child processes. Each of these process pools listens for a socket
socket. The specific diagram:
Where the worker
child process actually processes the connection request, the master
main process is responsible for managing the child process:
1. ' Master ' process, set 1s timer, through ' socket ' file monitoring 2. In ' pm=dynamic ', if ' idle worker ' quantity < ' Pm.min_spare_servers ', create a new subprocess 3. In ' pm=dynamic ', if ' Idle worker ' quantity > ' pm.max_spare_servers ', kill extra idle subprocess 4. At ' Pm=ondemand ', if the ' Idle worker ' idle time > ' pm.process_idle_timeout ', kill the Idle process 5. When the connection arrives, detect if ' worker ' number > ' Pm.max_children ', print ' warning ' log, exit if no exception, use ' Idle worker ' service, or create new ' worker ' service
Securing Basic Security
We need to set a global configuration for the restart in order to prevent the PHP-FPM
main process from being hung up due to some bad PHP code:
; If 10 sub-processes are interrupted within 1min, restart the main process emergency_restart_threshold = 10emergency_restart_interval = 1m
Process Number Tuning
Each sub-process can only serve one connection at a time, so it is important to control how many processes exist at the same time, if too little can cause many unnecessary rebuilds and destruction overhead, and if too much memory is consumed, it affects other services.
We should test how much memory our PHP process uses, generally just starting at 8M or so, running for a while because memory leaks and caches will rise to around 30M, so you need to set the number of processes based on your expected memory size. At the same time, the number of child processes in a process manager is limited according to the number of process pools.
Test the memory consumed by the average PHP child process:
$ps auxf | grep php | grep-v grepwork 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//Total memory consumption $ps AUXF | grep php | Grep-v grep | Grep-v Master | awk ' {sum+=$6} END {print sum} ' 162712//total number of child processes $ ps AUXF | grep php | Grep-v grep | Grep-v Master | Wc-l 6
You can see the 6th column, where the memory footprint of each child process is approximately 19-34m (in kilobytes). The average memory footprint is 162712KB/6 = 27.1M
.
To view the total memory size of the server
$ free-g Total used free shared buffers cachedmem: 157 141 0 4 123-/+ buffers/cache: 143Swap: 0 0 0
It can be seen that my server always has a memory size of 157G (-G in units of the unit).
Number of processes limit
At this point, if we allocate all the memory to PHP-FPM
use, then the number of processes can be limited 157000/27 = 5814
, but since my Server service a lot of content at the same time, so we can adjust down to 512 process number:
Process.max = 512pm = Dynamicpm.max_children = 512pm.start_servers = 16pm.min_spare_servers = 8pm.max_spare_serveres = 30
Prevent memory leaks
Because of bad plugins and libraries, memory leaks often occur, so we need to limit the number of requests per child process service to prevent unlimited memory leaks:
pm.max_requests = 1000
Restart
If the above configuration is configured according to your actual needs and environment, do not forget to restart the PHP-FPM
service.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!