Diagnosing and Resolving PHP-FPM High CPU Usage Print

  • Servers
  • 23

Welcome back to Serverhacks—a collection of tips, tricks, and troubleshooting guides for servers, networking, and system administration. I’m Corels from Emmanuel Corels Creatives, and in today’s article, we’re tackling a common challenge for web administrators: high CPU usage in PHP-FPM. When PHP-FPM (FastCGI Process Manager) consumes excessive CPU resources, your web applications can slow down or become unresponsive. In this guide, I’ll walk you through a systematic approach to diagnose the root cause of high CPU usage in PHP-FPM and implement effective solutions.


Step 1: Verify the Symptoms

Before diving into detailed diagnostics, it’s important to confirm that PHP-FPM is indeed the culprit.

  • Check CPU Usage:
    On your server, use tools like top or htop:

    top -o %CPU
    

    Look for PHP-FPM processes (often listed as php-fpm or similar) consuming a high percentage of CPU.

  • Identify Spike Patterns:
    Note whether the CPU usage spikes during specific times or when certain pages are requested. This can help narrow down whether the issue is related to a particular application or configuration.


Step 2: Review PHP-FPM Configuration

Misconfigurations in PHP-FPM can lead to inefficient resource usage.

  • Examine the Pool Configuration:
    PHP-FPM’s pool configuration files are typically located in /etc/php/7.x/fpm/pool.d/ (replace 7.x with your PHP version). Open the relevant pool file, for example:

    sudo nano /etc/php/7.4/fpm/pool.d/www.conf
    

    Check key parameters:

    • pm.max_children: This setting determines the maximum number of child processes. If set too high, it can overwhelm the CPU.
    • pm.start_servers, pm.min_spare_servers, pm.max_spare_servers: These control the number of processes that start and remain idle. Over-provisioning here can also lead to high CPU usage.

    Tip: Consider lowering pm.max_children if your server hardware is limited, or tuning the spare server settings based on your typical load.

  • Log Slow Requests:
    Enable slow log in PHP-FPM to capture scripts that take too long to execute:

    request_slowlog_timeout = 5s
    slowlog = /var/log/php7.4-fpm.slow.log
    

    This will help you identify which PHP scripts are causing bottlenecks.


Step 3: Monitor and Analyze Logs

Logs provide invaluable insights into what’s happening under the hood.

  • PHP-FPM Error Log:
    Check the PHP-FPM error log for any warnings or errors that might indicate a problem:

    sudo tail -n 50 /var/log/php7.4-fpm.log
    
  • Slow Log Analysis:
    Review the slow log file you configured. Look for frequently occurring slow scripts and assess whether they can be optimized.

    sudo tail -n 50 /var/log/php7.4-fpm.slow.log
    
  • Web Server Logs:
    Also check your web server’s logs (e.g., Apache or Nginx). High CPU usage in PHP-FPM can sometimes correlate with specific HTTP requests that are resource-intensive.


Step 4: Test Application Performance

Identify if specific application code is responsible for high CPU usage.

  • Profile PHP Scripts:
    Use tools like Xdebug or Blackfire to profile the performance of your PHP applications. These tools can pinpoint inefficient functions or database queries.

  • Benchmark and Stress Test:
    Tools like Apache Benchmark (ab) or JMeter can simulate traffic to help identify how your PHP-FPM configuration performs under load:

    ab -n 1000 -c 10 http://yourdomain.com/some_script.php
    

Step 5: Adjust and Optimize

Based on your findings, implement the following adjustments:

  • Tune PHP-FPM Settings:

    • Reduce pm.max_children to a value that matches your server’s capabilities.
    • Adjust spare server settings (pm.min_spare_servers and pm.max_spare_servers) to better match your typical traffic.
  • Optimize Application Code:

    • Refactor inefficient PHP scripts.
    • Optimize database queries that might be causing long execution times.
  • Implement Caching:
    Consider using an opcode cache like APCu or OPcache, and an HTTP accelerator (e.g., Varnish) to reduce the load on PHP-FPM by serving cached responses.

  • Hardware Considerations:
    If the CPU is consistently overburdened despite optimizations, consider upgrading your server resources or distributing the load across multiple servers.


Step 6: Monitor Post-Optimization

After applying your changes, continuously monitor the performance:

  • Use top and htop Regularly:
    Monitor CPU usage to see if the adjustments have stabilized resource consumption.
  • Log Analysis:
    Continue reviewing error logs and slow logs to ensure no new issues have emerged.
  • Feedback Loop:
    Regularly gather feedback from users regarding performance improvements and iterate on your configuration as needed.

Final Thoughts

High CPU usage in PHP-FPM can be caused by a variety of factors—from configuration settings and inefficient application code to external traffic spikes. By following a systematic approach—verifying system resources, reviewing configuration files, analyzing logs, testing application performance, and applying targeted optimizations—you can significantly improve the performance of your PHP-FPM processes.

I hope this guide helps you troubleshoot and resolve high CPU usage issues effectively. Remember, the key is to iteratively monitor and adjust your setup based on real-world usage. If you have any questions or need further assistance, feel free to reach out. Happy troubleshooting, and here’s to a faster, more efficient server environment!


Explained with clarity by
Corels – Admin, Emmanuel Corels Creatives


Does this help?

« Back