This resource provides instructions on how to tune performance directives when PHP-FPM is installed and enabled as a PHP handler in cPanel & WHM. Background information on how PHP-FPM is implemented with cPanel & WHM is available on our PHP-FPM documentation.
This document provides valuable information about the main PHP-FPM pool directives, Max Children, Max Requests, and Process Idle Timeout, as well as additional configuration values.
Let us start with the most conflicting one which is Max Children it defines the number of PHP-FPM processes to spawn to actually serve requests coming from the web server to process the PHP code.
The cPanel default is 5:
root@server [~]# egrep max_children /opt/cpanel/ea-php56/root/etc/php-fpm.d/cptech1.local.conf
pm.max_children = 5
There isn’t a standardized way to actually determine the Max Children directive as it depends on numerous factors, such as the site’s particular PHP code, however, there is a nice formula to help us be on the safe side:
Total Max Processes = (Total Ram - (Used Ram + Buffer)) / (Memory per PHP process)
We can get the RAM information from WHM at WHM Home »Server Status »Server Information
- The above interface is displaying the RAM usage in bytes so make sure to adjust these values to MegaBytes.
For the purposes of this tutorial, I used the
free command so I can paste the output here:
root@server [~]# free -m
total used free shared buff/cache available
Mem: 1838 572 267 33 998 1014
Applying the formula using the above values would look like this:
2.62 = (1838 - (572 + 998))/100
The above is saying, if we increase Max Children to 100 we can expect each independent PHP process to use 2.62MB of RAM, keep in mind this is per domain per PHP process.
The catch is we need to pre-select a number of Max Children to use in the formula, and then confirm is not going to consume all of the free RAM, in this case, I picked 100 Max Children as an example.
Apache Max Request Workers in relation with PHP-FPM pools
We are not going to cover in detail in this post what Max Request Workers is but we need to mention it because it does not matter how well tuned PHP-FPM's Max Children is configured if Apache is not able to serve those requests coming from it.
Just as PHP-FPM's Max Client setting, Apache's Max Request Workers determines how many requests are going to be accepted. The default configuration setting is 150, which is usually enough for most environments.
Apache's Max Request Workers can be changed by going to WHM Home » Service Configuration » Apache Configuration » Global Configuration -> Max Request Workers
The second directive we are going to look at is Max Requests, which controls how many requests are served by a process (Max Children) before killing and spawning a new process.
By default cPanel establishes a number of 20:
root@server [~]# egrep max_request /opt/cpanel/ea-php56/root/etc/php-fpm.d/cptech1.local.conf
pm.max_requests = 20
According to php.net, this is only useful to avoid memory leaks from 3rd party applications (custom libraries PHP code) which could misbehave and cause memory problems.
For example, you notice a specific site under cPanel account ‘X’ it is consuming excessive amounts of RAM, Max Request will clear out the ram used by the process and start a new one. However, if the problem is constant RAM usage, it will increase again.
Unless you suspect you have an application having memory issues and you suspect is leaking memory, the default value should be fine.
Process Idle Timeout
It defines the number of seconds after which an idle process (process not serving any requests) is set to be killed so it releases CPU time and RAM. The default cPanel value for this is 5 seconds:
root@server [~]# egrep process_idle_timeout /opt/cpanel/ea-php56/root/etc/php-pm.d/cptech1.local.conf
pm.process_idle_timeout = 10
The above option is available because the process manager is configured
root@server [~]# egrep ondemand /opt/cpanel/ea-php56/root/etc/php-fpm.d/cptech1.local.conf
pm = ondemand
Which means the processes are started when they are requested, is not a fixed set of starting processes.
It is important to understand this does not affect PHP’s
max_execution_time which is actually going to stop a running process should the threshold be exceeded.
So how does
process_idle_timeout affect your performance? It depends, if you are getting constant requests (hot traffic sites) then you want to extend this number in increments of 10 seconds and monitoring between increments.
If you do not have any high traffic sites, then a shorter time will help you free those resources to be used elsewhere.
So, how do you modify these directives? Easy enough, just go to WHM Home »Software »MultiPHP Manager and look for the PHP-FPM column and click the "Pool Options" button which is going to pop out a configuration box.
- Please remember this configuration is per domains.