Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Resolve server memory footprint caused by PHP-FPM process

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Recently, php-fpm servers often have high memory.

The simplest and most effective way is to restart the service and simply restart the server, which may be effective for the virtual memory caused by some temporary processes, but obviously not here. It is likely to affect the business.

Solve

Check server memory and process status

Overview of php-fpm parameters

The following is a brief overview and analysis of the meaning of each parameter

Pm=dynamic

There are three settings for this item: static, dynamic, and ondemand.

One is pm = static, which always maintains a fixed number of child processes, which is defined by pm.max_children, which is inflexible and usually not the default.

The other is pm = dynamic, which is like this. When starting, a fixed number of child processes (controlled by pm.start_servers) can be understood as the minimum number of child processes, while the maximum number of child processes is controlled by pm.max_children. In this way, the number of child processes will change in the maximum and minimum range, and the number of idle child processes can be controlled by another two configurations. Pm.min_spare_servers and pm.max_spare_servers, that is, idle child processes, can also have a minimum and maximum number, and if idle child processes exceed the pm.max_spare_servers, they will be killed. (note that pm.max_spare_servers should be less than pm.max_children)

The third is the pm = ondemand mode, which is contrary to pm = dynamic and stores it in the first place. Its working mode is very simple. Each idle process will be killed after it has been idle for pm.process_idle_timeout seconds. With this mode, the memory will naturally drop during the server trough. If the server does not have a request for a long time, there will only be one php-fpm main process. Of course, the drawback is It is impossible to avoid the problem that the server frequently creates processes during peak periods or if the value of pm.process_idle_timeout is too short, so who is more suitable for pm = dynamic or pm = ondemand depends on the actual situation.

As you can see, the pm = dynamic mode is very flexible and is usually the default option. However, dynamic mode will result in more memory usage in order to maximize server response, because this mode will only kill idle processes that exceed the maximum number of idle processes (pm.max_spare_servers), for example, the maximum number of idle processes is 30, the maximum number of idle processes is 50, and then the site goes through a visit peak, when all 50 processes are busy, 0 idle processes, and then after the peak. There may not be a single request, so there will be 50 idle processes, but at this time php-fpm will only kill 20 child processes, and always 30 processes will continue to wait for requests as idle processes, which may be why even though the number of requests has been greatly reduced after the peak, the server memory usage has not been greatly reduced, or it may be why sometimes the server will be much better after restarting, because after the restart The number of children of the php-fpm becomes the minimum number of idle processes, rather than the previous maximum number of idle processes.

Max_requests

That is to say, if each process exceeds this number (it has a little to do with php processes, it is not relevant), it will be killed automatically.

Max_children

The maximum number of processes, generally speaking, a server normally consumes about 40m of memory per php-fpm, and the ideal maximum number of processes can be calculated as 1000,000,40,25, but in fact, memory is not only occupied by php-fpm, so it can be reduced appropriately according to the actual situation so that the memory will not be exhausted because of too many php-fpm processes. If my "max_children" setting is small, such as 5-10, then the php-fpm will be "very tired", the processing speed will be slow, and the waiting time will be longer. The error of 504 Gateway Time-out will occur if there is no request to deal with for a long time.

Request_terminate_timeout

Set according to the performance of your server. Generally speaking, the better the performance, the higher you can set it, 20-30 minutes. Because my server PHP script takes a long time to run, some of it may take more than 10 minutes, so I set it for 1200 seconds, so it won't cause php-fpm to die and cause the error of 502Bad gateway.

Pm.start_servers

Number of initiating php-fpm processes in dynamic mode

Pm.min_spare_servers

Minimum number of php-fpm idle processes in dynamic mode

Pm.min_spare_servers

Maximum number of php-fpm idle processes in dynamic mode

The php-fpm configuration file is taken apart for ease of management. Under the / opt/plesk/php/7.1/etc/php-fpm.d directory

So we can locate which occupancy.

[root@orion php-fpm.d] # ls

Aabus.com.conf coachrun.com.conf jadiamortgage.com.conf jadia.net.conf livechat.taketours.com.conf lltours.com.conf sunshineboston.com.conf

[root@orion php-fpm.d] # vim livechat.taketours.com.conf

This is the previous configuration file:

Pm = ondemand # on demand ondemandpm.max_children = 5 # maximum number of processes pm.max_spare_servers = 1 # maximum number of php-fpm idle processes in dynamic mode pm.min_spare_servers = 1 # minimum number of php-fpm idle processes in dynamic mode pm.process_idle_timeout = 10s # how long does it take to kill php- FPM child processes on demand Until the master process pm.start_servers = 1 # the number of starting php-fpm processes in dynamic mode

Modified configuration:

Pm = ondemandpm.max_children = 5pm.max_spare_servers = 2pm.min_spare_servers = 2pm.process_idle_timeout = 10spm.start_servers = 1pm.max_requests = 1024 means that each child process restarts after 1024 requests have been processed

After tracking for a while, it is found that there is a fluctuation in the 30000kb, and the percentage of physical memory used by the process is only 0.1%.

Reference article: https://www.centos.bz/2018/03/php-fpm%E8%BF%9B%E7%A8%8B%E8%BF%87%E5%A4%9A%EF%BC%8C%E5%86%85%E5%AD%98%E8%80%97%E5%B0%BD/

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report