PHP-FPM Tuning for WordPress: Fix High CPU and Slow Pages
Size the PHP-FPM pool for WordPress from measured memory, read the max_children warning, enable the status page and slow log, set OPcache, and trace 100% CPU to its cause.
PHP-FPM runs WordPress with a pool of worker processes, one per request being served. Two settings decide most of its behaviour on a VPS: how many workers may exist (pm.max_children) and how they are started (pm). Too few and requests queue; too many and the server runs out of memory.
The right number comes from a measurement: the memory left for PHP divided by the memory one worker uses. When PHP-FPM sits at 100% CPU, more workers are almost never the fix. The cause is a flood of requests or slow code, and the status page and the slow log show which.
Directive names and defaults below are taken from the PHP manual. The commands were not run on a test server for this article.
Where the pool file is
# Ubuntu, Debian (replace 8.5 with your version: 8.3 on Ubuntu 24.04, 8.4 on Debian 13)
/etc/php/8.5/fpm/pool.d/www.conf # pool settings
/etc/php/8.5/fpm/conf.d/ # php.ini additions
/var/log/php8.5-fpm.log # log
sudo systemctl reload php8.5-fpm
# AlmaLinux, Rocky Linux
/etc/php-fpm.d/www.conf
/etc/php.d/
/var/log/php-fpm/error.log
sudo systemctl reload php-fpm
Find the installed version with php -v. After every change, test the file before reloading:
sudo php-fpm8.5 -t # php-fpm -t on AlmaLinux, Rocky Linux
dynamic, ondemand or static
| Mode | How it works | Fits |
|---|---|---|
pm = dynamic | Keeps between pm.min_spare_servers and pm.max_spare_servers idle workers ready, up to pm.max_children in total | Most single-site servers. The packaged default. |
pm = ondemand | Starts workers only when requests arrive and stops them after pm.process_idle_timeout (10 seconds by default) | Small VPS with several low-traffic sites, where idle memory matters more than the first request's speed |
pm = static | Always runs exactly pm.max_children workers | A server dedicated to one busy site with memory to spare |
Size pm.max_children from measured memory
Measure what the workers use now, after the site has served real traffic for a while:
# Ubuntu, Debian
ps -o rss= -C php-fpm8.5 | awk '{s+=$1; n++} END {printf "%d processes, average %.0f MB\n", n, s/n/1024}'
# AlmaLinux, Rocky Linux
ps -o rss= -C php-fpm | awk '{s+=$1; n++} END {printf "%d processes, average %.0f MB\n", n, s/n/1024}'
RSS counts shared memory, such as the OPcache, once for every process, so the figure errs on the high side. That is the safe direction.
Then do the arithmetic. This is a rule of thumb, not an exact formula:
max_children = (total RAM - RAM for everything else) / average worker size
A worked example with made-up figures: a 2 GB server, 500 MB for MariaDB, 100 MB for Redis, 300 MB for the system and nginx. That leaves about 1,100 MB. If the measurement says 70 MB per worker, 1100 / 70 is 15. Round down and start with 12 to 14. Use your own figures from free -m and the command above.
pm = dynamic
pm.max_children = 12
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500
pm.max_requests restarts each worker after it has served that many requests. The default is 0, which means never. A value in the hundreds limits the effect of memory leaks in plugins and extensions.
Reload, then verify the running values:
sudo php-fpm8.5 -tt 2>&1 | grep -E 'pm(\.| =)'
Setting the limit too high does not make the site faster. When all workers are busy at once the server swaps or the kernel kills a process; see the Linux OOM killer.
The "server reached pm.max_children" warning
WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
This line in the PHP-FPM log means every worker was busy and a new request had to wait. Waiting requests are what visitors see as slow pages, and when the wait is long enough, as 504 gateway timeouts.
sudo grep -c max_children /var/log/php8.5-fpm.log
Raise the limit only if the memory arithmetic allows it. If it does not, the workers are busy for too long, and the next two sections find out why.
Enable the status page
In the pool file:
pm.status_path = /fpm-status
In the nginx server block of the site, reachable only from the server itself (use your own socket path from fastcgi_pass):
location = /fpm-status {
allow 127.0.0.1;
deny all;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.5-fpm.sock;
}
Reload both services and query it:
sudo nginx -t && sudo systemctl reload nginx php8.5-fpm
curl -s -k --resolve example.com:443:127.0.0.1 https://example.com/fpm-status
curl -s -k --resolve example.com:443:127.0.0.1 'https://example.com/fpm-status?full'
The lines to read are active processes, listen queue (requests waiting for a worker), max children reached and slow requests. The ?full form lists each worker with the request URI it is serving and for how long, which shows at a glance what the busy workers are doing.
Enable the slow log
The slow log writes a PHP stack trace for any request that runs longer than a set time. In the pool file:
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm-slow.log
request_slowlog_timeout is 0 (off) by default. After a reload, verify that entries arrive once a slow request has happened:
sudo tail -n 40 /var/log/php-fpm-slow.log
Each entry shows the script and the chain of functions that was running, top line first. A path under wp-content/plugins/name/ names the plugin. Lines ending in curl_exec() mean the code was waiting for an outside server; mysqli_query() means a slow database query. Why is WordPress slow? covers what to do with each.
OPcache settings that matter
OPcache keeps compiled PHP code in shared memory so files are not parsed again on every request. It is enabled by default where it is installed (opcache.enable=1); on Ubuntu and Debian releases before PHP 8.5 it is the separate package php8.x-opcache. Check the live values:
sudo php-fpm8.5 -i | grep -E 'opcache\.(enable|memory_consumption|max_accelerated_files|validate_timestamps|revalidate_freq) '
| Directive | Default | For WordPress |
|---|---|---|
opcache.memory_consumption | 128 (MB) | Enough for one site with a normal set of plugins. Raise to 192 or 256 for several sites or many plugins. |
opcache.max_accelerated_files | 10000 | Must exceed the number of PHP files. Count them with find /var/www -name '*.php' | wc -l. |
opcache.validate_timestamps | 1 | Keep at 1. PHP then checks files for changes every opcache.revalidate_freq seconds (default 2). |
With opcache.validate_timestamps=0 PHP never notices changed files until it is reloaded. That saves a little work per request, but edits and updates made with WP-CLI or over SFTP do not take effect until you reload PHP-FPM. On a site that is updated from the dashboard, the default is the safer choice.
Put changes in a file of your own, for example /etc/php/8.5/fpm/conf.d/99-wordpress.ini (/etc/php.d/99-wordpress.ini on AlmaLinux and Rocky Linux), reload, and run the check above again.
The JIT compiler (opcache.jit) is disabled by default since PHP 8.4. Leave it off. WordPress spends its time in database calls and string handling, not in the tight numeric loops a JIT helps with.
When PHP-FPM uses 100% CPU
Check in this order.
1. Who is sending the requests? Most often it is traffic, not code:
sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
sudo awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
sudo grep -c -E 'xmlrpc\.php|wp-login\.php' /var/log/nginx/access.log
A few addresses with thousands of requests, or thousands of hits on xmlrpc.php and wp-login.php, are a login flood or an aggressive bot. Rate-limit or block them in nginx; WordPress security with nginx has the rules. Uncached pages requested by crawlers (search results, filter URLs) have the same effect.
2. What are the workers doing? The ?full status page shows the URI per worker. The same script in every busy worker, such as admin-ajax.php, points to one feature or plugin.
3. Is code looping? The slow log shows the same plugin function in entry after entry.
4. Is it cron? Many requests for wp-cron.php, or a heavy scheduled task that starts again before it finishes. See WordPress cron jobs.
Also keep PHP itself patched: the PHP-FPM request smuggling advisory lists the fixed versions.