PHP-FPM is the FastCGI Process Manager for PHP. On Unix-like operating systems, including Linux and BSD distributions, PHP-FPM is enabled by installing the php5-fpm (Linux) or php56-fpm (FreeBSD 10.1) package.

The problem with PHP-FPM is that the default configuration and that promoted by numerous blogs chews up too much resources – RAM and CPU. The server that powers this blog suffers from the same fate. And that’s because I used the same tutorials available out there that promote inefficient PHP-FPM configuration options.

You’ll find those inefficient configuration options in the pool file or files under the /etc/php5/fpm/pool.d directory. Here, for example, is the set of inefficient configuration options on one of my Cloud servers (not the one that powers this site):

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

That particular server is a DigitalOcean Droplet with 512 MB of RAM (that, by the way, is my referral link). It currently hosts one new website and when idle (no traffic whatsoever), only swapping saves it from freezing. The output of top shows the major consumers of RAM on the server.

13891 cont      20   0  396944  56596  33416 S  0.0 11.3   0:14.05 php5-fpm
13889 cont      20   0  396480  56316  32916 S  0.0 11.2   0:17.67 php5-fpm
13887 cont      20   0  624212  55088  32008 S  0.0 11.0   0:14.02 php5-fpm
13890 cont      20   0  396384  55032  32312 S  0.0 11.0   0:13.39 php5-fpm
13888 cont      20   0  397056  54972  31988 S  0.0 11.0   0:14.16 php5-fpm
14464 cont      20   0  397020  54696  31832 S  0.0 10.9   0:09.44 php5-fpm
13892 cont      20   0  396640  54704  31936 S  0.0 10.9   0:12.84 php5-fpm

13883 cont      20   0  396864  54692  31940 S  0.0 10.9   0:15.64 php5-fpm
13893 cont      20   0  396860  54628  32004 S  0.0 10.9   0:15.13 php5-fpm
13885 cont      20   0  396852  54412  32116 S  0.0 10.8   0:13.94 php5-fpm
13884 cont      20   0  395164  53916  32364 S  0.0 10.7   0:13.51 php5-fpm
13989 cont      20   0  394960  53548  32108 S  3.7 10.7   0:14.37 php5-fpm
2778 mysql     20   0 1359152  31704   1728 S  0.7  6.3   1:38.80 mysqld
13849 root      20   0  373832   1180    188 S  0.0  0.2   0:03.27 php5-fpm

That output shows 12 php5-fpm child processes (owner cont) spawned from one master process (owner root). And all 12 are just sitting there, doing nothing but consuming more than 10% of RAM each. And those child processes where mostly made possible by the pm = dynamic pool configuration option.

Related Post:  How to set up a 3-node CoreOS cluster, just for fun

To be honest, the vast majority of Cloud server owners do not know what all those options mean or do. It’s mostly copying and pasting. And I will not pretend that I know what every option in every PHP files means or does. I, too, for the most part, was a victim of copying and pasting.

But I examine resource usage often and always wonder why my servers use up too much RAM and CPU. Here, for another example, is the output of free -mt on this particular server.

              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

That’s almost 1 GB of memory usage (actual RAM plus swap) with no traffic. Sure, tuning the pm values would have made a difference, but only slightly; with pm = dynamic, there will still be child processes sitting idle waiting to be called into action.

I became aware of what an alternative configuration would do after reading an article titled A better way to run PHP-FPM. It was written about a year ago, so it’s kinda disappointing that I came across it while searching for a related topic just last night. If you run your own server and use PHP with PHP-FPM, you need to read that article.

After I read it, I changed the pm options in the pool configuration file to these:

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

The major change was setting pm = ondemand instead of pm = dynamic. And the impact on resource usage was drastic. Here, for example, is the output of free -mt after reloading php5-fpm:

              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

Compared to the output before, that’s more than a 50% drop in RAM usage. And the reason became obvious when I viewed top again:

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

Did you notice that there are no child processes? What happened to them? That’s what setting pm = ondemand does. A child process is spawned only when needed. After it’s done its job, it remains idle for 10 seconds (pm.process_idle_timeout = 10s) and then dies.

Related Post:  GRUB Customizer 4 released. Install it on Ubuntu 13.10 and Linux Mint 16

So what I have is a simple modification to the default PHP-FPM settings that saved me more than 50% of RAM. Sure, the server hasn’t come under heavy traffic, but I think it can withstand a reasonably heavy traffic, considering that it only has 512 MB of RAM. And with Nginx microcaching configured, I think it will do very well. There are other aspects of PHP-FPM and Percona MySQL that I’ve not optimized yet, so stay tuned. This was just to pass on a little tip that I found useful.


Share on facebook
Share on twitter
Share on pinterest
Share on linkedin

Hola! Did you notice that no longer runs network ads?  Yep, no more ads from the usual suspects that track you across the Internet.  But since  I still need to pay to keep the site running, feel free to make a small donation by PayPal.

Subscribe for updates. Trust me, no spam!

Mailchimp Signup Form

Sponsored links

1. Attend Algorithm Conference, a top AI and ML event for 2020.
2. Reasons to use control panel for your server.
3. DHgate Computers Electronics, Cell Phones & more.

3 Responses

  1. I am surprised that Ubuntu installer still doesn’t offer creation of SWAPFILE on root partition. On my new installation I don’t use SWAP partition but I create swapfile on Linux filesystem. “Windows way…” 😉 I understand that separate SWAP partition has some advantages but for most users there is no real difference compared to swap file. Advantage of SWAPFILE is that I can adjust size in easy way and disk layout is simpler; just one partition for Windows and second one for Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest

On social media
Via my newsletter
Mailchimp Signup Form

Partner links

1. Attend Algorithm Conference, a top AI and ML event for 2021.
2. Reasons to use control panel for your server.
3. DHgate Computers Electronics, Cell Phones & more.
Hacking, pentesting distributions

Linux Distributions for Hacking

Experts use these Linux distributions for hacking, digital forensics, and pentesting.


The authors of these books are confirmed to speak during

Algorithm Conference

T-minus AI

Author was the first chairperson of AI for the U.S. Air Force.

The case for killer robots

Author is the Director of the Center for Natural and Artificial Intelligence.

Why greatness cannot be planned

Author works on AI safety as a Senior Research Scientist at Uber AI Labs.

Anastasia Marchenkova

An invitation from Anastasia Marchenkova

Hya, after stints as a quantum researcher at Georgia Tech Quantum Optics & Quantum Telecom Lab, and the University of Maryland Joint Quantum Institute, I’m now working on superconducting qubit quantum processors at Bleximo. I’ll be speaking during Algorithm Conference in Austin, Texas, July 16 – 18, 2020. Meet me there and let’s chat about progress and hype in quantum computing.