Tune pm.max_children, pm.start_servers, and OPcache buffers based on your server's exact RAM specifications
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.
Spin up an Ubuntu 24.04 instance with instant activation to test this tutorial:
Deploy IndSoft VPSA Linux VPS running PHP-FPM (7.4, 8.1, 8.2, or 8.3).
Run a process inspection command to measure the average RSS RAM consumed by active worker threads.
ps --no-headers -o 'rss,cmd' -C php-fpm8.3 | awk '{ sum+=$1; n++ } END { if (n > 0) print (sum/n)/1024 " MB"; }'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.
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 = 1000Maximize OPcache memory buffer to keep compiled bytecode in RAM without disk reads.
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-fpmRun this quick verification check to confirm your installation is responding properly:
sudo systemctl status php8.3-fpmQuick resolutions for frequent setup pitfalls and configuration issues.
pm.max_children is set too high for available physical RAM. Lower the value by 20-30%.
/var/log/php8.3-fpm.log means all workers are saturated. Either increase RAM to support more children or optimize slow database queries.
opcache.validate_timestamps=0 is set, you must run sudo systemctl reload php8.3-fpm to flush the bytecode cache after deployments.