How To Optimize PHP-FPM For Magento 2 On Nginx
Running Magento 2 on Nginx with PHP-FPM gives your store great flexibility and speed — but only when it’s properly optimized.
Out-of-the-box PHP-FPM settings are usually too generic for Magento’s high-performance needs. Without tuning, you might experience high CPU usage, slow page loading, or even 502 Bad Gateway errors under heavy traffic.
In this guide, we’ll show you How to optimize PHP-FPM for Magento 2 on Nginx, step-by-step — improving efficiency and ensuring your store runs smoothly even under load.
If you haven’t configured PHP-FPM yet, start with our setup guide:
How to Configure PHP-FPM with Nginx
Why PHP-FPM Optimization Matters for Magento 2
Magento 2 is a resource-heavy eCommerce platform. Each request involves multiple PHP scripts, database queries, and cache operations. Poor PHP-FPM settings can easily create bottlenecks.
By optimizing PHP-FPM, you can:
- Reduce CPU and memory usage
- Improve page load times
- Handle more concurrent users
- Prevent server crashes under traffic spikes
Let’s explore how to fine-tune it effectively.
Steps To Optimize PHP-FPM For Magento 2
Step 1: Check Your PHP-FPM Configuration
Locate your PHP-FPM pool configuration file — usually here:
/etc/php/8.x/fpm/pool.d/www.conf
Use your PHP version (e.g., 8.1, 8.3).
Then open it for editing:
sudo nano /etc/php/8.x/fpm/pool.d/www.conf

This file controls how PHP-FPM spawns and manages worker processes — the most critical performance factor for Magento 2.
Step 2: Adjust pm Settings for Magento Workload
Key parameters:
| Parameter | Description | Recommended value (for Magento 2) |
pm | Process management mode | dynamic |
pm.max_children | Max concurrent PHP processes | 20–50 (depends on server RAM) |
pm.start_servers | Initial number of processes | 5 |
pm.min_spare_servers | Minimum idle processes | 5 |
pm.max_spare_servers | Maximum idle processes | 15 |
pm.max_requests | Requests per process before respawn | 500–1000 |
Example optimized configuration:
pm = dynamic
pm.max_children = 40
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 1000
Tip: To calculate pm.max_children, divide available memory by memory used per PHP process.
For example: 8GB RAM / 150MB ≈ 50 children.
Step 3: Enable Slow Log for Debugging
When PHP-FPM takes too long to execute a script, you can track it using a slow log:
request_slowlog_timeout = 5s
slowlog = /var/log/php8.x-fpm.slow.log
This helps identify problematic Magento extensions or database calls slowing down your site.
Restart PHP-FPM after saving changes:
sudo systemctl restart php8.x-fpm
Step 4: Tune Opcache for Magento 2
Magento 2 heavily benefits from PHP Opcache — it caches compiled PHP scripts in memory.
Edit your PHP configuration file:
sudo nano /etc/php/8.x/fpm/php.ini

Add or update these values:
opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=65407
opcache.revalidate_freq=2
opcache.validate_timestamps=1
The opcache.memory_consumption setting can be increased to 512MB or more depending on your extensions.
Step 5: Set Environment Variables for Magento 2
In /etc/php/8.x/fpm/pool.d/www.conf, ensure this line exists (Magento requires full environment access):
clear_env = no
This allows Magento to read variables like MAGE_MODE or Redis credentials correctly.
Step 6: Reload Services
After updating configuration files, reload both PHP-FPM and Nginx:
sudo systemctl reload php8.x-fpm
sudo systemctl reload nginx
Check that PHP-FPM is running properly:
sudo systemctl status php8.x-fpm
Step 7: Monitor PHP-FPM Performance
Use tools like:
htoportop– for real-time CPU/memory viewsudo systemctl status php8.x-fpm– to confirm active workers- New Relic APM – for deep insight into PHP transaction times
For a complete guide, read:
How to Use New Relic APM with Magento 2
Optimizing PHP-FPM for Magento 2 on Nginx isn’t just about faster page loads — it’s about server stability, scalability, and smoother customer experience.
By tuning your pm settings, enabling slow logs, and configuring Opcache effectively, you can dramatically boost your store’s performance.
Once your setup is stable, take optimization further by analyzing PHP performance in real time using New Relic APM — it helps identify bottlenecks that manual tuning can’t catch.