52

I have Nginx + php5-fpm. Several times per hour my website stucks and in logfile I see the following:

WARNING: [pool www] server reached pm.max_children setting (5), consider raising it.

/etc/php5/fpm/pool.d/www.conf file contains the following configuration:

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

Server: AMD Opteron™ 3280, Octo-Core, 8x 2.4 GHz, 16 GB DIMM (DDR3).

I have no idea what numbers should I put in www.conf file for this server. Can me help somebody? Thanks

50

There are many possible reasons why your PHP-FPM would reach the max_children. Most common ones are:

  • A lot of parallel requests from your clients
  • Slow execution of the PHP scripts
  • Very low setting of the max_children

Looking at the specs of your machine, assuming there is nothing else than PHP+Nginx running, I think you could set it much higher than 5. You say you have 8 Cores, usually Nginx needs much less CPU than PHP, so with 5 children you will probably never be able to use all of them. I'm usually setting it to something like the number of cores x 2 or number of cores x 4, depending on the memory consumption of your PHP scripts.

  • It's a dating website with php chat and users send millions of instant messages. Last night when I had ~300 users online, the command netstat -an |grep 80 |wc - result was almost 400. – user1821484 Feb 16 '13 at 15:50
  • 1
    I actually have a similar dating website. I think you definitely need more PHP children. My configuration is as following: 20k concurrent online users, 12 PHP machines with 8 Cores each, 32 workers on each of the PHP machines. That works pretty well. You should also look at how much free memory you have, I suspect you have some, unless your PHP is huge. If you have free memory, why not use it for some more workers? – replay Feb 16 '13 at 15:51
  • @user1821484 This is correct; pm.max_children is much too low. A value of 10 is reasonable for a small VPS server with 1GB of RAM; you have a much larger server. Raise this value until you stop receiving the errors, then raise it again in case you get a traffic spike. – Michael Hampton Feb 16 '13 at 19:36
  • Thanks for answers. I increased pm.max_children setting to 10 and now I started to get this error: WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 0 idle, and 8 total children. Can somebody suggest me what I need to increase? Thanks. – user1821484 Feb 17 '13 at 17:12
  • cores x 2 or cores x 4 sounds really low to me. I have a machine with 8 cores and 8GB RAM with pm.max_children=48 and we still get the server reached pm.max_children warnings. This machine is nowhere near its CPU or memory limits. A better approach I think would be to optimize this setting in conjunction with corresponding nginx (or apache) settings and choose a value after determining how much memory each PHP process consumes. – S. Imp Jan 24 '19 at 2:29
22

I found that by setting the pm.max_requests value (which is commented out by default) helped in fixing these errors. This setting forces child requests to respawn after executing a certain number of requests and can be helpful if there are memory leaks somewhere in your code or 3rd party libs.

In /etc/php-fpm.d/www.conf:

pm.max_requests = 500
16

This link may be useful - it explains how to calculate the number of child processes based on the amount of memory on the system:

Adjusting child processes for PHP-FPM (Nginx)

Problem:

The following warning message appears in the logs:

[26-Jul-2012 09:49:59] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 8 idle, and 58 total children
[26-Jul-2012 09:50:00] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it

It means that there are not enough PHP-FPM processes.

Solution:

We need to calculate and change these values based on the amount of memory on the system:

/etc/php-fpm.d/www.conf

pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
  • the following command will help us to determine the memory used by each (PHP-FPM) child process:
ps -ylC php-fpm --sort:rss

Additional note: Sometimes you need to specify the exact version of php-fpm you're using. You can use ps -eo comm | grep php-fpm to find the correct command name. This could be php-fpm7.4 for example.

The RSS column shows non-swapped physical memory usage by PHP-FPM processes in kilo Bytes.

On an average each PHP-FPM process took ~75MB of RAM on my machine.

Appropriate value for pm.max_children can be calculated as:

pm.max_children = Total RAM dedicated to the web server / Max child process size - in my case it was 85MB

The server has 8GB of RAM, so:

pm.max_children = 6144MB / 85MB = 72

I left some memory for the system to breathe. You need to take into account any other services running on the machine while calculating memory usage.

I’ve changed the settings as follows:

pm.max_children = 70
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 35
pm.max_requests = 500

Please note that very high values does not mean necessarily anything good.

You can check an average memory usage by single PHP-FPM process with this handy command:

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

You can use the same steps above to calculate the value for MaxClients for Apache web server - just substitute the php-fpm with httpd.

  • 15
    This answer will be useless once that link goes away. – miken32 Feb 9 '17 at 17:19
  • 5
    Basically, the link says... Appropriate value for pm.max_children can be calculated as: pm.max_children = Total RAM dedicated to the web server / Max child process size – jaywhy13 May 1 '17 at 15:48
  • This answer will not be useless, as long as we have that lifesaver of archive.org – Kamafeather Nov 18 '20 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.