This paper mainly explains some important parameters of PHP-FPM under Linux, and introduces in detail the PHP-FPM parameter optimization of performance.
php-fpm.conf Important Parameters Detailed
PID = Run/php-fpm.pid
#pid设置, the default is var/run/php-fpm.pid in the installation directory, it is recommended to turn on
Error_log = Log/php-fpm.log
#错误日志, Var/log/php-fpm.log in the installation directory by default
Log_level = Notice
#错误级别. The available levels are: alert (which must be processed immediately), error (Error condition), warning (warning condition), notice (general important information), debug (debug information). Default: Notice.
Emergency_restart_threshold = 60
Emergency_restart_interval = 60s
If the number of-cgi processes is more than Emergency_restart_threshold, the PHP-FPM will restart gracefully. These two options generally leave the default values.
Process_control_timeout = 0
#设置子进程接受主进程复用信号的超时时间. Available units: s (seconds), M (min), H (Hours), or D (day) default units: s (seconds). Default value: 0.
Daemonize = yes
#后台执行fpm, the default value is yes if you want to change to no for debugging. In FPM, you can use different settings to run multiple process pools. These settings can be set individually for each process pool.
Listen = 127.0.0.1:9000
#fpm监听端口, the address that PHP handles in Nginx, the general default value can be. The available formats are: ' Ip:port ', ' Port ', '/path/to/unix/socket '. Each process pool needs to be set.
Listen.backlog =-1
#backlog数, 1 means unrestricted, determined by the operating system, and this line is commented out.
Listen.allowed_clients = 127.0.0.1
#允许访问FastCGI进程的IP, set any to No limit IP, if you want to set other hosts Nginx can also access this FPM process, listen to set the cost of access to the IP. The default value is any. Each address is separated by commas. Allows any server to request a connection if it is not set or is empty
Listen.owner = www
Listen.group = www
Listen.mode = 0666
#unix socket Settings option, if accessed using TCP, Comment here.
user = www
Group = www
#启动进程的帐户和组
PM = dynamic
#对于专用服务器, the PM can be set to static.
#如何控制子进程, the options are static and dynamic. If Static is selected, the number of fixed child processes is specified by Pm.max_children. If dynamic is selected, it is determined by the following argument:
Pm.max_children #, maximum number of child processes
Pm.start_servers #, number of processes at startup
Pm.min_spare_servers #, guarantees the minimum number of idle processes, and creates a new child process if the idle process is less than this value
Pm.max_spare_servers #, guarantees the maximum number of idle processes, and cleans up if the idle process is larger than this value
pm.max_requests = 1000
#设置每个子进程重生之前服务的请求数. is useful for third-party modules that may have a memory leak. If set to ' 0 ', the request is always accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0.
Pm.status_path =/status
#FPM状态页面的网址. If not set, the status page cannot be accessed. Default value: None. Munin monitoring will be used to
Ping.path =/ping
#FPM监控页面的ping网址. If not set, the ping page cannot be accessed. This page is used to externally detect whether FPM is alive and can respond to requests. Note that you must start with a slash (/).
Ping.response = Pong
#用于定义ping请求的返回相应. Returns the Text/plain format text for HTTP 200. Default value: Pong.
Request_terminate_timeout = 0
#设置单个请求的超时中止时间. This option may be useful for scripts in php.ini settings where ' Max_execution_time ' is not aborted for some special reason. Set to ' 0 ' for ' Off '. You can try changing this option when a 502 error occurs frequently.
Request_slowlog_timeout = 10s
#当一个请求该设置的超时时间后, the corresponding PHP call stack information is written to the slow log in its entirety. Set to ' 0 ' means ' Off '
Slowlog = log/$pool. Log.slow
#慢请求的记录日志, use with Request_slowlog_timeout
Rlimit_files = 1024
#设置文件打开描述符的rlimit限制. Default: System-defined value default open handle is 1024, can be viewed using ULIMIT-N, ulimit-n 2048 modified.
Rlimit_core = 0
#设置核心rlimit最大限制值. Available values: ' Unlimited ', 0, or positive integer. Default value: System-defined value.
Chroot =
#启动时的Chroot目录. The defined directory needs to be an absolute path. If not set, Chroot is not used.
ChDir =
#设置启动目录, it is automatically chdir to the directory at startup. The defined directory needs to be an absolute path. Default value: Current directory, or/directory (chroot)
Catch_workers_output = yes
#重定向运行过程中的stdout和stderr到主要的错误日志文件中. If not set, stdout and stderr will be redirected to/dev/null according to the FASTCGI rules. Default value: Empty.
PHP-FPM parameter Tuning
PM = dynamic;
Indicates which process quantity is used to manage the method
Dynamic indicates that the number of PHP-FPM processes is dynamically, starting with the number specified by Pm.start_servers, and automatically incremented if more requests are available, guaranteeing that the number of idle processes is not less than pm.min_spare_servers. If the number of processes is large, the corresponding cleanup will be done to ensure that the number of redundant processes is not more than pm.max_spare_servers
Static indicates that the number of PHP-FPM processes is static, the number of processes is pm.max_children specified, no longer increases or decreases
Pm.max_children = +; Number of PHP-FPM processes open in static mode
pm.start_servers =; Number of start PHP-FPM processes in dynamic mode
pm.min_spare_servers = 5; The minimum number of PHP-FPM processes under dynamic mode
pm.max_spare_servers = +; Maximum number of PHP-FPM processes in dynamic mode
If PM is static, then only pm.max_children this parameter takes effect. The system will turn on the set number of PHP-FPM processes
If the PM is dynamic, then the Pm.max_children parameter is invalidated and the next 3 parameters take effect. The system starts the Pm.start_servers PHP-FPM process at the beginning of the PHP-FPM run and then dynamically pm.min_spare_servers and Pm.max_spare_ according to the system's requirements Adjust the number of PHP-FPM processes between servers
So, for our server, which kind of PM is better to choose? In fact, like Apache, the running PHP program will have more or less memory leaks after the execution is completed. This is why the beginning of a PHP-FPM process only consumes about 3M of memory, running for a period of time will rise to 20-30m reason.
For memory-heavy servers (such as 8G or more), it is actually more appropriate to specify a static Max_children because it does not require additional process number control to increase efficiency. Because the frequent switch PHP-FPM process will also sometimes lag, so the memory is large enough to open the static effect will be better. The amount can also be based on memory/30m, such as 8GB memory can be set to 100, then PHP-FPM memory can control the appearance of 2g-3g. If the memory is slightly smaller, such as 1G, then specifying the number of static processes is more advantageous to the stability of the server. This ensures that the PHP-FPM only gets enough memory and allocates a limited amount of memory to other applications, which makes the system run more smoothly.
For small memory servers, such as 256M of memory VPs, even according to a 20M of memory to calculate, 10 php-cgi process will consume 200M of memory, the system crash should be very normal. Therefore, the number of PHP-FPM processes should be controlled as much as possible, and when the memory used by other applications is generally clear, assigning a small amount of static to it will make the system more stable. Or use dynamic mode, because the dynamic way will end off the redundant process, can reclaim some memory, so it is recommended to use on the server or VPS with less memory. The specific maximum number is based on the memory/20m. For example, 512M VPS, recommended pm.max_spare_servers set to 20. As for Pm.min_spare_servers, it is recommended to set the load on the server and compare the appropriate values between 5~10.
On the 4G memory Server 200 can (my 1G tester, open 64 is the best, recommend using stress test to get the best value)
Pm.max_requests = 10240;
Nginx PHP-FPM configuration Process The biggest problem is the internal leak out of the problem: the server load is small, but the memory consumption quickly increased, quickly eat the memory and then began to eat swap partition, the system quickly hung up! In fact, according to the official introduction, php-cgi There is no memory leak, after each request is completed php-cgi will reclaim memory, but will not be released to the operating system, which will cause a lot of memory is php-cgi occupied.
The official solution is to lower the value of the php_fcgi_max_requests, if the PHP-FPM is used, the corresponding php-fpm.conf is max_requests, which means that the number of requests sent will restart the thread, We need to properly lower this value to let php-fpm automatically free up memory, not most of the online say 51200 and so on, there is actually another value associated with it Max_children, this is how many processes php-fpm each time, so that the actual memory consumption is max _children*max_requests* each request to use memory, according to this we can estimate the use of memory, there is no need to write a script to kill.
Request_terminate_timeout = 30;
Maximum execution time, which can also be configured in PHP.ini (Max_execution_time)
request_slowlog_timeout = 2; Turn on Slow log
Slowlog = log/$pool. Log.slow; Slow log path
rlimit_files = 1024x768; Increase the limit of PHP-FPM open file descriptors
php-fpm.conf parameters Clearly, you just have to read a few more times should be remembered, as for the PHP-FPM performance plan should be based on the actual situation, multi-test several times to obtain the best configuration scheme
Articles you may be interested in:
Detailed steps for data migration and data population in Laravel
PHP closures Get the difference between external variables and global keyword declaration variables
Using good anyproxy to improve the collection efficiency of public number articles