PHP-FPM Tweaking –

  PHP-FPM

Dynamic v On-demand

Good write up here.

I’d use on-demand for low priority and/or low traffic sites, especially if you’re running a mix of sites on the same box.

On-demand:

As mentioned:

pm = ondemand
pm.max_children = 5
pm.process_idle_timeout = 10s
pm.max_requests = 200

Set pm.max_children pretty low for your low priority sites, if it’s a low trsaffic but occasionally busier but want to support it then light it a bit (10 or whatever)

Dynamic:

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 5
pm.max_requests = 200

The usual work should be put into “how many children can you support”. Generally you’d divide your RAM* by the average size of your request, have a look using:

ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep php-fpm
 37.05 Mb php-fpm: pool hoops
 29.26 Mb php-fpm: pool hoops
 24.69 Mb php-fpm: pool hoops
 22.97 Mb php-fpm: pool hoops
 5.72 Mb php-fpm: master process (/etc/php-fpm.conf)
 5.72 Mb php-fpm: pool hoops

Then work out the average with IDK something like

ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep php-fpm |awk '{ total += $1 } END { print total/NR }'

*Now RAM, I don’t divide by amount int he box, just the amount I roughly want to assign to this. e.g. might have 32GB but allocate to caching IDK 8GB to Varnish, 8GB to MySQL, 8GB to whatever, so only 8GB to PHP-FPM. Whatever you want really.

 

The others

pm.start_servers = 5 
pm.min_spare_servers = 5
pm.max_spare_servers = 5

Can’t be arsed really, how many do you want to kick off with, how many do you want in reserve. Really depending on the traffic flow to your site. Are you about to get a rush, maybe increase the spars so they’re there ready and wait.

List of global php-fpm.conf directives

pm string
Choose how the process manager will control the number of child processes. Possible values: static, ondemand, dynamic. This option is mandatory.

static – the number of child processes is fixed (pm.max_children).

ondemand – the processes spawn on demand (when requested, as opposed to dynamic, where pm.start_servers are started when the service is started.

dynamic – the number of child processes is set dynamically based on the following directives: pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers.

pm.max_children int
The number of child processes to be created when pm is set to static and the maximum number of child processes to be created when pm is set to dynamic. This option is mandatory.

This option sets the limit on the number of simultaneous requests that will be served. Equivalent to the ApacheMaxClients directive with mpm_prefork and to the PHP_FCGI_CHILDREN environment variable in the original PHP FastCGI.

pm.start_servers int
The number of child processes created on startup. Used only when pm is set to dynamic. Default Value: min_spare_servers + (max_spare_servers – min_spare_servers) / 2.

pm.min_spare_servers int
The desired minimum number of idle server processes. Used only when pm is set to dynamic. Also mandatory in this case.

pm.max_spare_servers int
The desired maximum number of idle server processes. Used only when pm is set to dynamic. Also mandatory in this case.

pm.process_idle_timeout mixed
The number of seconds after which an idle process will be killed. Used only when pm is set to ondemand. Available units: s(econds)(default), m(inutes), h(ours), or d(ays). Default value: 10s.

pm.max_requests int
The number of requests each child process should execute before respawning. This can be useful to work around memory leaks in 3rd party libraries. For endless request processing specify ‘0’. Equivalent to PHP_FCGI_MAX_REQUESTS. Default value: 0.