Thanks for the clarification! Since the database server is separate, we can allocate more RAM to PHP-FPM because MySQL/PostgreSQL isnβt consuming memory on this server.
πΉ Adjusted PHP-FPM Configuration for 16GB RAM & 4 Cores
Since the database is on a different server, we assume PHP-FPM, Apache/Nginx, Redis, and OS services as the main consumers of memory.
1οΈβ£ Determine pm.max_children
Formula:
pm.max_children = (Total Available RAM - Reserved RAM) / Average PHP Process Size
- Available RAM: 16GB
- Reserve for OS, Webserver, Cache, and Other Services: ~3GB
- PHP Process Size: ~100MB (Check with
ps -ylC php-fpm8.2 --sort:rss | awk '{sum+=$8} END {print sum/NR/1024 " MB"}')
π‘ Calculation:
(16GB - 3GB) / 100MB = 130
π Recommended: pm.max_children = 120 (keeping buffer)
2οΈβ£ Set pm.start_servers
Formula:
pm.start_servers = Total CPU Cores * 2
For 4 cores:
pm.start_servers = 4 * 2 = 8
π If traffic is high, increase to 10-12.
3οΈβ£ Set pm.min_spare_servers & pm.max_spare_servers
Formula:
pm.min_spare_servers = pm.start_servers / 2
pm.max_spare_servers = pm.start_servers * 2
For 10 start servers:
pm.min_spare_servers = 5
pm.max_spare_servers = 20
π Recommended:
pm.min_spare_servers = 5
pm.max_spare_servers = 20
4οΈβ£ Set pm.process_idle_timeout
- For consistent traffic: Keep at 10s (default).
- For fluctuating traffic: Increase to 20-30s to avoid frequent process respawning.
π₯ Final PHP-FPM Configuration
pm = dynamic
pm.max_children = 120
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.process_idle_timeout = 10s
π Performance Monitoring
After applying the settings, restart PHP-FPM:
sudo systemctl restart php8.2-fpm
Monitor CPU/RAM usage:
htop
free -m
Check PHP-FPM process usage:
ps aux | grep php-fpm
Check logs for PHP-FPM activity:
sudo tail -f /var/log/php8.2-fpm.log
π Alternative: Use pm = ondemand for Variable Load
If your traffic is unpredictable:
pm = ondemand
pm.max_children = 120
pm.process_idle_timeout = 20s
This ensures PHP processes only run when needed, saving memory.
π These settings should provide optimal performance for your setup! Try them and monitor for any needed tweaks. Let me know if you need further adjustments! π―