Optimizing PHP-FPM for Better Performance

Introduction

If you are here, you probably hosting a PHP-based website or web application, The good news is you are reading the correct article.

Optimizing PHP-FPM can have deep impacts on your Website or Web application performance and efficiency, This is because PHP-FPM (PHP FastCGI) is a server module that is responsible for handling PHP requests mainly when you are using NGINX as your webserver. By optimizing PHP-FPM you can achieve:

  • reduce the resource usage
  • reduce the response time for PHP requests
  • handle more requests concurrently
  • prevent performance degradation
  • improve the reliability of your web server over time

All that will be leading to a faster and more efficient website or web application.

Note that all of the steps in this article are in an environment containing: PHP 8.1.14

Optimize PHP-FPM

Optimizing PHP-FPM is not rocket science if you know the variables and their purpose. So we are going to briefly explain the most important ones:

The Process manager types (pm)

The process manager configuration in PHP-FPM (pm) refers to the setting that controls how PHP-FPM manages the worker processes that handle PHP requests, These settings stated how many workers should be deployed or created to manage the PHP requests and how they should respond to changes in demand.

We have Three pm types:

Ondemand

This process manager creates a new process for each request that is received by PHP-FPM. This ensures that every request is processed by a separate process, which can be useful for reducing the risk of one request affecting another, but it can also be resource-intensive and slow down the server.

Dynamic

This process manager uses a pool of worker processes to handle incoming requests. The number of worker processes can be configured and can be adjusted based on the current demand. This process manager is more efficient than the on-demand process manager, as it uses fewer resources and can handle requests more quickly, but it may not provide the same level of isolation as the on-demand process manager.

Static

The static option as it’s clear from the name Will fix the number for all of the sub-options of the pm configurations and it will not adopt in any circumstances.

Most of the tips and configurations provided in this article are valid when the Process Manager type (pm) is set to dynamic, so it is assumed that the pm is set to dynamic.

Tuning the Configurations

To access the main configuration file of the PHP-FPM on an RHEL based OS you need to execute the following command:

vim /etc/opt/remi/php81/php-fpm.d/www.conf

The configuration file location could be different based on your Linux Distro or even based on your installation method.
After selecting your pm configuration based on the type and load of your website or web application, you can tweak the following variables to get the most out of the PHP-FPM.

pm.max_children

This setting determines the maximum number of child processes that can run simultaneously. Setting it too low can cause PHP-FPM to spawn new processes too frequently, leading to overhead.

pm.max_children = 50

pm.start_servers

The number of worker processes to start when PHP-FPM is launched. This option is only applicable when using the dynamic process manager.

We are going to set it to:

pm.start_servers = 10

pm.min_spare_servers & pm.max_spare_servers

The minimum and maximum number of idle worker processes to keep available at all times. This option is only applicable when using the “dynamic” process manager.

pm.min_spare_servers = 5
pm.max_spare_servers = 15

pm.max_requests

The maximum number of requests that each worker process should handle before it is terminated and replaced with a new process.

We are going to uncomment it and set it to:

pm.max_requests = 500

Handling Unexpected behavior

These three options in a PHP-FPM configuration file are related to the management of worker processes in the event of an error or unexpected behavior.

  • emergency_restart_threshold: This setting specifies the number of worker processes that need to exit in a short period of time before PHP-FPM will trigger an emergency restart. An emergency restart will terminate all worker processes and restart PHP-FPM, which can help resolve problems caused by a malfunctioning worker process.
  • emergency_restart_interval: This setting specifies the interval of time in which the emergency_restart_threshold must be exceeded before an emergency restart is triggered. In this example, the interval is set to 1 minute.
  • process_control_timeout: This setting specifies the amount of time PHP-FPM will wait for a worker process to exit gracefully before forcing it to terminate. If a worker process does not exit within the specified time, PHP-FPM will kill it and start a new worker process to take its place. This setting helps to prevent worker processes from hanging or slowing down the system, and helps to ensure the overall stability of PHP-FPM.

We are going to set them like below:

[global]
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10s

PHP Monitoring

Make sure to use PHP monitoring to decrease bottlenecks and the overall performance of your application and server.

Conclusion

Optimizing PHP-FPM is an important step in ensuring the performance and stability of your web server. By properly configuring PHP-FPM, you can control the number of worker processes, manage resource utilization, and improve response times for PHP requests.

In addition to optimizing PHP-FPM, it’s also important to optimize your PHP configuration file (php.ini) and your web server software (such as Nginx). This can involve setting appropriate values for memory limits, enabling caching, and tweaking other settings to match the specific requirements of your web applications.

To get the most out of your web server, it’s recommended to take a comprehensive approach to optimization, which includes optimizing PHP-FPM, PHP, and Nginx. There are many online resources available that can help you learn more about optimizing these components, and it’s always a good idea to stay up-to-date with the latest best practices and techniques. So, take the time to read more about optimizing PHP and Nginx, and start maximizing the performance of your web server today!

Leave a Reply

Your email address will not be published. Required fields are marked *