Tags pour la question fréquent:
CentOS Debian Linux NGinx PHP

Cet article a-t-il répondu à votre question?

Score de 4, basé sur 9 votes

Déterminer le nombre de processes valide pour PHP-FPM sur NGinx

Dernière mise à jour: 07/06/2018

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

[26-Mar-2018 10:10:04] WARNING: [pool www] server reached pm.max_children setting (35), consider raising it
[26-Mar-2018 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, but be aware that you also have to take the other processes into account, they also nibble on those resources (examples: MariaDB, MySQL, Redis). Never simply use 100% memory available in the cloud server!

The following steps need to be adjusted in the /etc/php/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 aware, this is only possible on a server where the website has a stable memory usage per process, 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 php-fpm --sort:rss

The name php-fpm matches your process name. If it possible it has a different name on your server, it depends on the PHP packages used.

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 php-fpm
S    33 24701 24439  2  80   0 61588 63335 -      ?        00:04:07 php-fpm
S    33 25319 24439  2  80   0 61620 63314 -      ?        00:02:35 php-fpm

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

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

ps --no-headers -o "rss,cmd" -C php-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 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 MariaDB 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 php-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 php-fpm service and see if the server behaves in a correct manner and allocates memory as configured.