PHP-FPM (FastCGI Process Manager) efficiently manages PHP processes for web servers. The pm.max_children parameter determines the maximum number of child processes PHP-FPM can run concurrently, directly impacting website performance and stability. This guide will show you how to configure pm.max_children in PHP-FPM and resolve the “server reached pm.max_children setting (5), consider raising it” error.
Understanding the pm.max_children configuration parameter
pm.max_children
is an important parameter in the PHP-FPM configuration that determines the maximum number of child processes that PHP-FPM can run at the same time. This directly affects the concurrency of PHP-FPM and the performance of the website.- Too few child processes can lead to weak performance and increased response times. When the number of child processes is too small, PHP-FPM may not be enough to handle all requests from the user simultaneously. As a result, the site will be slow to respond, take longer to load, and users may experience timeout errors.
- However, too many child processes can also cause system resource problems. Each child process will use a certain amount of memory and CPU resources. If the number of child processes is too high, the system resources will be overloaded and cause performance degradation or even cause the server to crash 503 (Service Unavailable).
- Therefore, to achieve a good balance between performance and system resources, we need to configure and optimize
pm.max_children
to match the traffic and resources available on the server. Adjusting the valuepm.max_children
properly is important to ensure a stable and efficient website.
Assess your site’s traffic and requests
Let’s first take a look at the default configuration of php-fpm:
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
To configure it pm.max_children
optimally, you need to evaluate the traffic and requirements of your site. There are several important factors to consider:
- Traffic: Determines the number of users that visit your website in a given period of time. You can use a traffic analysis tool like Google Analytics to collect the data.
- PHP Requests: Evaluate the number of PHP requests your site receives in the same time period. PHP requests include requests to load pages, process data, and perform other operations on the server.
Calculate and configure to optimize pm.max_children
After gathering the above information, you need to determine the average memory size per PHP-FPM process. The following command can be used to calculate:
ps --no-headers -o "rss,cmd" -C php-fpm8.0 | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
This command will calculate the average memory size of each PHP-FPM process based on the amount of RSS (Resident Set Size) memory of each process. (replace php-fpm8.0 with your php-fpm version)
Next, calculate the total memory consumed by all child processes based on the average memory size and pm.max_children
.
For example, if the average memory size per process is 156M (same as a screenshot from We) and let’s say your server has 2GB of RAM, and the maximum amount of RAM you want to optimally use pm.max_children for php-fpm is 1GB (1024MB), we have:
pm.max_children = 1024MB / 156MB ≈ 6.5
Since pm.max_children must be an integer, you can round up and set it pm.max_children
to 7.
pm.start_servers
, pm.min_spare_servers
, pm.max_spare_servers
: These parameters are related to the number of initially started processes and the number of standby processes. Normally, you can set the following values:
- pm.start_servers: Can keep the value 2.
- pm.min_spare_servers: Can keep the value 1.
- pm.max_spare_servers: Can keep the value 3.
After performing calculations and adjustments, the optimized pm.max_children PHP-FPM configuration should now become:
pm = dynamic
pm.max_children = 7
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
Finally, make sure that the total memory consumed does not exceed the amount of RAM available on the server. Check the server’s system resources to make sure that it has enough RAM to handle child processes and that there is still enough left over for other applications and services like Nginx, and Mysql… to run on the server.
From the collected information and the above calculations, you can conduct configuration to optimize pm.max_children
optimally to meet the traffic and requirements of the website without overloading the system resources. Thereby helping you to fix server error 503 “”server reached pm.max_children setting (5), consider raising it”.”
Configuration Details: pm.start_servers min/max_spare_servers
- pm.start_servers:
- The parameter
pm.start_servers
defines the number of child processes that are initially started when PHP-FPM is started or restarted. - The value of pm.start_servers should be set so that it is sufficient to handle the initial amount of requests without significant delay.
- Over time, pm.start_servers will adjust automatically based on PHP-FPM’s process management model and actual requirements.
- Typically, the starting value for pm.start_servers is between 2 and 8, depending on the volume of requests and the processing capacity of the server.
- The parameter
- pm.min_spare_servers:
pm.min_spare_servers
defines the minimum number of child processes that PHP-FPM maintains while running.- The value of pm.min_spare_servers ensures that PHP-FPM always has enough child processes ready to handle requests.
- Setting a lower value
pm.min_spare_servers
may result in the creation of new child processes when needed, increasing response time. - However, setting the value pm.min_spare_servers too high will result in wasting system resources.
- Typically, the starting value for pm.min_spare_servers is between 1 and 5, and can be adjusted based on server requirements and resources.
- pm.max_spare_servers:
pm.max_spare_servers
defines the maximum number of child processes that PHP-FPM maintains while running.- The value of
pm.max_spare_servers
limits the maximum number of child processes to avoid wasting system resources. - If the number of child processes exceeds pm.max_spare_servers, PHP-FPM will proceed to reduce the number of child processes to free up resources.
- Setting the value
pm.max_spare_servers
too low can result in creating new child processes too often, consuming CPU and memory resources. - Typically, the starting value for pm.max_spare_servers is between 3 and 10, and can be adjusted based on server requirements and resources.
The values for pm.start_servers, pm.min_spare_servers, and pm.max_spare_servers should be carefully adjusted to ensure that there are enough child processes available to handle the request, avoid wasting resources, and ensure good performance. of PHP-FPM.
Configuration and optimization pm.max_children
in PHP-FPM is an important step to ensure the performance and stability of your site. Determining the right value pm.max_children
based on traffic and system resources is necessary. Performance monitoring, using monitoring and logging tools, along with adjusting other parameters, will help you optimize the performance of your PHP-FPM and ensure your website performs well in production environments.