PHP-FPM max children is the most essential parameter for preventing 502 errors, memory exhaustion, and queue saturation on production servers. In this guide, you will learn how to calculate the optimal value based on real memory usage, detect queue bottlenecks, and validate changes under production-level traffic.
If you need a high-performance and reliable dedicated server to run PHP-FPM tuning in production, visit PerLod Hosting and choose a dedicated plan that matches your traffic and RAM needs.
What is the PHP-FPM Max Children Directive?
The pm.max_children directive defines the maximum number of simultaneous PHP worker processes that can exist at any given time. Each process handles one request at a time in PHP-FPM's process-based model.
When all the available PHP-FPM workers are busy, new requests have to wait in a queue.
If that queue gets full, NGINX may return 502 Bad Gateway because it can’t hand the request to PHP-FPM. If pm.max_children is too low, traffic spikes cause slowdowns and dropped or failed requests; if it’s too high, PHP-FPM can use too much RAM, leading to out-of-memory (OOM) crashes and overall server instability.
PHP-FPM offers three process manager types controlled by the pm directive, including:
pm = dynamic: PHP-FPM automatically adds or removes worker processes depending on traffic, keeping at least pm.min_spare_servers is idle and never going above pm.max_children. This mode works well when traffic goes up and down, and you want good performance without using too much RAM.
pm = static: In this mode, PHP-FPM keeps a fixed number of worker processes running all the time. This means performance is consistent because it doesn’t need to create new workers, but it can waste RAM when traffic is low. It’s usually best for dedicated servers with steady high traffic and enough memory available.
pm = ondemand: In this mode, PHP-FPM starts worker processes only when a request comes in, and it stops them after they’ve been idle for pm.process_idle_timeout. This saves RAM, but the first requests can be slightly slower because PHP-FPM must create new workers. It’s a good fit for low-traffic sites or shared hosting environments.
Note: For high-traffic production environments, pm = dynamic offers the best balance, while pm = static delivers maximum throughput when resources permit.
pm.max_children: Max workers, which prevents RAM overload.
pm.start_servers: Workers started at launch.
pm.min_spare_servers: Minimum idle workers.
pm.max_spare_servers: Maximum idle workers, which avoid waste.
pm.max_requests: Recycle a worker after N requests, which limits memory growth.
PHP-FPM max_children memory Calculation
At this point, you can set how many PHP worker processes your server can run without running out of RAM. Instead of guessing, you can measure the real memory usage of one PHP-FPM process under normal load, then divide the RAM you can safely dedicate to PHP-FPM by that per-process number.
This gives a safe pm.max_children value that helps avoid 502 and 504 errors from overload and prevents OOM crashes from over-allocating workers.
Step 1. Measure Real Process Memory Usage
The best way to set pm.max_children is to measure how much RAM a real PHP-FPM worker uses on your live server or a staging server that’s the same.
Method 1: Check RSS memory per process with the command below:
Bash
ps-ylCphp-fpm--sort:rss
In your output, you must see PHP-FPM processes sorted by RSS (Resident Set Size), which is real physical memory usage in kilobytes. Example output:
Look at the RSS column, in this example, processes use about 50MB, 47MB, and 52MB.
Method 2: Calculate average memory per process with the command below:
Bash
ps--no-headers-o"rss,cmd"-Cphp-fpm|awk'{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'
This command adds up the RSS used by all PHP-FPM processes, then divides by how many processes there are, so you get the average memory per process in MB. For example, 52M.
Important Note: Measure PHP-FPM memory usage while the site is under real traffic, because each worker’s RAM usage depends heavily on your app, plugins, and themes. A common real-world baseline is around ~60 MB per PHP-FPM process in some environments, but it can be higher or lower depending on the workload.
Step 2. Determine Available RAM for PHP-FPM
You cannot allocate 100% of system RAM to PHP-FPM; you must reserve memory for:
Operating system: Around 500MB to 1GB, depending on distro.
NGINX web server: Typically 50 to 200MB.
Database server like MySQL or MariaDB, if co-located: Around 1 to 4GB.
Caching layers like Redis or Memcached: 256MB to 2GB.
Note: Always round down so you keep a safety buffer for traffic spikes and other services. For example, if one worker uses ~52 MB, then 35 workers would use about 1,820 MB (35 × 52), which can push you past your safe RAM limit.
Step 4. Calculate Related pm Parameters
Once you measured max_children, you must set the related parameter values. Here is the calculation for them:
As we mentioned, when all max_children workers are busy, new requests are added to a listen queue. PHP-FPM accepts connections but cannot process them immediately.
The queue depth is controlled by the listen backlog and the system-level limit:
Remember to recalculate supporting parameters accordingly.
2. Increase the listen queue depth.
To buffer more connections during short traffic spikes, increase PHP-FPM’s socket backlog, but remember it can’t exceed the system limit:
Bash
listen.backlog=4096
Also, ensure the system limit is sufficient:
Bash
sudosysctl-wnet.core.somaxconn=4096
Make it persistent in /etc/sysctl.conf file:
Bash
net.core.somaxconn=4096
3. Optimize application code.
You can reduce the per-request execution time by:
Enabling opcode caching (OPcache).
Using object caching like Redis.
Optimizing database queries, such as adding indexes and reducing N+1 queries.
Implementing CDN for static assets.
Enabling HTTP keep-alive to reduce connection overhead.
4. Use pm = static model for high traffic.
For long periods of heavy traffic, use pm = static so PHP-FPM keeps a fixed number of workers always running and doesn’t waste time creating or killing processes.
Bash
pm=staticpm.max_children=50pm.max_requests=1000
This keeps all 50 workers alive continuously, ready to serve requests immediately.
PHP-FPM max_children Tuning Steps
Here is a simple checklist to safely adjust how many PHP-FPM workers your server can run in production. The goal is to increase capacity without causing high memory usage, queue buildup, or 502 and 504 errors.
1. Back up Existing Configuration
Before making changes, it is recommended to back up the current configuration. For example:
This happens because the PHP-FPM worker crashed or timed out. To fix this, check the PHP-FPM error log for crashes, and increase request_terminate_timeout.
This happens because all workers are busy and listen queue is full. To fix this, increase max_children or optimize application code.
Note: After load testing, you can adjust parameters if needed:
If memory usage is well below the limit, increase max_children by 20% to handle more concurrent traffic.
If queue saturation persists: Increase max_children if RAM is available or optimize the application to reduce request duration.
If memory pressure occurs: Reduce max_children by 10 to 15% and focus on application optimization.
If slow requests are common, investigate with profiling tools and optimize bottlenecks.
Conclusion
Setting PHP-FPM max children correctly is key to keeping high-traffic sites stable. Don’t trust defaults or copy-paste configs; measure real memory usage, calculate a safe value, then test and monitor in production. For best results, tune PHP-FPM and also optimize the app to reduce PHP load.
The goal isn’t to increase the max_children number; it’s to find the safe spot where your server handles target traffic reliably without losing resources or queuing requests.
We hope you enjoy this guide. Subscribe to our X and Facebook channels to get the latest articles.
When all workers are busy, requests will be rejected, and you may get 502 errors. If the queue overflows, some connections get dropped during traffic spikes.
If you set it too high, the server can run out of RAM. Then Linux may kill PHP-FPM processes, causing downtime or even making the whole server unstable.
Use pm = dynamic for most production sites because it scales workers up and down with traffic and saves memory when traffic is low. Use pm = static only when traffic is constantly high and you have plenty of RAM, since it keeps a fixed number of workers running all the time for maximum performance.
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.