How To Fix High CPU Usage In WordPress

How To Fix High CPU Usage In WordPress


This article will take you through some methods to fix common causes of high CPU usage in WordPress. 

Update WordPress 

This is the easiest fix, so it’s best to start here before getting into the more complicated ones. 

Missing a major WordPress update will cause your site to perform sub-optimally and, in turn, create a greater CPU load on the host server. 

Luckily, updating WordPress is as simple as a single click! Simply go to Dashboard>Updates. Here you can see if there’s a new version of WordPress available to be installed. Click “Update Now”, and you’re good to go!

Check your plugins

Plugins are one of the most common causes of performance issues in WordPress. There are several ways plugins drain your CPU resources:

  • Some plugins run constant background tasks
  • Some plugins might be out of date or not running a stable version 
  • Some plugins come with a load of features and settings you don’t use but that are still running in the background
  • Plugins can implement redirects that cause a redirect loop

You can use the WP Hive Chrome Extension to check the impact your plugins are having on site performance. Or you can do it the old-fashioned way by disabling plugins to see what impact that has.

Make sure your site is optimized

If your WordPress CPU usage is up to 90+%, then it’s most likely caused by hosting or traffic spikes, and you should address those areas first. However, it’s always a good idea to streamline your site and reduce the CPU load with some simple tips to speed up your WordPress site. These include:

Disable WP-CRON

Cron is a command line utility that schedules tasks to be performed automatically. These commands are known as “cron jobs”. In WordPress, cron jobs are handled by WP-CRON. However, WP-CRON is simply a function that WordPress uses to imitate the functionality of a real cron job. 

WP-CRON can cause performance issues because it fires every time a page is loaded instead of on a defined schedule. This means that with high traffic, WP-CRON is checking for scheduled events each time someone loads a page and putting extra strain on the server. On the other hand, if the site doesn’t have very much traffic, a scheduled event might be missed if nobody loads the page. 

For a lot of people, it’s better to simply disable WP-CRON and use a real cron job instead. 

How to disable WP-CRON

To disable WP-CRON, you’ll need to edit your wp-config.php file and add this command:

define('DISABLE_WP_CRON', true);

Put it just before the line that says “/*That’s all, stop editing! Happy publishing. */”.

disable cron

How to make your own cron jobs

Disabling WP-CRON only stops it from executing automatically, but you can still set up real cron jobs that execute the wp-config.php on command. 

There are a few different ways you can do this:

  • You may be able to set up cron jobs through your hosting provider’s control panel. 
  • If you use cPanel, you can schedule cron jobs by going to Advanced>Cron Jobs and setting it up there. 
  • Use third-party plugins like WP Crontrol
  • Configure cron jobs manually in SSH

Offload content to a Content delivery network

content deliver network

When someone visits your site from a country far away from the host server, it takes longer for the data to reach their computer. The extra time spent delivering these resources takes up processing power in the host server. 

The answer to this problem? CDNs (Content Delivery Networks).

A content delivery network is a system of geographically distributed servers that contain cached versions of static assets from your site. You can offload content to these servers so that users receive assets from the servers closest to them. This results in quicker loading times, a better user experience, and, best of all, it massively reduces the CPU usage from your WordPress site.

Block Bad Bots 

No doubt you’re already familiar with Google’s bots. These are the “good bots”. They crawl and index your site for search engines, and your SEO revolves around them.

However, there are a lot more bots that are visiting your WordPress site every day. A lot of these bots aren’t malicious, but they contribute to your site’s traffic which can put unnecessary strain on your CPU. 

You can consult this list of common crawling bots to know which ones to block from your website. To block a particular user agent from crawling your site, you need to create a rule in your robots.txt file using the following format:

User-agent: BotName
Disallow: / 
block bad bots

The “/” means you’ve blocked them from the root directory and, consequently, your entire site. If you want to only disallow bots from a certain directory, you can write:

User-agent: BotName

Disallow: /DirectoryName/

You can also block bots through your .htaccess file by adding the following rule: 

RewriteEngine On
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} UserAgentName [NC]
RewriteRule .* - [F,L]
To block multiple bots, format your rule like this: 
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^.*(Bot1|Bot2|Bot3).*$ [NC]
RewriteRule .* - [F,L]

Alternatively, you can use a plugin to catch the pesky bots and save you the effort of checking and banning them manually.

I’d recommend the Blackhole for bad bots plugin. It’s got quite a clever way of catching bad bots that’s a bit like trapping a wasp in a glass of sugar water. 

The plugin adds a link to your site that is only visible to crawlers. This link serves as bait.

You then add a rule in your robots.txt, forbidding bots from following this link. Of course, spam bots will disregard this rule and follow the link. Once they do, they are instantly banned from accessing your site. 

Quite clever, isn’t it? 

Review your hosting options

If your WordPress site is struggling with high CPU usage, it’s likely that you’re not getting enough server resources from your hosting provider. This is especially true if you’re using Shared Hosting. Shared Hosting is one of the most common causes of overworked CPUs as you’re constantly competing for power with all the other websites hosted on a server.

If you think you’ve outgrown your Shared Hosting, then it’s time to check out the other types of hosting providers:

  • Virtual Private Server (VPS) Hosting: You still share a server, only it’s divided into virtual partitions so you can have dedicated resources like memory, CPU cores, and storage. You’re effectively isolated from the other sites, so a sudden traffic spike to another site on the server won’t affect you.
  • Cloud Hosting: Cloud hosting is getting a fair bit of traction these days. Essentially it works a bit like a CDN. Your site is stored across a network of virtual and physical servers instead of a single server. Cloud hosting is often cheaper as you only pay for the resources you use. Also, since you’re spread over multiple servers, you get extra security, more reliable uptime, and essentially infinite scalability. 
  • Dedicated Hosting: Often thought of as the crème de la crème of web hosting, this option provides your very own server, giving you 100% control over the server resources you use. On the flip side, it’s usually the most expensive option and requires some technical expertise to manage properly.
  • Managed Hosting: With Managed Hosting, your host provider essentially takes care of your server for you. They’ll cover things like maintenance, updates, and security. This is more of an add-on to other types of hosting. You could have Managed Shared Hosting or Managed Dedicated Hosting. 

Conclusion

Reducing CPU usage will vastly improve your WordPress site’s overall health, leading to increased performance, more consistent uptime, and a better user experience. While many of the tips in this article will help you cut down on CPU load, if you’re consistently overloading your site’s CPU, then it’s almost definitely a hosting problem.



Source link