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:

formula
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:

terminal
# 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:

terminal
free -m
vmstat 1 5

Setting the values

With 80 MB per worker and 3 GB for PHP, my settings would look like this:

config
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_children is the limit. I always round it down to leave some room.
  • pm.start_servers is how many workers are ready when PHP starts, so the first visitors do not have to wait.
  • pm.min_spare_servers and pm.max_spare_servers set how many idle workers PHP keeps ready.
  • pm.max_requests restarts 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.

docs.cpanel.net/whm/software/multiphp-manager-for-whm
cPanel documentation page MultiPHP Manager for WHM
MultiPHP Manager in WHM is where cPanel keeps the PHP-FPM settings for each domain. Screenshot: cPanel documentation, MultiPHP Manager for WHM

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:

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_children to that number.
  • Check with vmstat that 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

  1. FPM configuration, PHP manual php.net
  2. MultiPHP Manager for WHM, cPanel documentation docs.cpanel.net
Irfan Bhat

Written by

Irfan Bhat

Web Developer & Server Engineer · Baramulla, Kashmir

Web developer and server engineer in Baramulla, Kashmir. I look after servers and websites, and I build practical AI tools for businesses.