In this article, were gonna improve PHP-FPM performance using upstream and nginx.

Let’s start with setting up the PHP side of it first. Find the following file first: /etc/php/7.2/fpm/pool.d/www.conf

; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[www]

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific IPv4 address on
;                            a specific port;
;   '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /run/php/php7.2-www.sock

Note that the pool name [www] and the listen (listen = /run/php/php7.2-www.sock) directive should be changed once you made a copy of the pool configuration.

For this instance were gonna make 2 pools in our configuration.

    cd /etc/php/7.2/fpm/pool.d/
    cp www.conf www-pool1.conf
    cp www.conf www-pool2.conf
    #delete the original pool
    rm -rf www.conf

So in the 2 pools the configuration should be updated different to each other. Like for pool names [www-pool1.conf] and [www-pool2.conf]. Listening directive should be (listen = /run/php/php7.2-pool1.sock) and (listen = /run/php/php7.2-pool2.sock).

Now that’s done, let’s move to the Nginx side of our application. In your site configuration make sure to add on top, before any of the server declarations:

upstream php-fpm {    
    server unix:/run/php/php7.2-pool1.sock max_fails=3 fail_timeout=30;
    server unix:/run/php/php7.2-pool2.sock max_fails=3 fail_timeout=30;
}

And then replace your fastcgi_pass unix:/var/run/php7.2-fpm.sock; with:

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        #fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_pass php-fpm;
        fastcgi_param CI_ENV 'development';
    }

Test nginx configuration first before reloading the service and your site should have slightly more redundancy.

    nginx -t
    #Once successful, reload both services
    service php7.2-fpm reload && sudo service nginx reload

By default your upstream pool configuration will use the round robin method in selecting which pool of php process to pass.

.