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.
If you find that you would prefer to avoid tuning and optimization for PHP, you could consider using something such as the LSAPI handler which provides comparable performance to FPM:
How to switch from PHP-FPM to the LSAPI handler
Max Children
Max Children defines the maximum number of PHP-FPM processes that are allowed to spawn. In the cPanel context, the max_children directive is applied on a per-domain basis. So a max_children setting of 5 means that only 5 processes can spawn to serve requests for that particular domain.
The purpose of the max_children limit is to provide a way to help limit the amount of resources used by PHP FPM in an attempt to prevent it from using too many resources and causing server-wide instability or crashes. It would be more ideal on a shared server for a single site to hit a limit and experience a 503 error rather than that site using all of the resources on the server and cause all sites to crash.
The cPanel default is 5:
root@server [~]# egrep max_children /opt/cpanel/ea-php##/root/etc/php-fpm.d/domain.name.conf
pm.max_children = 5
There isn’t a standardized way to determine the Max Children directive as it depends on numerous factors, however, the following formula could be used as a starting point:
Max Children = (Total Ram - (Used Ram + Buffer)) / (Memory per PHP process)
We can get the RAM information from WHM at WHM Home »Server Status »Server Information
Note:
- 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 to PHP-FPM
A detailed explanation of the Max Request Workers configuration for Apache is out of the scope of this guide, but another more in-depth guide can be found here:
Tuning MaxRequestWorkers for Apache
Apache's Max Request Workers configuration is a very similar concept to Max Children for FPM.
For each request that comes in Apache will assign a worker to that request. The Apache workers then send the request to a PHP FPM process.
If the combined total of the Max Children values for all domains on the server is larger than the Apache Max Request Workers value, it is possible for the Max Request Workers configuration to create a bottleneck. You may need to check Apache's configuration and tune it to achieve the full performance that your server is capable of.
Apache's Max Request Workers can be changed by going to WHM Home » Service Configuration » Apache Configuration » Global Configuration -> Max Request Workers
Max Requests
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-php##/root/etc/php-fpm.d/domain.name.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-php##/root/etc/php-fpm.d/domain.name.conf
pm.process_idle_timeout = 10
The above option is available because the process manager is configured ondemand
:
root@server [~]# egrep ondemand /opt/cpanel/ea-php##/root/etc/php-fpm.d/domain.name.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.
Making the Edit
System PHP FPM
- Login to WHM as the root user
- Navigate to: Home »Software »MultiPHP Manager
- Click on the "System PHP FPM Configuration" tab
Individual Pool
- Login to WHM as the root user
- Navigate to: Home »Software »MultiPHP Manager
- Find your desired domain in the table at the bottom
- Enable the PHP FPM toggle to turn FPM on
- Click the "Edit PHP-FPM" link