As an addition to my previous NGINX articles, today we’ll discuss how to adjust the number of child processes for PHP-FPM to fix the common error of “server reached pm.max_children” in the logs. This is a follow-up to the LEMP Install NGINX PHP7 MySQL on Ubuntu 16.04 Server or LEMP Install NGINX PHP7 MySQL on CentOS 7 / RHEL / Fedora articles. By default, the pm.max_children is set to 5 when using PHP-FPM.
Reasons Why You Reach max_children
The most common reasons why your PHP-FPM would reach the max_children are:
- A lot of concurrent site visitors
- Slow execution of the PHP scripts due to server resources or buggy scripts
- Very low setting of max_children setting in php-fpm config
Depending on where your PHP-FPM logs are stored, try running one of the following commands:
tail -f /var/log/php-fpm/error.log
tail -f /var/log/php7.0-fpm.log
If you see any lines like the one below, then you’ll want to adjust some of these settings:
server reached max_children setting (5), consider raising
Adjusting The max_children Setting
To adjust the settings, you’ll need to find your php-fpm.conf or www.conf depending on what version of PHP-FPM you have installed. In my case, I had to edit /etc/php/7.0/fpm/pool.d/www.conf. You’ll want to look for the following settings and make adjustments based on your server specs:
pm = dynamic
pm.max_children = 25
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 500
A few of these settings are the default, like pm=dymanic. For the pm.max_requests, the setting should be there but commented out with a ; in front of the line. Delete the ; to uncomment the setting.
To get an idea of what to use for the pm.max_children, you can use this calculation:
pm.max_children = Total RAM dedicated to the web server / Max child process size. Remember to leave some RAM for extra services you have running on your system.
Also remember, higher isn’t always better. If you have a bad PHP script, these settings won’t resolve your issues. Tweak these settings and see what works best on your server. After you make the changes, you need to restart your PHP-FPM service. Depending on the name of your service, you can try on of the following:
sudo systemctl restart php-fpm
sudo systemctl restart php7.0-fpm
Have you run into these errors in your logs? If so, what are your server specs and what settings worked best for you?