How to Configure PHP-FPM for Better Performance
Introduction
PHP-FPM (FastCGI Process Manager) is the recommended method for running PHP with Nginx. It provides better performance, improved resource management, and greater flexibility compared to traditional CGI execution.
A properly configured PHP-FPM pool can reduce memory usage, improve response times, and keep your server stable under heavy traffic. This guide explains the most important PHP-FPM settings, how they affect performance, and how to tune them on AlmaLinux 10 with PHP 8.3.
Test Environment
This tutorial was tested using the following environment:
What Is PHP-FPM?
PHP-FPM stands for FastCGI Process Manager. It manages a pool of worker processes that execute PHP scripts on behalf of the web server.
Instead of creating a new PHP process for every request, PHP-FPM keeps worker processes running in memory. Incoming requests are assigned to available workers, which significantly reduces process creation overhead and improves response time.
The basic request flow is:
Locate the PHP-FPM Configuration File
The default pool configuration is stored in:
/etc/php-fpm.d/www.conf
Open it with:
Understanding Process Management Modes
PHP-FPM supports three process management modes.
dynamic
pm = dynamic
Workers are created and destroyed automatically according to server load.
Suitable for most production servers.
static
pm = static
Always keeps a fixed number of workers running.
Provides predictable performance but uses more memory.
ondemand
pm = ondemand
Workers are started only when requests arrive.
This mode saves memory but may slightly increase response time for the first request.
It is suitable for low-traffic servers.
Recommended Configuration
For most VPS servers (2–8 GB RAM), the following configuration provides a good balance between performance and memory usage.
Configuration Explained
pm.max_children
Maximum number of PHP worker processes.
pm.max_children = 50
If all workers are busy, new requests must wait until a worker becomes available.
Increasing this value allows more concurrent requests but also increases memory consumption.
pm.start_servers
Number of workers started when PHP-FPM launches.
pm.start_servers = 5
Too many startup workers waste memory, while too few may slow the server immediately after startup.
pm.min_spare_servers
Minimum number of idle workers.
pm.min_spare_servers = 5
If idle workers fall below this value, PHP-FPM creates additional processes automatically.
pm.max_spare_servers
Maximum number of idle workers.
pm.max_spare_servers = 10
Extra idle workers beyond this limit are terminated to free memory.
pm.max_requests
Maximum requests handled by one worker before it is restarted.
pm.max_requests = 500
Restarting workers periodically helps prevent memory leaks in long-running PHP processes.
How to Calculate pm.max_children
One common mistake is choosing an arbitrary value.
A better approach is to estimate memory usage.
First, determine approximately how much memory one PHP worker consumes.
For example:
Average PHP Worker = 60 MB
Available Memory = 3 GB
Calculation:
3072 ÷ 60 ≈ 51
A reasonable value would be:
pm.max_children = 50
Leave some memory available for Nginx, MariaDB, Redis, and the operating system.
Restart PHP-FPM
After saving the configuration, restart PHP-FPM.
Check its status.
You should see:
Active: active (running)
Verify the Configuration
Verify the configuration syntax before restarting the service.
Expected output:
configuration file is valid
You can also confirm that the service is listening:
Common Configuration Mistakes
Setting pm.max_children Too High
A very large value can cause excessive memory usage and trigger the Linux Out Of Memory (OOM) killer.
Setting pm.max_children Too Low
If the worker limit is too small, requests may queue during traffic spikes, resulting in slower page loads.
Forgetting to Restart PHP-FPM
Changes in www.conf do not take effect until PHP-FPM is restarted.
Ignoring Log Files
If PHP-FPM fails to start, inspect the service logs:
These logs often contain detailed information about configuration errors.
Best Practices
Use dynamic mode for most production environments.
Choose pm.max_children based on available memory rather than guessing.
Restart workers periodically with pm.max_requests.
Monitor memory usage after configuration changes.
Test configuration updates on a staging server before applying them to production.
Conclusion
PHP-FPM is an essential component of modern PHP deployments. Correctly tuning process management parameters can improve performance, reduce memory usage, and increase server stability.
For most AlmaLinux 10 servers running PHP 8.3 with Nginx, the recommended settings in this guide provide an excellent starting point. Continue monitoring your server under real workloads and adjust the configuration as traffic and application requirements evolve.
After optimizing PHP-FPM, you can further improve PHP performance by enabling OPcache. See our guide: How to Enable PHP OPcache for Better Performance.
Explore More
› PHP opcache.max_accelerated_files Explained: Configuration, Performance, and Best Practices
› PHP opcache.validate_timestamps Explained: Configuration, Code Updates, and Best Practices
› PHP opcache.revalidate_freq Explained: Configuration, Performance, and Best Practices
› PHP opcache.enable Explained: Configuration, Performance, and Best Practices