Best PHP-FPM configuration for 4GB ram?

I need some help setting up my PHP-FPM I have EC2 on amazon C4.large

3.75 GB RAM 2 CORE

On my server I just have PHP-fpm, NGINX and an ftp server connected, MySql is on an RDS.

My current php-FPM configuration

pm.max_children = 25
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 5
pm.max_requests = 100
pm.process_idle_timeout = 40
rlimit_files = 131072
rlimit_core = unlimited

When it starts peak time the children can't stand and server starts to slow down and falls from timeout.

Can anyone help me optimize these settings ?

Author: TutiJapa Wada, 2016-07-19

1 answers

Php-fpm separates php processes into subprocesses, each of which is responsible for processing one php request at a time. Using a maximum value of php-FPM child process it is possible to know the maximum value of memory that can be consumed based on the value of memory_limit.

This means that,

Pm.max_children * memory_limit ~ = maximum amount of memory that can be used by a php-FPM pool.

In your case, let's assume that the value of memory_limit is 128M, then we will have:

25 * 128M ~ = 3200M

The maximum amount of memory your php-fpm should use is approximately 3.2 Gb, being a shared machine with the operating system and NGINX, the value would already be close to the limit.

If your php scripts consume less than 128m it is possible to increase the capacity of requests that this server will receive.

To find out how much memory that your request is spending vc can create a log:

 $initialMem = memory_get_usage();
 // ... Tudo aqui
 $finalMem = memory_get_peak_usage();
 error_log('Defina um label ou coloque o nome do arquivo usando __FILE__ '.($finalMem - $initialMem)/1024 . " Kbytes"); //Armazena no log o uso de memória

It is worth remembering that php scripts can modify this value using the init_set function, if available.

ini_set('memory_limit', '2048M');

You can raise the value of pm.max_requests so that a php-fpm child process receives more requests before being restarted, and reduce the pm.process_idle_timeout to 5 seconds (5s) so that the process does not get so much idle time by increasing the ability to get requests a bit.

In a way, this is an estimate, it is necessary to adjust the parameters according to your demand and the characteristic of your application.

 1
Author: LeonanCarvalho, 2018-06-20 12:24:20