Best PHP-FPM Configuration – Easy and Simple Calculation. PHP-FPM is a FastCGI Process Manager an alternative for PHP. PHP-FPM is highly configurable by adjusting the pool settings easily for different users using different PHP versions and many more.
In this guide you are going to learn the best PHP-FPM configuration method using some simple calculation to prevent some errors listed below.
1. server reached pm.max_children setting, consider raising it 2. seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers)
Step 1: Server Specifications
First, note down your server specifications. Your CPU cores and RAM size.
For example, in this guide let’s assume you have 2vCPU and 16 GB RAM.
Now, determine how much memory you can allot for PHP processing. We can assume around 10GB RAM out of 16 GB RAM we can allot for PHP processing.
Step 2: How much RAM Each PHP-FPM process Consuming
You can execute the following command to check the average memory consumed for the running PHP-FPM processes.
ps -ylC php-fpm7.4 --sort:rss
Replace the PHP version with the one that is used for your application.
You can will get an output similar to the one below.
Take note of the values below the RSS column. This is the memory used by each of your PHP-FPM process.
Here we have about 20MB RAM is used in average by each PHP-FPM child process.
Step 3: Calculate Values for PHP-FPM Children
By default the
pm value will be set to
dynamic, so you will need to configure the minimum and maximum spare servers and also the maximum children.
We will change the
pm value to
ondemand to the spare servers will get created automatically according to the demand.
pm = ondemand
Now we can calculate how much child process can be created for the application to run according to the server specifications.
As we are about to allot 10GB RAM for PHP-FPM processing. By executing the command in previous step we found an average value of 20MB consumed by each child.
Maximum RAM can be used by PHP-FPM / RAM consumed by each child = Maximum children value for PHP-FPM
So 10000/20 = 500. You can use upto 500 PHP-FPM children according to your server specifications. So your PHP-FPM configuration should be…
pm.max_children = 500
These are the 2 values that needs to be adjusted for PHP-FPM to run smooth on your server.
You can also configure maximum requests to 500 by just uncommenting the
pm.max_requests. This will be useful if there are any memory leaks in any third party libraries.
The final configuration should be like this.
pm = ondemand pm.max_children = 500 pm.max_requests = 500
Prepare yourself for a role working as an Information Technology Professional with Linux operating system
Now you have learned how to configure PHP-FPM values according to your server specifications with simple calculations.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.