Tags for this FAQ item:
CentOS Debian Linux NGinx PHP

Help us by rating this article!

Rated 5 stars, based on 3 votes

Determining the correct number of child processes for PHP-FPM on NGinx

Last updated: 19/01/2016

If you are using a NGinx webserver with PHP5-FPM (or PHP-FPM) most likely you will have to adjust the out of the box settings of php-fpm. If your php5-fpm.log file contains the following error:

[26-Mar-2015 10:10:04] WARNING: [pool www] server reached pm.max_children setting (35), consider raising it
[26-Mar-2015 12:04:52] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers)

This means you do not have enough processes available for php-fpm.

Based on your realtime logging data, it is easy to determine the correct settings for the php-fpm configuration on your server. The exact settings are based on the memory available to your cloud server or virtual server, but be aware that you have to take the other processes also into account, they also nibble on those resources (example: MySQL). Never simply use 100% memory available in the virtual machine!

The following steps need to be adjusted in the /etc/php5/fpm/pool.d/www.conf file (typical location on Debian although the path may vary):

pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 4
pm.max_spare_servers = 8

To find the correct values, you will have to measure how much memory a proces consumes. Be warned though this is only possible on a server where the website has a stable memory usage per proces, in other words for a shared hosting environment with different websites and different developped code the memory usage per proces will seriously fluctuate!
The following line (executed in shell) will give you valuable output:

ps -ylC php5-fpm --sort:rss

The name php5-fpm matches your process name. If it possible it is named php-fpm on your server.

The column RSS contains the average memory usage in kilobytes per process. 

S   UID   PID  PPID  C PRI  NI   RSS    SZ WCHAN  TTY          TIME CMD
S     0 24439     1  0  80   0  6364 57236 -      ?        00:00:00 php5-fpm
S    33 24701 24439  2  80   0 61588 63335 -      ?        00:04:07 php5-fpm
S    33 25319 24439  2  80   0 61620 63314 -      ?        00:02:35 php5-fpm

In this example 61588 kilobytes or converted about +- 60 Mb per process.

Another option is to get the average of one php5-fpm process:

ps --no-headers -o "rss,cmd" -C php5-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"Mb") }'

To get the correct value for the number of pm.max_children we will have to check how much memory our cloud or vps server can use for the php-fpm process and divide that total by the average size in Mb of our just checked process.

An example: if our cloud server has 4 GB RAM and a MySQL database service is running as well that consumes at least 1 GB our best aim is to get 4 - 1 - 0,5 (marge) GB = 2,5 GB RAM or 2560 Mb.

pm.max_children brings us to 2560 Mb / 60 Mb = 42 max_children

We have made the following changes in our www.conf file in the php5-fpm pool:

pm.max_children = 40
pm.start_servers = 15
pm.min_spare_servers = 15
pm.max_spare_servers = 25
pm.max_requests = 500

Restart the php5-fpm service and see if the server behaves in a correct manner and allocates memory as configured.