How to reduce PHP-FPM (php5-fpm) RAM usage by about 50%
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:
1234 ; Choose how the process manager will control the number of child processes.pm = ondemandpm.max_children = 75pm.process_idle_timeout = 10spm.max_requests = 500The 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: Eyes in the Sky: The Rise of Gorgon Stare and How It Will Watch Us Allfree –mt after reloading php5-fpm:
1234 total used free shared buffers cachedMem: 490 196 293 28 9 70–/+ buffers/cache: 116 373Swap: 2047 452 1595Total: 2538 649 1888Compared to the output before, that’s more than a 50% drop in RAM usage. And the reason became obvious when I viewed top again:
1234 2778 mysql 20 1359152 56708 3384 S 0.0 11.3 2:11.06 mysqld26896 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–fpm25818 root 20 64208 4148 1492 S 0.0 0.8 :01.88 php5–fpm17385 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–analysisd1500 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 initDid 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.