Tutorials/Tips

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.

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:  Eyes in the Sky: The Rise of Gorgon Stare and How It Will Watch Us All

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

Related Post:  End-to-end Machine Learning with TensorFlow on Google Cloud Platform

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:

We Recommend These Services

Register now for Big Data & AI Conference, international Big Data and AI conference in Dallas, TX (USA), June 27 - 29, 2019

Reasons to use control panel for your server

Register for the End-to-end Machine Learning with TensorFlow on Google Cloud Platform workshop. It will be conducted by the manager of Google's Cloud AI Advocacy team

Launch an SSD VPS in Europe, USA, Asia & Australia on Vultr's KVM-based Cloud platform starting at $5:00/month (15 GB SSD, 768 MB of RAM).


22 Comments

  1. HI

    please comment using your configuration for big server is it work.

    I use it and it reduce ram the culpit is php7.2-fpm it eat all ram.

    bEST REGARDS

  2. David Levy

    The later part of the original tutorial of having each sub pool conf have it’s own master process I couldn’t get to work, and I followed the instructions exactly… anyone else able to get multiple PHP-FPM masters working?

  3. This config suggestion is stinky unless you have a very inactive website. Of course you will limit the memory usage in idle mode with this config, but if you have a highly active website, this config will drain CPU and IO uneccessarily as each PHP environment needs to be loaded to your memory before it actually – and where does the environment come from (a: disk) and who will handle it (a: cpu)? Leaving the process “open” and ready in your memory means that the PHP environment is preloaded in order to handle your visitors requests.

    One very powerful option with PHP-FPM is the performance. But this performance depends on having processes preloaded. When switching to on demand you actually kills this idea, and could rather go with something else like the horrible slow suphp.

    Suggestion. Do always keep atleast one process alive and swith pm to dynamic, as it would always fall back to “pm.start_servers” in idle periods. If you have memory limits do instead tweak the pm.max_children setting.

  4. Hi, could you explain more about this ” .. Nginx microcaching configured ..” ?

    Thanks

  5. What you fail to mention is that setting it to ondemand means longer response times as the PHP interpreter has to be started from scratch every time it’s needed. Consider leaving it at dynamic and rely on low values for the pm.max_children, pm.max_requests, and pm.process_idle_timeout settings to clear out memory usage.

  6. Matt Fletcher

    This is useful, and yes it does work.

    However, it is in some ways selling snake oil because you’re not making it any less memory-hungry; instead, you’re just preventing it from pre-allocating a maximum amount of memory, so it’s easier to get bitten on the arse when your server runs out of memory from a traffic spike. Even though PHP-FPM using up 600mb of memory, for instance, when it’s doing nothing, it allows you to work out how much memory everything else will need before the whole thing explodes.

    Still, I think this is a good option for smaller sites that don’t get a huge amount of traffic, especially if you limit the max connections. So I will be using it, but people just need to be warned that it will still use the same amount of memory for the same amount of connections- but that sudden RAM spike may come as a shock.

  7. Believe it or not, this tweak reduced my php5-fpm RAM usage by half. Good work Bro.

  8. Hello;

    Thanks for this tweak, just tried it….. since i started using my own server this is the first time php-fpm is over using CPU

  9. Thank you… this helped me a lot

  10. amazing, it reduced RAM usage more than 50% to me 🙂

  11. I have website with 100 to 200 Concurrent Users. 2GB RAM and 2 CPU Cores So what will be good config?

    My Current is

    [GLOBAL]
    pm = dynamic
    pm.start_servers = 1
    pm.max_children = 5
    pm.min_spare_servers = 1
    pm.max_spare_servers = 5

    What can be good?

  12. I think its important to point out that using less memory is a secondary issue to latency. If the box is spending time spinning up and down workers, that added latency is felt by the consumer.

    These settings might work for a small website. I would not recommend it for high usage systems. From my testing, I get the the best performance using static workers as the expense of increased memory usage.

    • Less memory is not a secondary issue to latency if it’s a small site and other things have to run in the same space. Linode servers give you a gig for the $10/mo option, and you have to make it last…

  13. Hello

    Found you on google.
    Here’s an output of server resources usage on DO:
    24069 serverp+ 20 0 490016 99564 70984 S 2.7 9.8 2:35.12 php-fpm
    24847 serverp+ 20 0 484932 93696 69316 S 2.7 9.2 1:53.23 php-fpm
    24070 serverp+ 20 0 490668 101784 71420 S 2.2 10.0 2:08.30 php-fpm
    24072 serverp+ 20 0 489028 100720 71996 S 2.2 9.9 2:06.21 php-fpm
    23977 serverp+ 20 0 484444 97488 73492 S 1.8 9.6 2:12.38 php-fpm
    23996 serverp+ 20 0 493456 107496 74340 S 1.8 10.6 2:22.87 php-fpm
    24231 serverp+ 20 0 488612 100456 72248 S 1.8 9.9 2:07.68 php-fpm
    25955 serverp+ 20 0 491272 97948 67044 S 1.8 9.6 1:12.52 php-fpm

    serverpilot@server:~$ free -ht
    total used free shared buffers cached
    Mem: 994M 925M 68M 104M 88M 216M
    -/+ buffers/cache: 620M 374M
    Swap: 511M 35M 476M
    Total: 1.5G 961M 544M

    Gonna implement these changes straight away. Thank you.

    • Can’t edit my comment, but my server runs with DO 1gb package and free serverpilot.io package.

      I made some custom config changes here and there, but I’m not a pro either.

  14. In my case, I always have HIGH CPU USAGE and LOW RAM USAGE.
    Is there any solution by tuning php5-fom? Or it is just a case of bad PHP/SQL scripts?

    • I think it’s mostly a case of bad scripts. No matter how much I tweak stuff, I’m still getting higher than expected RAM/CPU usage on the PHP side. I only managed to reduce RAM usage by MySQL simple by disabling performance metrics, though I’m not sure if it has any negative effect that I’m not seeing yet.

  15. If you enable ondemand and timeout, what happens to opcache? Is the cache enable on a per-process basis, and if so does it get wiped when that process times out?

    • Didn’t look at the effect on opcache, but now that you brought it to my attention, I’ll have to take a look at it.

    • Opcache was not enabled, but after turning it on and tweaking it, RAM usage dropped even lower. Still trying to see hwo well I can tune it so all works well… Stay tuned!

Leave a Comment

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

*