On this page
I see this a lot. A website runs fine for months. Then traffic grows a little, and the server starts freezing at busy times. Pages take forever to load, the load average goes very high, and a restart fixes it for a few hours. The usual reaction is to buy a bigger server.
Most of the time the server is big enough, and the problem is a setting. PHP has been allowed to run more worker processes at the same time than the server has memory for. When traffic is low, you never reach that limit, so everything looks fine. When traffic is high, PHP keeps starting more workers, the server runs out of memory and starts using swap, and everything slows down.
The simple maths
Every PHP worker is a separate process, and each one uses its own memory. The setting that controls how many can run at once is called pm.max_children. The rule is:
max_children × memory per worker < memory available for PHP
Here is an example. On a server with 4 GB of RAM, the operating system, MySQL and everything else might use about 1 GB. That leaves about 3 GB for PHP. If each WordPress worker uses around 80 MB, the server can safely run about 37 workers.
Now imagine pm.max_children is set to 120, because someone copied it from a tutorial. At a busy moment PHP will try to use around 9.6 GB on a 4 GB server. It does not fail cleanly. The server starts swapping, requests get slower, more workers start to deal with the waiting visitors, and it keeps getting worse until the site freezes.
Measure your own numbers
Do not guess the memory per worker, and do not copy a number from an article, including this one. Measure it on your own server while it is busy:
# PHP processes, biggest first (memory in KB)
ps -eo rss,args --sort=-rss | grep php | head -20
Run it a few times at a busy hour, not in the middle of the night. Note the average, and also the biggest worker. One heavy admin page can use several times more memory than a normal page.
Then check how much memory you really have, and whether the server is already swapping:
free -m
vmstat 1 5
Setting the values
With 80 MB per worker and 3 GB for PHP, my settings would look like this:
pm = dynamic
pm.max_children = 35
pm.start_servers = 8
pm.min_spare_servers = 6
pm.max_spare_servers = 12
pm.max_requests = 500
pm.max_childrenis the limit. I always round it down to leave some room.pm.start_serversis how many workers are ready when PHP starts, so the first visitors do not have to wait.pm.min_spare_serversandpm.max_spare_serversset how many idle workers PHP keeps ready.pm.max_requestsrestarts each worker after a number of requests. This stops a slow memory leak from growing forever.
On a cPanel server, you can set the maximum number of workers (Max Children) and Max Requests for each domain in WHM, under MultiPHP Manager.

The warning in the log
PHP tells you when it reaches the limit. You will see a line like this in the PHP error log:
WARNING: [pool www] server reached pm.max_children setting (35), consider raising it
Do not raise the number just because the log says so. Do the maths again first. If the memory is already full, a higher limit turns a short wait for visitors into a server that freezes. If you really need more workers, make the pages faster or add more RAM.
One pool for each website
If one server runs several websites, give each site its own pool, with its own user and its own worker limit. Then a traffic spike on one site cannot use up every worker and take the other sites down with it. It also keeps each site's files apart, so if one site gets hacked, the attacker cannot easily read the others.
Quick checklist
- Measure how much memory each PHP worker uses at busy times.
- Work out how much memory is left for PHP after the system, MySQL and everything else.
- Divide, round down, and set
pm.max_childrento that number. - Check with
vmstatthat the server does not swap at peak times. - Watch the log for the max_children warning, and do the maths before raising it.
- Give each website on a shared server its own pool.
In most cases this is a settings fix and not a hardware problem, and it takes an afternoon. If the numbers still do not work out, the server really is too small, and now you know that for sure. This is part of my Linux server management work.
Sources
- FPM configuration, PHP manual php.net
- MultiPHP Manager for WHM, cPanel documentation docs.cpanel.net
