Recently I found that the memory of the blog was always being "eaten" every now and then. After logging in to the backend, there would occasionally be lags. At first, I suspected that it was caused by insufficient Swap, so I added a few G of Swap to the VPS host and observed. After a while, I found that no matter how big the Swap was, it was slowly "eaten"!

It is obvious that some PHP services have been occupying the memory of the VPS and have not released it, causing Swap to be called after the physical memory is exhausted. Obviously, Swap does not run as efficiently as the physical memory, so the process becomes stuck. Considering that the WordPress website currently uses too many themes and plug-ins, it is normal for this situation to occur.

In the LNMP architecture, PHP runs in FastCGI mode. According to the official statement, php-cgi will recycle all the memory used by the script at the end of each request, but it will not release it to the operating system, but will continue to hold it. Respond to the next PHP request. And php-fpm is the FastCGI process manager, used to control php's memory and processes.

Linux php-fpm optimization experience-php-fpm process takes up a lot of memory and does not release memory

Therefore, the solution is to optimize the total number of processes and the memory occupied by a single process through php-fpm, thereby solving the problem of the php-fpm process occupying large memory and not releasing memory. More Linux server optimization methods and website building experience, as well as:

  1. Linux Crontab command scheduled task basic syntax and operation tutorial-VPS/Server Automation
  2. Alibaba Cloud Japan VPS Host Speed ​​Performance Evaluation-Japan SoftBank Hong Kong NTT Line
  3. DNS domain name resolution enables DNSSEC to prevent DNS hijacking-Google Cloud DNS settings DNSSEC

PS: Updated on December 14, 2018, If the memory and performance of your VPS host are not very good, it is best to enable caching at this time to greatly save resource consumption: WordPress enables Nginx fastcgi_cache cache acceleration method -Nginx configuration example.

PS: Updated on September 29, 2019, Due to the server optimization artifact ngx_pagespeed developed by Google, it integrates a complete set of optimization tools such as image delayed loading, adaptive webp, JS and CSS optimization, and image optimization: PageSpeed Server optimization artifact-Nginx deploys the ngx_pagespeed module and accelerates the effect experience.

1. Analyze and determine the memory usage of php-fpm

If you find that the VPS host is stuck, first check the memory usage. Commonly used commands are Top, Glances, Free, etc. Friends who don’t understand these commands can first check out the topic of whether the mining site is working: Linux system Summary of monitoring commands - master CPU, memory, disk IO, etc. to find performance bottlenecks.

Use the Glances command and press m to view the memory usage of the current VPS host process and sort them from most to least occupied memory (or use the Top command and press M, the effect is the same). As shown below (click to enlarge):

3.2  Reduce the number of php-fpm processes

If the memory of your VPS host is occupied and exhausted, you can check the number of your php-fpm processes. Calculate according to the number of php-fpm processes = memory/2/30. The number of php-fpm processes suitable for 1GB of memory is 10- Between 20 and 20, it depends on the add-ons loaded by your PHP.

3.3  php-fpm configuration example

Here, a VPS configuration of php-fpm with 1GB memory is used as a demonstration. In actual operation, the setting value must be considered based on the performance of the server itself, PHP, etc.

pm = dynamic #dynamic和ondemand适合小内存。
pm.max_children = 15 #static模式下生效,dynamic不生效。
pm.start_servers = 8 #dynamic模式下开机的进程数量。
pm.min_spare_servers = 6 #dynamic模式下最小php-fpm进程数量。
pm.max_spare_servers = 15 #dynamic模式下最大php-fpm进程数量。

4. Solve the problem that the php-fpm process does not release memory

The above problem of reducing the memory usage of php-fpm is achieved by reducing the total number of php-fpm processes. During actual use, it is found that the php-fpm process still has the problem of occupying memory for a long time without releasing it. The solution is to reduce the number of pm.max_requests.

The maximum number of requests, max_requests, means that when the number of requests processed by a PHP-CGI process accumulates to max_requests, the process will be automatically restarted, thus achieving the purpose of releasing memory. Take the VPS host settings with 1GB of memory as an example (if the value you set does not reach the level of freeing up memory, you can continue to lower it):

pm.max_requests = 500 

When the php-fpm process reaches the value set by pm.max_requests, the process will be restarted to release memory. The picture below is the result after my test. It can be seen that the php-fpm process was forcibly terminated and the memory was released.

5. Summary

For large memory and concurrency and availability requirements, it is recommended to use static management mode + the largest pm.max_children. If it is a server with small memory, it is recommended to use dynamic or ondemand mode and reduce the number of pm.start_servers and pm.max_spare_servers processes.

Why didn't I adjust the parameters to achieve the desired effect? According to the experience of wzfou.com, the php-fpm configuration file parameters cannot be generalized. They must be adjusted based on the server's own performance, WEB dynamic content and availability requirements. If the memory is occupied for a long time, it is best to check whether there is a memory leak.

Updated on October 9, 2019, If your php-fpm parameters are adjusted too small, a 502 error may occur. Solution: Solve the 502 error in the WordPress background edit save menu.

Leave a Reply