最近 WordPress 上传图片的时候,偶尔会发生失败,报错 503。以前比较少,最近越来越频繁,于是跟踪了一下服务器的日志,看到如下报错信息:
1 |
xxx.xxx.xxx.xxx - - [08/Aug/2023:14:50:36 +0800] "POST /wp-admin/async-upload.php HTTP/2.0" 503 417 "https://www.mobibrw.com/wp-admin/post.php?post=xxxx&action=edit&classic-editor" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" |
查看 PHP 的日志,却找不到任何有用的日志,只看到如下警告:
起初是百思不得其解,按理说应该有相关报错才对。
后来仔细思考了一下,才想明白,原来是并发数超过最大限制了,默认的并发数量是 5,这个绝对是不够的,并发超过限制是没有任何报错信息的,日志只会给出一个警告。
详细的解决方案如下:
If you encounter such an error in the php logs, then it is time to modify max_children. The php logs are usually located in /var/log/
, and in my case they are in /var/log/php7.4-fpm.log
1 |
WARNING: [pool ********] server reached max_children setting (8), consider raising it |
Search for the file where the max_children variable is located, in my case the file is www.conf
and is located in the folder: /etc/php/7.4/fpm/pool.d/
Change the value pm.max_children = 8
to a larger one, 16 for example
1 |
$ sudo nano /etc/php/7.4/fpm/pool.d/www.conf |
save the .conf file and restart the web server:
1 |
$ sudo systemctl restart apache2 |
or only the php-fpm service as follows:
1 |
$ sudo systemctl restart php7.4-fpm.service |
Eventually you can check these logs, who knows, you may not even know you have such a problem!
** As I noticed, for HestiaCP (VestaCP) users max_children can be found in both the /etc/php/7.4/fpm/pool.d/www.conf
file and the file /usr/local/hestia/php/etc/php-fpm.conf
** Attention, you must also have enough memory for this, otherwise the results are not as expected!
Check the memory used by each php-fpm service-child with the following command:
1 2 3 4 5 6 7 |
$ ps -ef | grep '[f]'pm root 618 1 0 Dec17 ? 00:00:27 php-fpm: master process (/usr/local/hestia/php/etc/php-fpm.conf) root 1692 1 0 01:11 ? 00:00:00 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) matrix 3539 1692 14 01:30 ? 00:00:03 php-fpm: pool mydomain314159265.com matrix 3542 1692 10 01:30 ? 00:00:01 php-fpm: pool mydomain314159265.com matrix 3547 1692 0 01:30 ? 00:00:00 php-fpm: pool mydomain314159265.com matrix 3548 1692 15 01:30 ? 00:00:00 php-fpm: pool mydomain314159265.com |
Then, to see the memory required for these processes, we need to add the rss parameter which is “Resident Set Size” or the physical memory. Note: Modify php-fpm7.4 to suit your version of PHP:
1 2 3 4 5 |
$ ps -C php-fpm7.4 -o rss= 81316 136872 138096 121728 |
These numbers are in Kbytes , so we calculate an average of the displayed values and multiply by max_children
, and we find approximately the memory needed only for php, but don’t forget, the server also needs memory for Apache, Nginx, mysql … maybe Varnish, HAproxy, Redish, etc. .. so, check these values carefully!
参考链接
Solved! WARNING: [pool ***] server reached max_children, consider raising it