Home Backend Development PHP Tutorial Introducing solutions to reduce PHP-FPM memory usage

Introducing solutions to reduce PHP-FPM memory usage

Aug 16, 2017 am 09:47 AM
php php-fpm reduce


Abstract: PHP-FPM is a FastCGI process manager for PHP. In Unix-like operating systems (including Linux and BSD systems), PHP-FPM is used by installing php5-fpm (Linux) or php56-fpm (FreeBSD 10.1). But the biggest problem with PHP-FPM installed by default and installed according to the recommendations of many blogs is...

PHP-FPM is the ## of PHP #FastCGIProcess Manager. In Unix-like operating systems (including Linux and BSD systems), PHP-FPM is installed by php5-fpm(Linux) or php56-fpm(FreeBSD 10.1) to use.

But the biggest problem with the default installation and PHP-FPM installed as recommended by a large number of blogs is that it consumes a lot of resources, including memory and CPU. The server used by this blog suffered a similar fate. Because I also installed according to those tutorials, and the description of the configuration options of PHP-FPM in the tutorials is not effective enough.

You can find these inefficient configuration options in the /etc/php5/fpm/pool.d directory. For example, here are the inefficient options on my server (not the current site, of course):

; Choose how the process manager will control the number of child processes.
pm = dynamic
pm.max_children = 75pm.start_servers = 10pm.min_spare_servers = 5pm.max_spare_servers = 20pm.max_requests = 500
Copy after login

That server is a DigitalOcean Droplet, configured 512M Memory. A new website was running on it, and even when it was completely idle, it had to swap memory to avoid freezing. Executing the top command displays the processes that occupy the most memory on the server.

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
13891 cont      20     396944  56596  33416 S  0.0 11.3   :14.05 php5-fpm
13889 cont      20     396480  56316  32916 S  0.0 11.2   :17.67 php5-fpm
13887 cont      20     624212  55088  32008 S  0.0 11.0   :14.02 php5-fpm
13890 cont      20     396384  55032  32312 S  0.0 11.0   :13.39 php5-fpm
13888 cont      20     397056  54972  31988 S  0.0 11.0   :14.16 php5-fpm
14464 cont      20     397020  54696  31832 S  0.0 10.9   :09.44 php5-fpm
13892 cont      20     396640  54704  31936 S  0.0 10.9   :12.84 php5-fpm
 
13883 cont      20     396864  54692  31940 S  0.0 10.9   :15.64 php5-fpm
13893 cont      20     396860  54628  32004 S  0.0 10.9   :15.13 php5-fpm
13885 cont      20     396852  54412  32116 S  0.0 10.8   :13.94 php5-fpm
13884 cont      20     395164  53916  32364 S  0.0 10.7   :13.51 php5-fpm
13989 cont      20     394960  53548  32108 S  3.7 10.7   :14.37 php5-fpm
2778 mysql     20    1359152  31704   1728 S  0.7  6.3   1:38.80 mysqld
 
13849 root      20     373832   1180    188 S  0.0  0.2   :03.27 php5-fpm
Copy after login

输出结果显示有12php5-fpm子进程(用户名是cont)和一个主进程(用户名是root)。而这12个子进程只是呆坐在那里,什么事也不做,每个子进程白白消耗超过10%的内存。这些子进程主要是由pm=dynamic这个配置选项产生的。

老实说,绝大部分的云主机拥有者也不知道所有这些配置选项是干什么用的,只是简单地复制粘贴而已。我也不准备假装我了解每个PHP配置文件里的每一个选项的目的和意义。我在很大程度上也是复制粘贴的受害者。

但是我经常检查服务器的资源占用情况,困惑于为什么我的服务器占用这么多的内存和CPU。举另外一个例子,是这台服务器上的free -mt命令的结果:

              total       used       free     shared    buffers     cached
Mem:           490        480          9         31          6         79
-/+ buffers/cache:        393         96
Swap:         2047        491       1556
Total:        2538        971       1566
Copy after login

在没有任何访问量的情况下,也几乎有整整1G的内存(实际内存加上交换内存)被占用。当然,通过调整配置pm的数量可以有所改变,但只是轻微的。只要设置pm=dynamic,就会有空闲的子进程等在那里等待被使用。

直到读了一篇文章《A better way to run PHP-FPM》(更好地运行PHP-FPM)之后,我开始意识到应该如何修改我的配置文件。那篇文章是大约一年前写的,令人失望的是我从昨天晚上搜索相关主题时才看到它。如果你也有服务器并且使用PHP-FPM的话,我建议你好好读一下那篇文章。

读完文章之后,我修改了我的pm选项,如下:

; Choose how the process manager will control the number of child processes.pm = ondemand
pm.max_children = 75pm.process_idle_timeout = 10s
pm.max_requests = 500
Copy after login

最主要的改动就是用pm=ondemand替换了pm=dynamic。这一改动对资源占用的影响是巨大的。下面是改动并重新加载php5-fpm之后运行free -mt的结果:

              total       used       free     shared    buffers     cached
Mem:           490        196        293         28          9         70
-/+ buffers/cache:        116        373
Swap:         2047        452       1595
Total:        2538        649       1888
Copy after login

和之前的结果对比,内存使用量下降了50%。产生这一下降的原因通过执行top命令一目了然:

 2778 mysql     20    1359152  56708   3384 S  0.0 11.3   2:11.06 mysqld                               
26896 root      20     373828  19000  13532 S  0.0  3.8   :02.42 php5-fpm                             25818 root      20      64208   4148   1492 S  0.0  0.8   :01.88 php5-fpm
25818 root      20      64208   4148   1492 S  0.0  0.8   :01.88 php5-fpm                            
17385 root      20      64208   4068   1416 S  0.0  0.8   :02.23 php5-fpm                              1465 ossec     20      15592   2960    480 S  0.0  0.6   :08.60 ossec-analysisd                      
 1500 root      20       6312   2072    328 S  0.0  0.4   :45.55 ossec-syscheckd                          1 root      20      33444   1940    812 S  0.0  0.4   :03.29 init
Copy after login

你注意到这里已经没有子进程了吗?它们去哪里了?这就是设置pm=ondemand的作用。这样设置之后,只有当有需要的时候,子进程才会被产生。事情做完之后,子进程会留在内存中10秒钟时间(pm.process_idle_timeout = 10s),然后自己退出。

我只是对PHP-FPM的配置做了一点小小的修改,就节省了50%的内存。当然,这台服务器没有承受大并发的压力,但我相信它能顶得住合理的高负载,考虑到它只有512M内存。再加上Nginx微缓存的配置,我想它会做的更好。当然还有另外一些PHP-FPMPercona MySQL的配置优化我还没有做。这里只是给大家分享一个我觉得非常有用的小窍门。

The above is the detailed content of Introducing solutions to reduce PHP-FPM memory usage. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles