PHP–FPM is a mighty beast, but throw too much it’s way and it will crash and burn. If you’re running your application via a PHP socket, consider load balancing the traffic and passing it to multiple PHP processes. Here you can learn how to improve PHP-FPM performance using upstream and nginx.
In order to do that you will first have to decide how many processes your server can afford. I tend to go with one process per processor unless I’m running it on t2.micro in which case I put two. I’d suggest you check how the CPU of your application responds to more ports and adjust accordingly. For the purpose of this example I will be load balancing PHP-FPM on two ports.
Let’s start with setting up the PHP side of it first. Find the following file first /etc/php5/fpm/pool.d/www.conf
here you will see something along these lines:
[www] ; Unix user/group of processes ; 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 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses on a ; specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. listen = /var/run/php5-fpm.sock
copy the file into /etc/php5/fpm/www-common and remove the lines I marked in red from it. The rest should stay the same. This will be a shared file that you will include in configuration for each of the ports.
Now remove /etc/php5/fpm/pool.d/www.conf alltogether
Once that’s done, create as many configuration files for each of the ports you will be running that will follow this format. In my example I will be creating: /etc/php5/fpm/pool.d/www-ip9001.conf and /etc/php5/fpm/pool.d/www-ip9002.conf with following content
[www-900X] listen = 127.0.0.1:900X include=/etc/php5/fpm/www-common.conf
Where X is the number of port, i.e. 9001 and 9002 in the example above.
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 php5-fpm { server localhost:9001 max_fails=3 fail_timeout=30; server localhost:9002 max_fails=3 fail_timeout=30; }
and then replace your fastcgi_pass unix:/var/run/php5-fpm.sock; with:
fastcgi_pass php5-fpm;
Now you can restart php and nginx and your site should have slightly more redundancy.
sudo service php5-fpm reload && sudo service nginx reload
very good 🙂
thanks