Parameter introduction and file configuration of php-fpm
This article introduces the relevant knowledge of "introduction of php-fpm parameters and file configuration". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Recently, the project found that the server CPU soared to 98% when the traffic was heavy. The background viewing process is mainly PHP-fpm consuming memory. Php-fpm.conf has two important parameters:
Pm.max_children represents the maximum number of child processes that php-fpm can start.
Request_terminate_timeout indicates that processes that have been executed for too long will be terminated directly.
These two values need to be calculated and configured.
Pm.max_children
In principle, the larger the better, the more php-cgi processes will be processed quickly, and the number of queued requests will be very small.
Setting "max_children" also needs to be set according to the performance of the server.
The calculation method is as follows:
Generally speaking, a server normally consumes about the memory 20M~30M of each php-cgi.
For example, my server memory is 16 gigabytes, if I set 500, then 30 gigabytes 400 = 12000m = 11.7g
However, if the max_children setting is small, such as 5-10, then the php-cgi processing speed is also slow, the waiting time is longer, and the CPU consumption is high.
The error 504 Gateway Time-out occurs if the request is not processed for a long time, and the error 502 Bad gateway occurs if there is a problem with those php-cgi that are being processed.
The better setting mode of max_children is set according to req/s (throughput, the maximum number of requests processed by the server per unit time, unit req/s). If the program has a processing capacity of 100 req/s, it is better to set 100, which is adjusted dynamically.
Request_terminate_timeout
The calculation method is as follows:
If your server performance is good enough, and the broadband resources are sufficient, and the PHP script does not have loops or BUG, you can set "request_terminate_timeout" to 0s directly. 0s means to keep the PHP-CGI running without a time limit.
If you can't do this, that is to say, there may be a BUG in your PHP-CGI, or your broadband is insufficient or some other reason causes your PHP-CGI to fake death, then it is recommended that you assign a value to "request_terminate_timeout", which can be set according to the performance of your server.
Generally speaking, the better the performance, the higher you can set it, 20-30 minutes. According to the server PHP script needs to run for a long time, generally the response time does not need to be very long, then you can set it a little shorter. However, if the concurrency is large or some scripts in PHP take a long time to run, this will result in the death of PHP-CGI and the error of 502 Bad gateway.
Pm = dynamic; indicates which process quantity management method is used
Dynamic indicates that the number of php-fpm processes is dynamic, starting with the number specified by pm.start_servers. If there are more requests, it will automatically increase to ensure that the number of idle processes is not less than pm.min_spare_servers. If the number of processes is large, it will be cleaned accordingly to ensure that the number of redundant processes is not more than pm.max_spare_servers.
Pm = static means that the number of php-fpm processes is static, and the number of processes is the number specified by pm.max_children from beginning to end, and no longer increases or decreases.
Pm.max_children = 600; number of php-fpm processes enabled in static mode
Pm.start_servers = 200; number of initiating php-fpm processes in dynamic mode
Pm.min_spare_servers = 50; minimum number of php-fpm processes in dynamic mode
Pm.max_spare_servers = 500; maximum number of php-fpm processes in dynamic mode
Numerical settings, with reference to your actual hardware configuration, can be calculated with reference to the total memory / 30m.
If pm is set to static, only the parameter pm.max_children takes effect. The system will open a set number of php-fpm processes.
If pm is set to dynamic, then the pm.max_children parameter is invalid and the next three parameters take effect. The system will start pm.start_servers php-fpm processes at the beginning of php-fpm operation, 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.
How do you choose pm = dynamic or "pm = static"? In fact, like Apache, running PHP programs will more or less have memory leaks after execution.
This is why a php-fpm process takes up only about 3 megabytes of memory at the beginning and rises to 20-30 megabytes after a period of time.
For servers with large memory (such as more than 8G), static max_children is actually more appropriate, because it does not require additional process number control and will improve efficiency. Because frequently switching php-fpm processes can sometimes lag, it is better to turn on static when the memory is large enough. The amount can also be obtained according to the total memory / 30m, for example, 8GB memory can be set to 100, so the memory consumed by php-fpm can be controlled in the appearance of 2G-3G.
If the memory is slightly smaller, such as 1GB, then specifying the number of static processes is more conducive to server stability. This ensures that php-fpm only gets enough memory and allocates a small amount of memory to other applications, which will make the system run more smoothly.
Therefore, you should try to control the number of php-fpm processes, generally clear the memory consumed by other applications, give it a small amount of static, it will make the system more stable.
Summary: dynamic is recommended for small memory (pm = dynamic) and static is recommended for large memory (pm = static).
Php slow log, used to monitor request_slowlog_timeout = 10sslowlog = log/$pool.log.slow
Configure the maximum number of file handles that the php-fpm process can open
Rlimit_files = 1024
Default is 1024, and this value does not need to be configured
This is the end of the introduction of php-fpm parameters and file configuration. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!