PHP-FPM Configuration

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! 🎯

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *