How to Identify and Fix High CPU Usage Caused by PHP-FPM
High CPU usage caused by the PHP-FPM (FastCGI Process Manager) process is a common issue, especially for WordPress sites with many plugins. This can lead to slow website performance, unresponsive pages, and in some cases, server crashes.
One of the main culprits behind this problem is the wp-cron.php
script in WordPress. This is a pseudo-cron job that runs on every page load, executing various tasks and background processes. As a website grows and the number of plugins increases, these tasks can start to queue up, never fully completing before the next round starts, causing the CPU to become overloaded.
In this article, we'll explore the root causes of high PHP-FPM CPU usage, and provide step-by-step solutions to track down and resolve the exact script causing the problem.
Understanding the Problem: What is PHP-FPM?
PHP-FPM (FastCGI Process Manager) is a popular process manager for PHP applications. It's responsible for handling the execution of PHP scripts and serving dynamic content to web servers like Nginx or Apache.
When a user requests a PHP-powered page, the web server forwards the request to PHP-FPM, which then spawns a new PHP process to handle the request. This process-based architecture allows PHP-FPM to scale and handle multiple concurrent requests efficiently.
However, if one or more PHP scripts are causing excessive CPU usage, it can lead to a situation where the PHP-FPM processes are constantly maxing out the available CPU resources, resulting in slow website performance and potentially even server crashes.
Identifying the Root Cause: The wp-cron.php Script
Something I've seen cause this on WordPress, especially on sites with lots of plugins, is the wp-cron.php
script. This is a pseudo-cron job that runs on every page load, executing various tasks and background processes.
As a website grows and the number of plugins increases, these tasks can start to queue up, never fully completing before the next round starts, causing the CPU to become overloaded.
To disable the built-in wp-cron.php
script, you can add the following line to your WordPress wp-config.php
file:
define('DISABLE_WP_CRON', true);
This will prevent the wp-cron.php
script from running on every page load, but it will also prevent certain WordPress features that rely on the cron system from working correctly. To work around this, you can add a cronjob to your server to run wp-cron.php
every 5 minutes:
*/5 * * * * wget -q -O - "http://example.com/wp-cron.php" > /dev/null 2>&1
If you're unfamiliar with cron, you can add this task by running crontab -e
in your server's console.
Tracking Down the Exact Script Causing High CPU Usage
While disabling the built-in wp-cron.php
script can help in some cases, it's not a guaranteed solution. There might be other scripts or plugins that are causing high CPU usage, and you'll need to identify the exact culprit.
Here's a step-by-step process to track down the script causing the problem:
-
Monitor PHP-FPM Processes: Use a tool like top
or htop
to monitor the PHP-FPM processes on your server. Look for any processes that are consistently using a high percentage of CPU.
-
Identify the Script: Once you've identified the high-CPU process, you can use the following command to find the corresponding script:
ps -ef | grep php-fpm | grep <PID>
Replace <PID>
with the process ID of the high-CPU process. This will show you the full command line, which should include the script that's causing the issue.
-
Profile the Script: Use a profiling tool like xdebug
or blackfire.io
to analyze the script and identify the specific functions or sections of code that are consuming the most CPU resources.
-
Optimize the Script: Based on the profiling results, you can then optimize the problematic code by reducing unnecessary operations, caching data, or parallelizing tasks.
-
Monitor and Validate: After making the necessary optimizations, continue monitoring the PHP-FPM processes to ensure the high CPU usage has been resolved.
Preventing High CPU Usage in the Future
To prevent high CPU usage caused by PHP-FPM in the future, consider the following best practices:
-
Keep WordPress and Plugins Up-to-Date: Outdated WordPress versions and plugins can contain bugs or inefficient code that can lead to high CPU usage. Regularly update your WordPress installation and all installed plugins to ensure you're using the latest and most optimized versions.
-
Minimize the Number of Plugins: While plugins can extend the functionality of your WordPress site, each one added can potentially introduce additional overhead and CPU usage. Carefully evaluate the plugins you install and only use ones that are necessary for your site's requirements.
-
Optimize Database Queries: Inefficient database queries can be a major contributor to high CPU usage. Review your site's code and plugins to ensure that database queries are optimized, and consider using caching mechanisms to reduce the load on the database.
-
Use a Content Delivery Network (CDN): Offloading static assets like images, CSS, and JavaScript to a CDN can significantly reduce the load on your web server and PHP-FPM processes, improving overall performance.
-
Monitor and Analyze Website Performance: Regularly monitor your website's performance using tools like Flowpoint.ai, which can help you identify technical issues, including high CPU usage, and provide actionable recommendations to improve your site's efficiency.
By following these steps, you can effectively identify and resolve high CPU usage caused by PHP-FPM, ensuring your WordPress site operates smoothly and efficiently
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.