Optimizing PHP-FPM Performance For High Load

We have setup a WordPress site which use PHP and running on GCE with Nginx, its time to tweak some configurations for better performance. If you haven’t got your site up, read this How to host WordPress with Google Cloud Free

What is PHP-FPM?

PHP-FPM is PHP FastCGI Process Manager. It is a FastCGI daemon that allows a website to handle high load. But sometimes this service itself causes high load.

To debug the issue, the first thing we did is to checking the PHP-FPM error logs, which is available in a location such as “/var/log/php7.4-fpm.log”.

The location for the error logs varies based on the installation path and PHP version. 

Use command below to open the log.

sudo nano /var/log/php7.4-fpm.log 

While checking the error log, we found some warnings like these.

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

This message means that the PHP-FPM pool is very busy and is unable to handle the high traffic with the existing configuration parameters.

PHP-FPM can lead to high server load due to many reasons such as :

  • Site plugins like page caching, analytic, form
  • Customizing theme with builder for WordPress
  • Too many processes running due to high traffic or attacks
  • A related service such as MySQL or web server being abused
  • PHP-FPM configuration is not properly mapped to available server resources
  • Poorly coded or buggy applications abusing the server resources

How To Optimize For High Load?

We can restart the server or PHP-FPM service for an immediate fix to the high load. But simply restarting is only a band-aid solution, and you will soon see the load spiking again.

So here are 2 methods to optimize.

  1. Change Process Manager
  2. Tweak PHP-FPM Configurations

1. Change Process Manager

There are 3 types of Process Manager

  • Static – A fixed number (pm.max_children) of child processes
  • Dynamic – The number of child processes are set dynamically based on the PHP-FPM parameters in conf file.
  • Ondemand – No children are created at startup. Children will be forked when new requests will connect. This will cause some delay on site first load

The performance of the service varies based on the type.

Normally we will use “Dynamic” type and tweak the parameters.

You can use “Static” type to allocate fix resources of each pool when you have multiple site on same server.

2. Tweak PHP-FPM Configurations

PHP-FPM has a lot of configuration parameters which determine the way it performs. These parameters have to be determined based on available server resources such as CPU and Memory.

Here are the main parameters to tweak with :

  • pm.max_children – This is used to set the maximum processes allowed
  • pm.start_servers – The number of child processes created on startup 
  • pm.min_spare_servers – Defines the minimum number of idle processes
  • pm.max_spare_servers – Sets the maximum number of idle server processes
  • pm.process_idle_timeout – The number of seconds an idle process will be alive
  • pm.max_requests – This sets the execution time of each child process and is used to curb memory leaks.

There are difference values to determine the pm.max_children, depend on the server resources, website traffic and content of the sites with the formula

pm.max_children = Total Memory Available / (50MB~90MB)

In our example, we running site on GCP with f1-micro (1 vCPU, 0.6 GB memory). Our server running Nginx + MySQL do use some memory, so we calculate with

450MB / 50MB = 9 pm.max_children

We will configure the parameters below to match the pm.max_children. Use the command below to open the conf file

 

sudo nano /etc/php/7.4/fpm/pool.d/www.conf 

Change the parameters value as these, remember to remove in front of parameters if applicable. Hit F6 or ctrl+w to search. After done, ctrl+x then Y to save.

pm = dynamic
pm.max_children = 9
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500 

Test the validity of your php-fpm configuration syntax

sudo php-fpm7.4 -t 

You will see this if done properly

NOTICE: configuration file /etc/php/7.4/fpm/php-fpm.conf test is successful 

Now restart php7.4-fpm

sudo service php7.4-fpm restart 

Conclusion

Tweaking PHP-FPM for best performance, is not just a template fix. The configuration and parameters for the service would vary based on the server needs and resources. It is an on-going process, you have to tweak it for higher traffic!

Monitor the server performance and perform periodic server tweaks to ensure that server load stays normal.

Leave a Comment

Scroll to Top