Error uploading huge file in WordPress

How to reduce PHP-FPM (php5-fpm) RAM usage by about 50%

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):

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.

Related Post:  Can Artificial Intelligence be creative?

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.

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.

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:

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

Related Post:  End-to-end Machine Learning with TensorFlow on Google Cloud Platform
free -mt after reloading php5-fpm:

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:

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.

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.

Please share:
Tags: No tags

22 Responses

Add a Comment

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