The series kicked off with Redis and Memcached, continued with the web server as a reverse proxy, and concluded with edge caching. Three layers, three articles. And one that got left out.
It wasn’t deliberate. Simply put, walking through the stack from the outside in, PHP-FPM ended up falling through the cracks. Yet it is precisely the layer living between Nginx and WordPress, the one that decides how many PHP processes can run simultaneously. Without understanding it, the rest of your optimized stack can still crumble under heavy load.
This is that missing chapter.
0. The Symptom That Truly Is Urgent This Time
Everything seems properly configured. Redis is running and cache hit rates are high. Nginx stands out front filtering traffic. And yet, this happens:
- Under load, the server responds with 502 Bad Gateway or 504 Gateway Timeout
- CPU usage barely hits 40%… yet the site collapses all the same
- WooCommerce works fine with 5 concurrent users, but with 20 everything degrades
- The admin dashboard becomes unusable during peak traffic hours
If that is your scenario, the bottleneck likely isn’t the database or the reverse proxy. It lies in how many PHP processes can run in parallel.
1. Where PHP-FPM Fits in the Stack
This series has been building the stack from the outside in. Looking at the complete picture:
graph TD
A[Cliente] --> B[CDN / Edge Cache]
B --> C[Nginx / Apache]
C --> D[PHP-FPM]
D --> E[WordPress]
E --> F[(MySQL / Redis)]
We already understand the role of each layer. The CDN resolves requests at the edge without ever touching your origin server. Nginx filters, caches, and shields the server before PHP ever gets involved. Redis prevents WordPress from hammering MySQL with repetitive queries.
However, when a request bypasses all those filters and reaches PHP—a login, adding an item to the cart, a REST API call, or any dynamic, un-cached content—PHP-FPM is what decides whether there is available capacity to process it.
2. Workers: The Concept That Explains Everything
PHP-FPM stands for PHP FastCGI Process Manager. Its job is to maintain a pool of ready PHP processes to execute code, known as workers.
When Nginx hands off a dynamic request, an available worker picks it up and executes the full WordPress lifecycle:
- Loads core files, plugins, and the active theme.
- Initializes hooks and functions.
- Queries Redis or MySQL depending on what is needed.
- Renders the final HTML.
- Sends the response back to Nginx to be delivered to the client.
Each worker is an independent process that consumes RAM, CPU, and execution time. And it can only handle one single request at a time.
The direct consequence: the number of workers defines how many dynamic requests your server can process in parallel. No more, no less.
3. The Setting That Matters Most: pm.max_children
The pm.max_children directive in your PHP-FPM pool configuration sets the absolute ceiling for concurrent workers.
With pm.max_children = 10, your server can process exactly 10 dynamic requests in parallel.
What happens to request number 11? It enters a waiting queue controlled by listen.backlog, a parameter that on many operating systems defaults to a surprisingly low value, sometimes 511 or even 65. When that queue fills up as well, the server starts dropping connections immediately. That is where 502 and 504 errors appear.
This is the most misleading phenomenon when diagnosing WordPress under load: a server can sit at 40% CPU and still be completely paralyzed. The bottleneck is not compute capacity; it is a shortage of available workers.
4. More Workers Is Not Always the Solution
The knee-jerk reaction when a server crashes is bumping pm.max_children to an arbitrarily large number. The issue is that each worker consumes anywhere between 40 MB and 120 MB of RAM depending on site complexity. If you spawn more workers than your physical RAM can handle, the server will begin swapping to disk.
When that happens, latency spikes dramatically. And on many modern VPS instances where swap is disabled altogether, the kernel simply invokes the dreaded OOM Killer to terminate processes abruptly, causing far more damage than the original issue.
The proper formula is:
pm.max_children = RAM disponible para PHP ÷ consumo promedio por worker
Example: 2 GB dedicated to PHP, with workers consuming roughly ~80 MB each:
2048 MB ÷ 80 MB ≈ 25 workers
To apply this formula accurately, however, you need to know how much memory each worker actually uses on your specific setup. Here is the command to measure it:
ps --no-headers -o rss -C php-fpm | \
awk '{sum += $1} END {
if (NR > 0)
print sum/NR/1024 " MB";
else
print "0 MB (no hay procesos php-fpm)"
}'
The output is an average, so the more traffic you have when running it, the more representative the measurement will be. Ideally, run it during peak hours or while running a load test; an average calculated with a single idle worker can drastically underestimate memory usage under full production load.
Important: Remember to leave breathing room for the rest of the stack: Nginx, MySQL, Redis, and the OS itself all need memory. Not 100% of your RAM belongs to PHP.
5. The Three Process Management Modes
PHP-FPM offers three distinct strategies for managing worker lifecycles. Each serves a specific purpose.
ondemand – For Severely Constrained Resources
Workers are spawned strictly when requests arrive and are destroyed after an inactivity window (pm.process_idle_timeout).
It consumes virtually zero RAM when the site is idle, but it introduces cold-start latency on the first request after silence: the operating system must fork a new process from scratch. Useful in local development or on small VPS nodes hosting dozens of low-traffic sites. Not recommended for production environments with active users.
dynamic – The Sweet Spot for Most Production Sites
Maintains a baseline number of active workers (pm.start_servers) and dynamically creates or destroys processes based on incoming demand, within boundaries set by pm.min_spare_servers and pm.max_spare_servers.
This is the most versatile mode and the default recommendation for WordPress in production. It responds smoothly to moderate traffic spikes without wasting RAM during quiet periods.
A reasonable baseline configuration for a 2 GB VPS:
pm = dynamic
pm.max_children = 20
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
static – For Dedicated Servers with Predictable Traffic
Maintains a fixed number of workers permanently alive, regardless of whether there is incoming traffic. Maximum raw performance with minimal master process overhead. The trade-off: that RAM is permanently locked down. It only makes sense on dedicated servers running a single application with steady, predictable load.
6. What Redis and OPcache Cannot Solve
In our article on Redis, we covered how object caching drastically cuts down repetitive queries to MySQL. There is a counterpart for PHP code execution: OPcache, which stores precompiled script bytecode in shared memory, preventing the engine from having to recompile PHP scripts on every single request.
Both tools are indispensable. But neither increases your available worker count.
What they actually do is reduce the execution time each worker needs to complete its task—say, from 400 ms down to 50 ms. A worker that finishes faster returns to the idle pool sooner, ready to pick up the next queued request. That increases total throughput without adding a single extra worker.
In other words: Redis and OPcache improve throughput per worker. PHP-FPM defines how many workers exist. They are complementary layers, not substitutes. Just as Nginx protects PHP-FPM, and the CDN protects Nginx, each layer in this series does a job that the others cannot.
7. How to Diagnose if PHP-FPM Is Your Bottleneck
Before modifying any configuration file, verify that the bottleneck is indeed here.
Clear Signs of Worker Exhaustion
- 502 or 504 errors under traffic spikes, especially during peak hours
- Low CPU utilization paired with high latency and long response times
- Errors vanish during quiet hours and reappear as concurrent users climb
Inspecting Status in Real Time
# Contar procesos PHP-FPM activos
watch -n 1 "ps aux | grep php-fpm | grep -v grep | wc -l"
Enabling the Slowlog to Catch Slow Workers
In your PHP-FPM pool configuration, add:
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 2s
Any request taking longer than 2 seconds will be recorded along with its complete stack trace. It is the most direct diagnostic tool to uncover what is holding workers hostage longer than necessary: a plugin executing slow remote API calls, an unindexed SQL query, or an unresponsive external service.
8. Now You Know Why the Server Crashes Even at Low CPU
You can have Redis in place, OPcache enabled, Nginx filtering traffic, and a CDN handling the bulk of your requests.
If PHP-FPM is not properly sized, the server will continue to collapse the moment dynamic traffic exceeds worker capacity.
Not due to a lack of raw compute power, but because every worker is busy and incoming requests have nowhere to go.
Measure real memory consumption per worker, calculate how many your available RAM can reliably support, pick the process manager mode suited to your workload, and validate your settings under real traffic.
That is what separates an infrastructure that scales from one that merely survives… until it can’t anymore.
PHP-FPM is the missing chapter in a series breaking down the real-world performance layers of WordPress in production. If you landed here directly, catch up on the rest in any order: Redis and Memcached as an Object Cache – The Web Server as a Reverse Proxy Layer – Edge Caching with a CDN Layer