Low PHP-FPM max_children value causes Website Slow to Response + CloudFlare 524 Error


virtual-private-servers Low PHP-FPM max_children value causes Website Slow to Response + CloudFlare 524 Error apache server linux php

virtual-private-servers

The cloudflare 524 error means that a TCP/IP connection has been established to your server but the server fails to response with HTTP code/status within a time frame, usually 100 seconds before cloudflare sends the 524 code to the visitor.

The websites on the server are slow to respond – sometimes the mouse pointer are spinning and spinning – lots of visits are dropped. Usually, restarting the apache2 server (via service apache2 restart) if you are on prefork mode, or restarting the PHP-FPM (using service php-fpm restart) if you are on the mpm_event or mpm_worker.

Opening the PHP log located at /var/log/php7.x-fpm.log and I found the log:

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

This is due to the max_children value set too low which limits the number of PHP-FPM process (php-fpm: pool www). And this lags the website if there are many visits to your site at the same time. PHP-FPM will limit to the number of spawns to max_children. Each process will handle the requests one by one.

The default value for pm.max_children is set to 5 – and if you are on the mode of ondemand, you can estimate this value by using your available RAM and the maximum RAM one PHP process can eat. For example, given a 1 GB RAM server, if a PHP program has set_memory_limit to 256MB, you can set max_children to 1GB/256MB=4. Don’t worry you need spare RAM to do other OS tasks, as you will also have the SWAP on – which usually runs on high speed SSD.

You can run service php7.2-fpm status (replace your installed PHP version e.g. 7.2) to view the status (how many PHP has been spawned) of the PHP-FPM.

* php7.2-fpm.service – The PHP 7.2 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-05-14 19:07:03 UTC; 1h 53min ago
Docs: man:php-fpm7.2(8)
Main PID: 31847 (php-fpm7.2)
Status: “Processes active: 0, idle: 1, Requests: 3385, slow: 0, Traffic: 0.3req/sec”
Tasks: 2 (limit: 4915)
CGroup: /system.slice/php7.2-fpm.service
|- 9222 php-fpm: pool www
`-31847 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)

May 14 19:07:03 uploadbeta systemd[1]: Starting The PHP 7.2 FastCGI Process Manager…
May 14 19:07:03 uploadbeta systemd[1]: Started The PHP 7.2 FastCGI Process Manager.

Recommended PHP-FPM settings for ondemand

If your PHP-FPM is set to ondemand, you will need to configure three settings in /etc/php/7.2/fpm/pool.d/www.conf

  • pm.max_children: the maximum number of children that can be alive at the same time.
  • pm.process_idle_time: The number of seconds after which an idle process will be killed. I set it to 6 seconds.
  • pm.max_requests: 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. I set it to 1000.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
676 words
Last Post: Big Data Case Study: TV Usage and Power Consumption
Next Post: How to Start a Podcast on WordPress?

The Permanent URL is: Low PHP-FPM max_children value causes Website Slow to Response + CloudFlare 524 Error

Leave a Reply