We Have Described the Way to Calculate the Total RAM Usage by PH5-FPM to Prevent Eating Up Memory to OOM. Optimize PHP5-FPM Memory Usage. It is not always easy to calculate and often, a not properly set and monitored RAM usage can miss any attack including XMLRPC Attack. Eating up memory can drive you crazy and server can reboot out of memory. We never suggest to use swap on cloud server instances, except for emergency need. What the providers supply for cloud servers, it is very difficult to predict. Using very high values of pm.max_children
, pm.start_servers
etc. is not exactly a great idea. It becomes the culprit for abnormal eating up of memory followed by reboot by the virtualization software. It is very bad & some web host may give warning. Your instances are sucking whole memory & suddenly releasing after reboot. These are towards doing odd suspicious works. Sysadmins understand that is not DDoS, but honestly that reboot is not good for your total system.
Optimize PHP5-FPM Memory Usage : Cloud Server & Provider
Calculation of real RAM is essentially impossible to physically calculate on cloud server – even without any cheating method applied by the provider. It is on a software defined layer above the real physical RAM. Obviously dedicated is the best choice but not exactly cost effective.
4GB RAM on Cloud Server is a good size for WordPress with MySQL on the same server. Rough calculation is – 1GB for Php, 1GB for MySQL, keep 1GB free and rest 1GB will be sucked by the other softwares including Nginx. Otherwise, it may become difficult to SSH in to instance on high load! 2GB RAM on Cloud Server is a good size for WordPress with MySQL on different server. Basically 3GB server does not exist and more instances you will have, more will be wastage of some memory. Instances basic operating system with some softwares will eat minimum 128MB RAM. Aruba Cloud, VPSdime, OVH – all are giving higher RAM at lower price. With increasing competition, there is no meaning to use the high RAM server on a well renowned provider like Rackspace. Keeping one instance of Rackspace in front as reverse proxy does the work of having great, white listed IP. Linode, DigitalOcean are in between services. There is basically no meaning of having “in-between” quality. With low RAM instance as Nginx reverse proxy on Rackspace or HP Cloud or Amazon, 100% uptime is near obvious.
---
Optimize PHP5-FPM Memory Usage : Calculations
Run free
commands manually few times on random time gap, you should get something like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | root@localhost:~# free total used free shared buffers cached Mem: 4045132 1066992 2978140 38636 47000 171848 -/+ buffers/cache: 848144 3196988 Swap: 131068 0 131068 root@localhost:~# free total used free shared buffers cached Mem: 4045132 1044780 3000352 39112 49224 173696 -/+ buffers/cache: 821860 3223272 Swap: 131068 0 131068 root@localhost:~# free total used free shared buffers cached Mem: 4045132 1096056 2949076 42972 59264 193756 -/+ buffers/cache: 843036 3202096 Swap: 131068 0 131068 |
You can see that middle one had 3000352
MB RAM free. That means, RAM consumption is not increasing with time – that is suspicious! Run top
command to check the load average. If RAM consumption is continuously increasing after restarting PHP5-FPM, it is likely a problem of wrong settings of /etc/php5/fpm/pool.d/www.conf
file. This command will tell us average process memory :
1 | ps -ylC php5-fpm --sort:rss | awk '!/RSS/ { s+=$8 } END { printf "%s\n", "Dr. Abhishek Ghosh Calculated Total Memory Used by PHP-FPM Child Processes is : "; printf "%dM\n", s/1024 }' |
This is derived from :
1 | ps -ylC php-fpm --sort:rss |
For your easiness, I have created a script here on Github Gist, which can directly wget from this URL as raw file from server :
1 2 3 4 | cd ~ wget https://gist.githubusercontent.com/AbhishekGhosh/01b935bc71200badd89f/raw/1d21fe369d14865371731b8cef81e18c3bf7a5b9/mem_php5.sh chmod +x mem_php5.sh |
whenever you’ll run sh mem_php5.sh
from $HOME
(~
) location, you can easily get this output :
1 2 3 4 5 6 | root@localhost:~# sh mem_php5.sh Dr. Abhishek Ghosh Calculated, Total Memory Used by PHP-FPM Child Processes is : 109M and One PHP5-FPM Process is Eating : 54M |
Without my name, you’ll forget with time, where from this script came! Practical fact.
Optimize PHP5-FPM Memory Usage & Prevent Out of Memory (OOM)
If you have a memory leak, then pm.max_requests
will kill/restart the PHP-FPM Child Processes after it is handled this many requests. Try setting this to a number to causes each child process to be respawned every 20 minutes or so. So if you get 200 requests a minute, and you have 5 processes, set pm.max_requests
to 500
to 800
might work.
PHP-FPM may be creating too many child processes on a traffic spike, it will factually initiate the starting of out of memory. Very bad – Google bot will not crawl.
Without going in to much fuss, this should be safe for 4GB :
1 2 3 4 5 6 7 | pm = ondemand pm.max_children = 100 pm.start_servers = 10 pm.min_spare_servers = 5 pm.max_spare_servers = 15 pm.process_idle_timeout = 10s; pm.max_requests = 1000 |
If WordPress or whatever web software cache the PHP webpages, with increased traffic load, 4GB will not die. Decrease the pm.max_requests
for fine tuning. pm = on demand
stops that memory sucking with minimal leak. Run my script on random time, run free
to go closer towards a stable value. Now, if free
command’s memory and your calculation from the script gives hugeeeee difference, then there is some other issue. Lower down the values to even 1
to check the issue. This is another set of setting with lower value :
1 2 3 4 5 6 7 | pm = ondemand pm.max_children = 40 pm.start_servers = 15 pm.min_spare_servers = 15 pm.max_spare_servers = 25 pm.process_idle_timeout = 10s; pm.max_requests = 500 |