Better PHP-FPM configuration for 4gb ram?

Asked

Viewed 2,870 times

4

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

3.75 GB RAM 2 CORE

On my server I only have PHP-fpm,NGINX and an ftp server connected, Mysql is on a 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 peak time starts Hildren can not stand and server starts to get slow and drops from the timeout.

Can anyone help me optimize these settings ?

  • I know it’s been a while, but I would like to comment on something important, often you can even make a great setup, but the problem is in the web-application and not in the server itself. Many applications like Wordpress have a high consumption. However CMS like wordpress has plugins for caching, and even if it is not a CMS, if it is an application of yours it is likely to use a framework, so you can look for a PHP solution for caching both front and back :)

1 answer

1

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

It means that,

pm.max_children * memory_limit ~= Quantity maxim 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.2Gb, being a machine shared 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 request capacity that this server will receive.

To find out how much memory your request is spending you 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

Remember 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 for a child process of php-fpm to receive more requests before it is restarted and reduce the pm.process_idle_timeout to 5 seconds (5s) so that the process does not stay so long idle increasing the ability to obtain requests a little.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.