Applicable to:
- Plesk for Linux
Symptoms
-
Websites that have PHP handler set as FPM application served by Apache or FPM application served by nginx at Domains > example.com > PHP Settings experience one of the following issues:
-
load slowly
-
not accessible (permanently or periodically) with a 50x error in a web-browser:
PLESK_INFO: 502 Bad Gateway
PLESK_INFO: 503 Service Temporarily Unavailable
PLESK_INFO: 504 Gateway Time-out
Cloudflare 522 timeout error
-
-
The following message appears in the PHP-FPM error logfile
/var/log/plesk-phpXX-fpm/error.log
(where XX is a PHP version used by the websites):CONFIG_TEXT: WARNING: [pool example.com] server reached max_children setting (*), consider raising it
-
The message above is usually accompanied with this error:
CONFIG_TEXT: unable to read what child say: Bad file descriptor (9)
-
-
If nginx is enabled on the server, one of the following error messages may appear in domain's error logfile
/var/www/vhosts/system/example.com/logs/proxy_error_log
:CONFIG_TEXT: [error] 11456#0: *118348 connect() to unix:///var/www/vhosts/system/example.com/php-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: 203.0.113.2, server: example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:///var/www/vhosts/system/example.com/php-fpm.sock:", host: "example.com"
CONFIG_TEXT: [error] 20058#0: *4725139 upstream prematurely closed connection while reading response header from upstream, client: 203.0.113.2, server: example.com, request: "POST /path/to/some/file HTTP/2.0", upstream:
Cause
The PHP-FPM pm.max_children
has been reached by the website(s).
Resolution
Increase the pm.max_children
limit for the affected domains:
-
Connect to the Plesk server via SSH.
-
Find affected domains using the command below:
Note: The number on the left indicates how many times the
pm.max_children
limit has been reached by the website.
If a domain is using a dedicated FPM application and has a custom vhosts location, change the path /var/www/vhosts/system/ in the command below:# grep -r "server reached max_children setting" /var/log/*php*-fpm/error.log /var/www/vhosts/system/*/logs/php-fpm_error.log | cut -d' ' -f5 | tr -d ']' | sort | uniq -c | sort -nr
100 example.com
50 example.net
25 example.org
<...> -
Go to Domains > example.com > PHP Settings > PHP-FPM Settings.
-
Increase the
pm.max_children
value for the domain and apply the changes. Increase this value for all domains listed in step 2.Note: In this example, we are setting
pm.max_children
to 20. To fine tune this setting, visit this KB article.
Other options to increase the pm.max_children
limit:
-
Go to Service Plans > plan_name > PHP Settings tab > PHP-FPM Settings section.
-
Increase the
pm.max_children
value and click Update & Sync.Note: The changes will not be applied to subscriptions if:
- A subscription is not synced with this service plan (Marked with a blue lock in the Subscriptions menu).
- If the permission Hosting performance settings management is granted in service plan settings at Service Plans > plan_name > Permissions tab.
-
Create a
php.ini
file in domain's system directory/var/www/vhosts/system/example.com/conf/
:# touch /var/www/vhosts/system/example.com/conf/php.ini
-
Open the
php.ini
file in any text editor (for example, vi editor) and add the following records:CONFIG_TEXT: [php-fpm-pool-settings]
pm.max_children = 20 -
Save the changes and close the file.
-
Run the following command to apply the changes:
# /usr/local/psa/bin/php_settings -u
Comments
18 comments
Great help and tutorial ! Thanks a lot
Possible to apply this setting to all domains with single file editing? I have lots of domains to be configured 1 by 1
@Sharul Hafiz You can check the article mentioned in Additional Information section - https://support.plesk.com/hc/en-us/articles/115001201949
Keep in mind that increasing of pm.max_children parameter for all domains can affect the server performance significantly. It is better to filter only affected domains in /var/log/plesk-phpXX-fpm/error.log file
===
grep "server reached max_children setting" /var/log/plesk-phpXX-fpm/error.log
===
and adjust max_children just for them.
@Nikolay What is the limit of pm.max_children? let say if I have 10GB free ram.
@Sharul
The setting is purely depends on amount of traffic for the website. Try to double the default value (from 5 to 10) and monitor the site for several days.
If the same error appears again, analyze the time period between reaching MaxChildren. In case the time like several minutes, double the value again (from 10 to 20), in case the error appears once a day, just add one more child to the configured value (20 to 21).
Typically, 20 is more than enough for medium-loaded websites.
This line
is wrong, as it should be
May you insert
in Additional PHP directives field of PHP Settings page for domain?
Hello Marco,
Both files (/var/log/php-fpm/error.log and /var/log/plesk-phpXX-fpm/error.log) exist on the filesystem. The first is used to store errors from system php-fpm, and the second is used when we are speaking about php-fpm which is provided bu Plesk.
Meanwhile, I have edited the article in this way:
# grep "server reached max_children setting" /var/log/*php*-fpm/error.log | awk '{print $5}' | sort -n | uniq -c
As for the second question, Additional PHP directives field is used to insert configuration directives in /var/www/vhosts/system/example.com/conf/php.ini file (configuration file for php itself), however pm.max_children directive should be added to the configuration file of php-fpm pool (/var/www/vhosts/system/example.com/conf/php.ini).
So, this is two different files. And now Plesk could manage only php settings, but not php fpm pool settings.
If you would like Plesk to manage php fpm pool settings, vote for this feature on Plesk Uservoice portal.
@Konstantin
You provided the same path for both php.ini files and said these were two different files.
Hello @Lars,
Thank you for noticing. Indeed, additional directives are stored in/var/www/vhosts/system/example.com/conf/php.ini and PHP-FPM settings are stored in /opt/plesk/php/X.X/etc/php-fpm.d/example.com.conf.
As it is stated in the article, now it is possible to change PHP-FPM settings in Pleks GUI.
I'd like to create a weekly scheduled task (cron job) to check for "server reached max_children setting" in the plesk-php*-fpm error.log, on CentOS 7. It looks like that log is rotated weekly.
Could someone make a suggestion for a good way to schedule a weekly task, which will e-mail the server administrator, weekly, with a list of domains that have reached "max_children"? I suppose I could create something in "/etc/cron.weekly", but how do I make sure the script runs before the weekly log rotation?
Hello @Bob B!
You are right, the log files from the directories /var/log/plesk-phpXX-fpm/ are rotated weekly - in the logrotate configuration files /etc/logrotate.d/plesk-phpXX, options daily, weekly, monthly, or yearly are not specified, and in the main configuration file /etc/logrotate.conf, the option weekly is set.
For the weekly task you would like to create, you can write a corresponding script and put it into the option prerotate/endscript in the logrotate configuration files /etc/logrotate.d/plesk-phpXX. For more details on logrotate configuration options, see the link https://linux.die.net/man/8/logrotate. Please note that the script should be made on your own.
Bob Benson
I created a little shell script to do this daily, /root/fpmwatch.sh. Be sure to give the script executable permissions (chmod +x fpmwatch.sh) It checks for any occurrences of max_children in all the plesk-php*-fpm error logs for the current day and if it exists sends an email. I include the server IP in the subject line as we have many servers so be sure to update that with your IP and the email address. There may be a much better way of writing the script, but this works for us. I'm not sure if the date format in the log can be different but if it is you have to modify that part of the grep command. The date part of our log format is 30-Jul-2020.
#!/bin/bash
grep "$(date +"%d-%b-%Y")" /var/log/plesk-php*-fpm/error.log | grep max_children > /root/fpmlimitreached.txt
if [ $? -eq 0 ]
then
cat /root/fpmlimitreached.txt | mail -s "PHP-FPM limit reached for site(s) on ipaddresshere" your@emailaddress.com
fi
rm -rf /root/fpmlimitreached.txt
Then I set a cron to run the script at 11:55 PM each night.
55 23 * * * /root/fpmwatch.sh
This is exactly what i was looking for, thanks! Do you have a way to date sort them, and grab only head, to find only the latest offenders, so that when adjusting you can ony get the results you want?
I was using this, but cant pin down the sort.
`sudo find /var/log/*php*/ -type f -exec grep max_child {} \; | sort -t ' ' -k 3.9,3.12n -k 3.5,3.7M`
`| sort -k2M -k3n -k4`
`| sort -k 2,1`
`| sort -s -k 1M -k 4,4`
alot of them are shots in the dark, beacuse i cant seem to output exactly (and only) what `k` sees, or is filtering with, so not sure how to dial it in yet.
Your script is VERY helpful, but doesnt help me find the latest offenders.
mon serveur est super lent
Hello JEAN-CHARLES BURGER
This may have various reasons: high load on websites, hardware issues, DDoS attacks.
Please, inspect all these points.
Bonjour, je suis débutant, connaîtrez-vous un spécialiste en plesk pour vérifier la config que j'ai faite même si je dois payer car mon site inreims.fr est vraiment très lent en attendant votre réponse cordialement burger jc
Hello JEAN-CHARLES BURGER we do not have any contacts of individuals performing such tasks Thought you may look into Plesk partners list, some of them provide managed services: https://www.plesk.com/partner-directory/
Please sign in to leave a comment.