Home  >  Article  >  Backend Development  >  Introduction to php-fpm parameter configuration and detailed explanation of parameter optimization under Linux

Introduction to php-fpm parameter configuration and detailed explanation of parameter optimization under Linux

jacklove
jackloveOriginal
2018-07-07 17:53:232147browse

This article mainly explains the Chinese detailed description of some important parameters of php-fpm under Linux, and introduces in detail the parameter optimization of php-fpm regarding performance

php-fpm Detailed explanation of important .conf parameters

pid = run/php-fpm.pid
#pid setting, the default is var/run/php in the installation directory -fpm.pid, it is recommended to enable

error_log = log/php-fpm.log
# error log, which is var/log/php-fpm.log in the installation directory by default.

log_level = notice
#Error level. Available levels are: alert (must be processed immediately), error (error situation), warning (warning situation), notice (general important information) ), debug (debugging information). Default: notice.

emergency_restart_threshold = 60
emergency_restart_interval = 60s

# means that a SIGSEGV or SIGBUS error occurs within the value set by emergency_restart_interval If the number of php-cgi processes exceeds emergency_restart_threshold, php-fpm will restart gracefully. These two options generally remain at their default values.

process_control_timeout = 0
#Set the timeout for the child process to accept the main process multiplexing signal. Available units: s (seconds), m (minutes), h (hours), Or d (days) Default unit: s (seconds). Default value: 0.

daemonize = yes
#Execute fpm in the background, the default value is yes, if it is for debugging, it can Change to no. In FPM, it is possible to run multiple process pools with different settings. These settings can be set individually for each process pool.

listen = 127.0.0.1:9000
#fpm listening port, which is the address processed by php in nginx. Generally, the default value is sufficient. Available formats are: 'ip:port', 'port', '/path/to/unix/socket'. Each process pool needs to be set.

listen.backlog = -1
#The number of backlogs, -1 means no limit, is determined by the operating system, just comment out this line.

listen.allowed_clients = 127.0.0.1
#Allow access to the IP of the FastCGI process. Set any to unrestricted IP. If you want to set nginx of other hosts to also access this FPM Process and listen must be set to a local IP that can be accessed. The default value is any. Each address is separated by a comma. If not set or empty, any server requesting a connection is allowed

##listen.owner = wwwlisten.group = www
listen.mode = 0666
#Unix socket setting options, if you use tcp to access, just comment here.

user = wwwgroup = www
#The account and group that started the process

pm = dynamic# For dedicated servers, pm can be set to static.
#How to control the child process, the options are static and dynamic. If static is selected, a fixed number of child processes is specified by pm.max_children. If dynamic is selected, it is determined by the following parameters:

pm.max_children #, Maximum number of child processes
pm.start_servers #, Number of processes at startup
pm.min_spare_servers #, ensure the minimum number of idle processes. If the idle process is less than this value, create a new child process
pm.max_spare_servers #, ensure the number of idle processes Maximum value, if the idle process is larger than this value, this will be cleaned up

pm.max_requests = 1000#Set the number of requests served before each child process is reborn. For possible memory leaks It is very useful for third-party modules. If set to '0', requests are always accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.

pm.status_path = /status#The URL of the FPM status page. If not set, the status page cannot be accessed. Default value: none. Munin monitoring will use

ping.path = /ping#The ping URL of the FPM monitoring page. If it is not set, the ping page cannot be accessed. This page is used to externally detect whether FPM is alive and can respond to requests. Please note that it must start with a slash (/).

ping.response = pong# is used to define the return response of the ping request. The returned text/plain format text is HTTP 200. Default value: pong.

request_terminate_timeout = 0#Set the timeout abort time for a single request. This option may be useful for scripts where the 'max_execution_time' in the php.ini setting does not abort running scripts for some special reasons. Setting '0' means 'Off'. You can try changing this option when 502 errors occur frequently.

request_slowlog_timeout = 10s#When a request exceeds the set timeout, the corresponding PHP call stack information will be completely written to the slow log. Set to '0 ' means 'Off'

slowlog = log/$pool.log.slow
#Slow request recording log, used with request_slowlog_timeout

rlimit_files = 1024
#Set the rlimit limit of the file open descriptor. Default value: The system-defined value of the default open handle is 1024, which can be viewed using ulimit -n and modified with ulimit -n 2048.

rlimit_core = 0
#Set the maximum limit value of core rlimit. Available values: 'unlimited', 0 or positive integer. Default value: System-defined value.

chroot =
#Chroot directory at startup. The defined directory needs to be an absolute path. If not set, chroot will not be used.

chdir =
#Set the startup directory, which will be automatically Chdired to during startup. The defined directory needs to be an absolute path. Default value: current directory, or / directory (when chrooting)

catch_workers_output = yes
#Redirect stdout and stderr during the running process to the main error log file. If not set, stdout and stderr will be redirected to /dev/null according to FastCGI rules. Default Value: empty.

php-fpm parameter tuning

pm = dynamic;

Indicates which process number management method is used

#dynamicIndicates that the number of php-fpm processes is dynamic, starting with the number specified by pm.start_servers. If there are many requests, it will be automatically increased to ensure that the number of idle processes is not less than pm.min_spare_servers. If there are many processes, it will be cleaned up accordingly to ensure that the number of redundant processes is not more than pm.max_spare_servers

staticIndicates that the number of php-fpm processes is static. The number of processes is the number specified by pm.max_children from beginning to end, and will no longer increase or decrease.

pm.max_children = 300; Number of php-fpm processes started in static mode
pm.start_servers = 20; Number of starting php-fpm processes started in dynamic mode
pm.min_spare_servers = 5; The minimum number of php-fpm processes in dynamic mode
pm.max_spare_servers = 35;The maximum number of php-fpm processes in dynamic mode

If pm is static, then In fact, only the parameter pm.max_children takes effect. The system will open a set number of php-fpm processes

If pm is dynamic, then the pm.max_children parameter will be invalid and the next three parameters will take effect. The system will start pm.start_servers php-fpm processes when php-fpm starts running, and then dynamically adjust the number of php-fpm processes between pm.min_spare_servers and pm.max_spare_servers according to the needs of the system.

Then, For our server, which PM method is better? In fact, like Apache, the running PHP program will more or less have memory leaks after execution. This is also the reason why a php-fpm process only occupies about 3M of memory at the beginning, and it will increase to 20-30M after running for a period of time.

For servers with large memory (such as 8G or more), it is actually more appropriate to specify static max_children, because this does not require additional process number control and will improve efficiency. Because frequent switching of the php-fpm process will cause lag, so if the memory is large enough, the static effect will be better. The quantity can also be obtained based on memory/30M. For example, 8GB memory can be set to 100, then the memory consumed by php-fpm can be controlled to 2G-3G. If the memory is slightly smaller, such as 1G, then specifying a static number of processes is more conducive to the stability of the server. This can ensure that php-fpm only obtains enough memory, and allocates a small amount of memory to other applications for use, which will make the system run more smoothly.

For a server with small memory, such as a VPS with 256M memory, even if it is calculated based on a 20M memory, 10 php-cgi processes will consume 200M of memory, and the system crash should be very serious. Normal. Therefore, you should try to control the number of php-fpm processes as much as possible. After roughly clarifying the memory occupied by other applications, assigning it a static small number will make the system more stable. Or use dynamic mode, because dynamic mode will end redundant processes and can recycle and release some memory, so it is recommended to use it on servers or VPS with less memory. The specific maximum amount is obtained based on memory/20M. For example, for a 512M VPS, it is recommended to set pm.max_spare_servers to 20. As for pm.min_spare_servers, it is recommended to set it according to the load of the server. A more suitable value is between 5 and 10.

On a server with 4G memory, 200 is enough (for my 1G test machine, 64 is the best. It is recommended to use stress testing to obtain the best value)

pm. max_requests = 10240;

nginx The biggest problem during the php-fpm configuration process is internal leakage: the load on the server is not large, but the memory usage increases rapidly. It quickly eats up the memory and then starts to eat the swap partition, and the system quickly hangs! In fact, according to the official introduction, php-cgi does not have memory leaks. After each request is completed, php-cgi will reclaim the memory, but will not release it to the operating system. This will cause a large amount of memory to be occupied by php-cgi.

The official solution is to lower the value of PHP_FCGI_MAX_REQUESTS. If you are using php-fpm, the corresponding php-fpm.conf is max_requests. This value means how many requests will be sent before the thread will be restarted. , we need to lower this value appropriately to allow php-fpm to automatically release memory. It is not 51200 and so on as most people say on the Internet. In fact, there is another value max_children associated with it. This is every time php-fpm How many processes will be created, so that the actual memory consumption is max_children*max_requests*memory used by each request. Based on this, we can estimate the memory usage, so there is no need to write a script to kill.

request_terminate_timeout = 30;

The maximum execution time can also be configured in php.ini (max_execution_time)

request_slowlog_timeout = 2 ; Enable slow log
slowlog = log/$pool.log.slow; Slow log path

rlimit_files = 1024; Add php-fpm Limitations on opening file descriptors

The parameters of php-fpm.conf are clearly stated. You can probably remember them as long as you read them a few times. As for the php-fpm performance plan, it should be determined based on the actual situation and tested several times. Get the best configuration solution

Articles you may be interested in:

Detailed steps for data migration and data filling in Laravel

Explanation on the difference between PHP closure acquisition of external variables and global keyword declaration of variables

Detailed explanation of using anyproxy to improve the efficiency of public account article collection

The above is the detailed content of Introduction to php-fpm parameter configuration and detailed explanation of parameter optimization under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn