Optimizing PHP-FPM Configuration for Maximum Throughput
Jul 24, 2025 pm 05:42 PMUse dynamic process manager for most cases, static for steady high traffic, and avoid ondemand for high throughput; 2. Set pm.max_children based on available RAM divided by average PHP process memory, leaving headroom to prevent swapping; 3. Configure pm.max_requests to recycle workers and prevent memory leaks, set request_terminate_timeout to kill long-running requests, enable slowlog to track bottlenecks; 4. Use Unix sockets for lower I/O overhead, adjust listen.backlog to match web server settings, increase rlimit_files if needed; 5. Continuously monitor FPM status, upstream response times, and system metrics using tools like htop or Grafana; additionally, enable Opcache with appropriate settings to reduce script parsing overhead and significantly boost performance, as even well-tuned PHP-FPM will underperform without it.
Optimizing PHP-FPM for maximum throughput isn’t about cranking every setting to the max — it’s about aligning configuration with your application’s behavior, server resources, and traffic patterns. Poorly tuned PHP-FPM can lead to slow response times, high memory usage, or even 502 errors under load. Here’s how to get the most out of it.

1. Choose the Right Process Manager (pm)
PHP-FPM supports three process managers: static
, dynamic
, and ondemand
. Your choice directly affects performance and resource use.
-
dynamic
(Recommended for most cases)
Balances performance and memory. Keeps a pool of child processes that can grow or shrink based on demand.pm = dynamic pm.max_children = 50 pm.start_servers = 10 pm.min_spare_servers = 5 pm.max_spare_servers = 15
static
All child processes are spawned at startup. Best for high, consistent traffic when you can afford the memory.pm = static pm.max_children = 30
ondemand
Spawns children only when requests arrive. Saves memory but increases latency during traffic spikes — not ideal for high-throughput scenarios.
? Rule of thumb: Use
dynamic
unless you have predictable, steady traffic and want to minimize process management overhead.
2. Set pm.max_children
Based on Available Memory
This is the most critical setting. Too high = memory exhaustion. Too low = request queuing.
Estimate max_children:
max_children = Total RAM dedicated to PHP-FPM / Average memory per PHP process
For example:
- You allocate 2GB RAM to PHP-FPM
- Average PHP process uses ~64MB
2048 MB / 64 MB = 32
So set pm.max_children = 32
(leave some headroom for system overhead).
You can check memory per process using:
ps --no-headers -o "rss,cmd" -C php-fpm | awk '{sum =$1} END {print int(sum/NR/1024)" MB"}'
?? Avoid overcommitting memory — swapping kills performance.
3. Tune Request Handling and Timeout Settings
Long-running or stuck requests can exhaust the worker pool.
pm.max_requests = 500 ; Restart workers after 500 requests (helps prevent memory leaks) pm.process_idle_timeout = 10s ; For 'ondemand' only request_terminate_timeout = 30s ; Kill requests taking longer than 30s request_slowlog_timeout = 5s ; Log slow requests (pair with slowlog) catch_workers_output = yes ; Capture stdout/stderr
pm.max_requests
helps mitigate memory leaks in long-running scripts (e.g., legacy code).request_terminate_timeout
prevents hanging requests from blocking workers.- Enable
slowlog
to identify bottlenecks:slowlog = /var/log/php-fpm-slow.log
4. Optimize I/O and Concurrency
PHP-FPM works best when it doesn’t become the bottleneck. Pair it with a fast web server (like Nginx) and optimize request flow.
Use socket connections instead of TCP (if on same host):
listen = /run/php/php8.1-fpm.sock listen.owner = www-data listen.group = www-data listen.mode = 0660
Unix sockets have lower overhead than
127.0.0.1:9000
.Adjust
listen.backlog
if you see connection rejections:listen.backlog = 511
Set this close to the web server’s
backlog
(e.g., Nginxlisten ... backlog=511
).Increase
rlimit_files
if hitting file descriptor limits:rlimit_files = 65535
5. Monitor and Iterate
Tuning is not one-time. Monitor key metrics:
FPM status page (enable in pool config):
pm.status_path = /status
Access via web server to see active/idle processes, queue length, etc.
Nginx upstream response times — long waits may indicate FPM saturation.
System metrics: CPU, memory, swap, and load average.
Use tools like htop
, sysdig
, or Prometheus Grafana for deeper insight.
Bonus: Use Opcache
PHP-FPM handles process management, but Opcache dramatically reduces script parsing overhead.
opcache.enable=1 opcache.memory_consumption=256 opcache.max_accelerated_files=20000 opcache.validate_timestamps=0 ; Disable in production (re-enable for deployments) opcache.fast_shutdown=1
Without Opcache, even perfectly tuned FPM will underperform.
Basically, optimizing PHP-FPM comes down to: matching max_children
to memory, choosing the right process manager, preventing stuck requests, and reducing I/O overhead. Combine that with Opcache and monitoring, and you’ll squeeze the most throughput out of your PHP stack.
The above is the detailed content of Optimizing PHP-FPM Configuration for Maximum Throughput. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In order to improve the performance of Go applications, we can take the following optimization measures: Caching: Use caching to reduce the number of accesses to the underlying storage and improve performance. Concurrency: Use goroutines and channels to execute lengthy tasks in parallel. Memory Management: Manually manage memory (using the unsafe package) to further optimize performance. To scale out an application we can implement the following techniques: Horizontal Scaling (Horizontal Scaling): Deploying application instances on multiple servers or nodes. Load balancing: Use a load balancer to distribute requests to multiple application instances. Data sharding: Distribute large data sets across multiple databases or storage nodes to improve query performance and scalability.

Nginx performance tuning can be achieved by adjusting the number of worker processes, connection pool size, enabling Gzip compression and HTTP/2 protocols, and using cache and load balancing. 1. Adjust the number of worker processes and connection pool size: worker_processesauto; events{worker_connections1024;}. 2. Enable Gzip compression and HTTP/2 protocol: http{gzipon;server{listen443sslhttp2;}}. 3. Use cache optimization: http{proxy_cache_path/path/to/cachelevels=1:2k

Methods to improve Apache performance include: 1. Adjust KeepAlive settings, 2. Optimize multi-process/thread parameters, 3. Use mod_deflate for compression, 4. Implement cache and load balancing, 5. Optimize logging. Through these strategies, the response speed and concurrent processing capabilities of Apache servers can be significantly improved.

Performance optimization for Java microservices architecture includes the following techniques: Use JVM tuning tools to identify and adjust performance bottlenecks. Optimize the garbage collector and select and configure a GC strategy that matches your application's needs. Use a caching service such as Memcached or Redis to improve response times and reduce database load. Employ asynchronous programming to improve concurrency and responsiveness. Split microservices, breaking large monolithic applications into smaller services to improve scalability and performance.

In order to improve the performance of concurrent, high-traffic PHP applications, it is crucial to implement the following architectural optimizations: 1. Optimize PHP configuration and enable caching; 2. Use frameworks such as Laravel; 3. Optimize code to avoid nested loops; 4. Optimize database, Build index; 5. Use CDN to cache static resources; 6. Monitor and analyze performance, and take measures to solve bottlenecks. For example, website user registration optimization successfully handled a surge in user registrations by fragmenting data tables and enabling caching.

PHP Framework Performance Optimization: Embracing Cloud-Native Architecture In today’s fast-paced digital world, application performance is crucial. For applications built using PHP frameworks, optimizing performance to provide a seamless user experience is crucial. This article will explore strategies to optimize PHP framework performance by combining cloud-native architecture. Advantages of Cloud Native Architecture Cloud native architecture provides some advantages that can significantly improve the performance of PHP framework applications: Scalability: Cloud native applications can be easily scaled to meet changing load requirements, ensuring that peak periods do not occur bottleneck. Elasticity: The inherent elasticity of cloud services allows applications to recover quickly from failures and maintain availability and responsiveness. Agility: Cloud-native architecture supports continuous integration and continuous delivery

Tips for improving performance in C++ class design include: avoiding unnecessary copies, optimizing data layout, and using constexpr. Practical case: Use object pool to optimize object creation and destruction.

When processing XML and RSS data, you can optimize performance through the following steps: 1) Use efficient parsers such as lxml to improve parsing speed; 2) Use SAX parsers to reduce memory usage; 3) Use XPath expressions to improve data extraction efficiency; 4) implement multi-process parallel processing to improve processing speed.
