Performance Tuning Advanced 20 min

How to Optimize PHP-FPM Performance for High Traffic Websites

Tune pm.max_children, pm.start_servers, and OPcache buffers based on your server's exact RAM specifications

IndSoft Cloud Engineering
Reviewed October 2026
Production Verified Commands

Guide Overview & Architecture Outcome

By following this guide, you will calculate and configure optimal PHP-FPM pool settings based on your available server RAM. This prevents out-of-memory kernel panics and eliminates 502 Bad Gateway errors during traffic surges.

Prerequisites & System Requirements

A Linux VPS running PHP-FPM (7.4, 8.1, 8.2, or 8.3).

1

Determine Average Memory Usage per PHP Process

Run a process inspection command to measure the average RSS RAM consumed by active worker threads.

bash · step-1-command.sh
ps --no-headers -o 'rss,cmd' -C php-fpm8.3 | awk '{ sum+=$1; n++ } END { if (n > 0) print (sum/n)/1024 " MB"; }'
Expected System Output:
45.2 MB
2

Calculate pm.max_children for Your Available RAM

Formula: Max Children = (Total Dedicated RAM - OS/DB Reserve) / Average Process Size. Example for 8 GB RAM server with 3 GB reserved for OS/MySQL: (5000 MB) / 45 MB = ~110 children.

bash · step-2-command.sh
sudo nano /etc/php/8.3/fpm/pool.d/www.conf # Set: # pm = dynamic # pm.max_children = 110 # pm.start_servers = 25 # pm.min_spare_servers = 15 # pm.max_spare_servers = 35 # pm.max_requests = 1000
Expected System Output:
Configuration updated.
3

Tune PHP OPcache Settings in php.ini

Maximize OPcache memory buffer to keep compiled bytecode in RAM without disk reads.

bash · step-3-command.sh
sudo nano /etc/php/8.3/fpm/php.ini # opcache.enable=1 # opcache.memory_consumption=256 # opcache.max_accelerated_files=20000 # opcache.validate_timestamps=0 # on production sudo systemctl restart php8.3-fpm
Expected System Output:
PHP-FPM restarted.

Check That It Worked (Verification)

Run this quick verification check to confirm your installation is responding properly:

verify-service.sh
sudo systemctl status php8.3-fpm
Expected Response:
Active: active (running) with 25 worker pools waiting for connections.

Troubleshooting Common Errors

Quick resolutions for frequent setup pitfalls and configuration issues.

Diagnostic Tips
Server swap usage spikes to 100%
Your pm.max_children is set too high for available physical RAM. Lower the value by 20-30%.
WARNING: [pool www] server reached pm.max_children setting
This warning in /var/log/php8.3-fpm.log means all workers are saturated. Either increase RAM to support more children or optimize slow database queries.
Code changes not appearing immediately
If opcache.validate_timestamps=0 is set, you must run sudo systemctl reload php8.3-fpm to flush the bytecode cache after deployments.