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
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 :
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.
There are 3 types of Process Manager
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.
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 :
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
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.