Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!
cPanelResources

Tutorial PHP-FPM Performance Tuning Basics

Instructions on how to tune PHP-FPM performance directives.

Tags:
  1. cPanelResources
    Overview
    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.

    Max Children
    The cPanel default is 5:

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

    Code:
    Total Max Processes = (Total Ram - (Used Ram + Buffer)) / (Memory per PHP process)
    
    We can get the RAM information from WHM:

    Code:
    Home »Server Status »Server Information
    
    Please 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:

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

    Code:
    2.62 = (1838 - (572 + 998))/100
    
    The above is saying, if we increase MaxChildren to 100 we can spect 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 MaxChildren to use in the formula, and then confirm is not going to consume all of the free RAM, in this case, I picked 100 MaxChildren as an example.

    Apache MaxRequestWorkers in relation with PHP-FPM pools
    We are not going to cover in detail in this post what "MaxRequestWorkers" is but we need to mention it because it does not matter how well tuned PHP-FPM's MaxChildren is configured if Apache is not able to serve those requests coming from it.

    Just as PHP-FPM's "MaxClient" setting, Apache's "MaxRequestWorkers" determines how many requests are going to be accepted. The default configuration setting is 150, which is usually enough for most environments.

    Apache's "MaxRequestWorkers" can be changed by going to WHM:
    Code:
    Home » Service Configuration » Apache Configuration » Global Configuration ->MaxRequestWorkers
    
    The second directive we are going to look at is MaxRequests, which controls how many requests are served by a process (MaxChildren) before killing and spawning a new process.

    Max Requests
    By default cPanel establishes a number of 20:

    Code:
    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 use by the process and start a new one, however, if the problem is constant RAM usage is going to come back up.

    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:

    Code:
    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 “ondemand”:

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

    Code:
    Home »Software »MultiPHP Manager
    
    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.
    sierrablue and eva2000 like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice