How to Optimize PHP-FPM?


Reading Time: 3 mins

What is PHP-FPM?

PHP-FPM stands for PHP-FastCGI Process Manager, primarily focused to manage the heavy loaded sites is an alternative to FastCGI daemon for PHP. For each of the web requests, PHP-FPM spins up one or more (child) processes via maintaining pools. It is faster than the conventional CGI- based methods by handling more than a single request by spinning up too many child processes at a time.  

The following are the basic attributes which will be presented in the PHP-FPM by default. You can increase the values of these basic attributes to the desired number at required times such as during high-traffic days. Need to install the PHP server on your system? To know the installation process of the PHP, visit our blog post on Installing PHP On Ubuntu 18.04

pm.max_children

This helps to restrict the number of concurrent child processes to be handled and it is not a mandatory one. This decides the max. no. of child processes to be created at the time when pm is set to dynamic and number of child processes at the time when pm is set to static.

pm.max_children = 10

pm.start_servers

It is applicable only when the pm is set to dynamic and it decides the number of child processes to be created on startup. 

Default Value = min_spare_server+(max_spare_server-min_spare_server)/2
pm.start_servers = 4

pm.min_spare_servers

This decides the preferred minimum number of idle server processes. It is applicable only when the pm is set to dynamic and a mandatory one. 

pm.min_spare_servers = 2

pm.max_spare_servers

This decides the preferred maximum number of idle server processes. It is applicable only when the pm is set to dynamic and a mandatory one. 

pm.max_spare_servers = 4

pm.max_requests

This decides the maximum number of requests to be executed before respawning by the child processes. To set endless processing of request fix 0. 

pm.max_requests = 1000

Optimizing PHP-FPM:

It is possible to use the default file called www.conf but it is applicable only for a single website. To configure PHP-FPM for multiple websites, follow the below procedures.
Let’s start by creating the required user and its group. To create the user group, example.com:

sudo groupadd example.com.conf;

To create example.com user for the created group:

sudo useradd -g example.com.conf example.com.conf;

Note: If the names of the created sites end with .conf extension, it makes easier for PHP to be automatically located in the global configuration except the core domain/server name. 
Now, using the path cd /etc/php/7.2/fpm/pool.d/  create a new file for the new website, sample.com using the command:

sudo nano /etc/ php7.2/fpm/pool.d/example.com.conf

The file will be seen as something like this:

sudo nano /etc/ php7.2/fpm/pool.d/example.com.conf
[example.com.conf]
user = example.com.conf
group =example.com.conf
request_slowlog_timeout = 5s
listen = /var/run/php7.2-fpm-example.com.conf.sock
listen.owner = www-data
listen.group = www-data
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm.max_children = 10
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 200
chdir = /
request_terminate_timeout = 120s

Now, you have to include the created file into the Nginx server block. To know more, visit our blog on Setting Up Nginx Server Blocks for Magento
To save the carried out changes, restart the PHP-FPM soon after the configuration using the command:

sudo service php7.2-fpm restart

To verify the running status of the pool, use the command:

ps aux  |grep example.com.conf

And the output will be shown as,

example.com.conf   14042 0.0 0.8 133620  4208 ? S 14:45   0:00 php-fpm:pool example.com.conf
example.com.conf  14043 0.0 1.1 133760  5892 ? S 14:45   0:00 php-fpm: pool example.com.conf

 

Thus, we have successfully optimized the PHP-FPM. It is to be specifically noted that the above-given values are not constant and is purely for demonstration purposes. Depending upon  your website’s performance, traffic and resources, you can increase the values as per your requirements. Further, it is to be noted that to attain efficiency,  you need to configure PHP-FPM to manage multiple websites.

2 thoughts on “How to Optimize PHP-FPM?”

  1. Hi,
    PHP-fpm having default configuration and domain-specific PHP fpm file in location /etc/php/7.2/fpm/pool.d/
    If http://www.conf having smaller values than domain-fpm.conf … which one will be effective for the domain?
    like – pm.max_children = 5 ( in http://www.conf )
    but the domain.conf having value –
    pm.max_children = 20

    1. The www.conf is the default one and its applicable for basic sites with low traffics. These values will add depends on the site traffic, if you want to set it for high traffic sites then you have to adjust the values on respective blocks either with www.conf or domain.conf and make sure wherever you changed you have to add the respective socket path in Nginx server block, Then only these values will apply for a respective domain name.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top