Running multiple PHP-FPM versions (e.g., PHP 8.2 and PHP 8.4) on an Ubuntu server with at least 16 CPUs and 60GB RAM should ideally provide smooth performance. However, if your server is experiencing high load, it’s crucial to identify the root cause and optimize your setup.
In this guide, we’ll explore common issues that lead to high server load when running multiple PHP-FPM versions and provide practical solutions to resolve them.
Table of Contents
Why Is Your Server Under High Load?
When running multiple PHP-FPM versions, several factors can contribute to high CPU or memory usage:
- Too many PHP-FPM processes running simultaneously.
- Memory leaks in PHP applications.
- Inefficient database queries causing bottlenecks.
- High traffic or DDoS attacks overwhelming the server.
- Misconfigured OPCache leading to excessive CPU usage.
- Insufficient system resources due to poor optimization.
- Conflicts between PHP-FPM versions causing instability.
Let’s dive into each issue and explore how to fix it.
1. High PHP-FPM Process Count
PHP-FPM spawns multiple worker processes to handle requests. If these processes are not properly managed, they can consume excessive CPU and memory.
How to Check:
Run the following command to list all PHP-FPM processes:
ps aux | grep php-fpm
Check the status of each PHP-FPM version:
sudo systemctl status php8.2-fpm
sudo systemctl status php8.4-fpm
How to Fix:
Adjust the pm.max_children setting in the PHP-FPM pool configuration:
sudo nano /etc/php/8.2/fpm/pool.d/www.conf
sudo nano /etc/php/8.4/fpm/pool.d/www.conf
Set pm.max_children based on your server’s RAM. A general formula is:
max_children = (Total RAM - Reserved RAM) / (Memory per PHP process)
For example, if each PHP process uses 50MB and you reserve 10GB for other services:
max_children = (60GB - 10GB) / 50MB ≈ 1000
Start with a lower value (e.g., 50-100) and monitor performance.
Use pm = dynamic to allow PHP-FPM to adjust the number of processes dynamically:
pm = dynamic
pm.max_children = 100
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
Restart PHP-FPM after making changes:
sudo systemctl restart php8.2-fpm
sudo systemctl restart php8.4-fpm
2. Memory Leaks in PHP Applications
Memory leaks in PHP scripts can cause PHP-FPM workers to consume excessive RAM over time, leading to high server load.
How to Check:
Monitor memory usage per process:
top -c
Look for PHP-FPM processes consuming unusually high memory.
Check PHP error logs:
sudo tail -f /var/log/php8.2-fpm.log
sudo tail -f /var/log/php8.4-fpm.log
How to Fix:
Enable PHP memory limits in php.ini:
sudo nano /etc/php/8.2/fpm/php.ini
sudo nano /etc/php/8.4/fpm/php.ini
Set:
memory_limit = 256M
Adjust based on your application’s needs.
Profile PHP applications using tools like:
- Xdebug for memory usage analysis.
- Blackfire for performance profiling.
Optimize PHP code to reduce memory consumption.
3. Inefficient Database Queries
Slow or unoptimized database queries can cause PHP-FPM workers to wait, leading to high CPU usage.
How to Check:
Monitor database queries:
sudo apt install mysqltuner
sudo mysqltuner
Check slow query logs in MySQL/MariaDB:
sudo tail -f /var/log/mysql/slow.log
How to Fix:
- Optimize database queries (add indexes, rewrite queries).
- Use caching (Redis, Memcached) to reduce database load.
- Increase database server resources if needed.
4. High Traffic or DDoS Attacks
Sudden spikes in traffic or malicious attacks can overwhelm PHP-FPM, causing high server load.
How to Check:
Monitor network traffic:
sudo apt install iftop
sudo iftop
Check web server logs for unusual activity:
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/apache2/access.log
How to Fix:
Implement rate limiting in Nginx/Apache:
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
server {
location / {
limit_req zone=mylimit burst=20 nodelay;
}
}
Use a WAF (Web Application Firewall) like ModSecurity or Cloudflare.
5. Misconfigured OPCache
OPCache improves PHP performance by caching precompiled scripts, but misconfigurations can cause high CPU usage.
How to Check:
Verify OPCache settings:
sudo php8.2 -i | grep opcache
sudo php8.4 -i | grep opcache
How to Fix:
Enable and optimize OPCache in php.ini:
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
Restart PHP-FPM:
sudo systemctl restart php8.2-fpm
sudo systemctl restart php8.4-fpm
6. Insufficient System Resources
Even with 8 CPUs and 60GB RAM, poorly optimized applications or too many services can cause bottlenecks.
How to Check:
Monitor overall system load:
top
htop
Check disk I/O:
iotop
How to Fix:
- Upgrade server resources if consistently maxed out.
- Offload tasks (e.g., cron jobs, backups) to other servers.
- Use a CDN to reduce server load for static assets.
7. Conflicts Between PHP-FPM Versions
Running multiple PHP-FPM versions can sometimes cause conflicts if not properly isolated.
How to Check:
Verify socket permissions:
ls -la /run/php/
Ensure sockets are owned by www-data:
sudo chown www-data:www-data /run/php/php*.sock
How to Fix:
Use separate user pools for each PHP-FPM version:
sudo nano /etc/php/8.2/fpm/pool.d/user_pool.conf
Configure:
[user_pool]
user = user1
group = user1
listen = /run/php/php8.2-user1.sock
8. Log Analysis
Check PHP-FPM and web server logs for errors:
sudo tail -f /var/log/php8.2-fpm.log
sudo tail -f /var/log/php8.4-fpm.log
sudo tail -f /var/log/nginx/error.log
Recommendations
- Optimize PHP-FPM settings (
pm.max_children,memory_limit). - Profile and optimize PHP applications.
- Monitor database performance.
- Implement rate limiting and caching.
- Check for memory leaks and inefficient queries.
- Ensure proper OPCache configuration.
- Isolate PHP-FPM versions with separate pools.
Conclusion
Running multiple PHP-FPM versions on Ubuntu can be highly efficient if configured correctly. By following the steps outlined in this guide, you can reduce server load, improve performance, and ensure stability.