I have a CentOS server. System is nginx/php-fpm. It has 16GB RAM. CPUs : 8
CPU Frequency: 2660.203 MHz
Why am I getting this error in my error log?
php-fpm/error.log:
[02-Aug-2014 17:14:04] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 21 total children
This is my php-fpm configuration for the www pool:
php-fpm/www.conf:
pm = dynamic
pm.max_children = 32768
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 10
pm.max_requests = 5000
How to fix the problem?
pm.max_children = Total RAM dedicated to the web server / Max child process size. For example, if the server has 8GB of RAM and 6GB of RAM is planned to be allocated to the web server, then the pm.max_children value will be the following: pm.max_children = 6144MB / 85MB = 72.
The configuration file is /etc/php- fpm.
It is a tough cookie because there could be numerous factors involved. The first problem with your config is the max_children
is ridiculously high. If each child process is using 50MB then 50 x 32768 would easily deplete 16GB.
A better way to determine max_children
is to find out how much each child process uses, then factor in the maximum RAM you would like php-fpm to use and then divide the values. E.g. If I have a 16GB server, I can run the following command to determine how much ram each php-fpm child consumes:
ps -ylC php-fpm --sort:rss
Note! It may be required to explicitly specify user if php-fpm is running under the different one.
ps -ylC php-fpm --sort:rss -u www-data
where www-data is the user under which php-fpm is being run.
You are on the lookout for the RSS column; it states resident memory and is measured in KB. If I have an average of 50MB per process and I want to use a maximum of 10GB for php-fpm processes, then all I do is 10000MB \ 50MB = 200. So, on that basis, I can use 200 children for my stated memory consumption.
Now, with regards to the servers, you will want to set the max_spare_servers
to x2 or x4 the number of cores. So if you have an 8 core CPU then you can start off with a value of 16 for max_spare_servers
and go up to 32.
The start_servers
value should be around half of the max_spare_servers
value.
You should also consider dropping the max_requests
to around 500.
Also, in addition to dynamic
, the pm
value can also be set to static
or ondemand
.
static
will always have a fixed number of servers running at any given time. This is good if you have a consistent amount of users or you want to guarantee you don't breach the max memory.ondemand
will only start processes when there is a need for them. The downside is obviously having to constantly start/kill processes which will usually translate into a very slight delay in request handling. The upside, you only use resources when you need them.dynamic
always starts X amount of servers specified in the start_servers
option and creates additional processes on an as-need basis.If you are still experiencing issues with memory then consider changing pm
to ondemand
.
This is a general guideline, your settings may need further tweaking. It is really a case of playing with the settings and running benchmarks for maximum performance and optimal resource usage. It is somewhat tedious but it is the best way to determine these types of settings because each setup is different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With