Solving WordPress CPU and RAM Spikes

Working with WordPress over the years I’ve become familiar with the software, its bugs, and the quirks you encounter hosting WordPress in different server environments. When I first started with…

Working with WordPress over the years I’ve become familiar with the software, its bugs, and the quirks you encounter hosting WordPress in different server environments.

When I first started with WordPress, I had it installed for me using a one-click installation on DreamHost. Later, as my hosting needs grew and my sites started to load slow on a shared-hosting environment, I ended up getting my own Virtual Private Server (VPS).

My hosting provider assured me that getting a VPS would improve my site load times. For a little while, the switch to a VPS improved things. But then, my sites began to load slowly again and I started to look into different hosting options.

Digital Ocean

One of my brothers told me about Digital Ocean, a high-quality hosting provider that has VPS packages starting from $5/month.

While this article isn’t about Digital Ocean–it’s about WordPress–the switch to Digital Ocean, and the graphing and data tools they provide, helped me diagnose and solve the CPU and RAM spikes I was seeing on my WordPress installations.

(Quick note: I do recommend Digital Ocean, and so if you do decide to use their service, please use this affiliate link.)

Now, on to the solution to a slow loading WordPress site with CPU and RAM spikes.

WordPress CPU and RAM Usage

Digital Ocean provides a few useful graphs for each website (they call them “droplets”) that you host with them. It was after moving a WordPress site to Digital Ocean that I noticed a spike in CPU usage when the sites were loading slowly.

After investigating further I learned that it wasn’t just the CPU that spiked, but the RAM usage as well.

Digital Ocean has a one-click WordPress installation option and that is what I had originally used. To solve my problem, and to solve yours if you’re reading this, you’ll need to learn how to install WordPress yourself.

You’ll also need to learn a little about the NGINX web server.

Apache, NGINX, and RAM Management

WordPress is traditionally installed on a LAMP stack. LAMP stands for Linux (the operating system), Apache (the web server), MySQL (the database), and PHP (the programming language).

To solve your problem you’re going to need to install WordPress on a LEMP stack. LEMP stands for Linux, a web server called NGINX (pronounced Engine-X), MySQL, and PHP.

Apache is the world’s most common web server and it does a lot of things really well. Unfortunately, even though Apache is traditionally used as the web server for WordPress, and because of how Apache and WordPress interact, the combination tends to eat up a lot of RAM.

Once the RAM is overloaded, Linux attempts to use memory swapping. Essentially, it tries to use your hard drive to perform functions that should be taken care of by RAM.

This is why your CPU spikes.

The NGINX web server does a much better job at managing RAM in the WordPress context than Apache does. There are a number of speed tests online comparing a LAMP and LEMP WordPress configuration, and all of them demonstrate that NGINX manages WordPress resources significantly better than Apache does.

However, because the LAMP stack is the traditional setup for WordPress you can’t find a shared hosting or one-click installation for NGINX and WordPress anywhere. What that means is that you’ll have to install it yourself.

Lucky for you, Digital Ocean provides a guide that you can follow to install WordPress on a LEMP stack.

Getting Started with Digital Ocean

The instructions I’m posting are for installing WordPress on a LEMP stack on Digital Ocean, but you should be able to adapt the instructions to any hosting provider that will give you shell access to your own VPS.

I’m also not going to be posting detailed instructions. Instead, I’ll be directing you with links to resources and instructions that have already been written out at Digital Ocean.

What’s amazing about all this is that you’re going to get a faster WordPress website, with no CPU or RAM spikes, for only $5/month.

As an interesting note, the $5/month VPS gives you 512 MB of RAM. With a LEMP + WordPress configuration, you can host a website on a $5/month VPS.

On Digital Ocean’s one-click install of WordPress using the Apache web server, they no longer allow you to use the $5 droplet–you must begin with the $10/month droplet which provides 1GB of RAM.

That’s not really a statement about whether Apache does a bad job, as it is an indicator of how great NGINX works with WordPress.

Back to the droplet.

Creating Your Droplet on Digital Ocean

After clicking “Create Droplet,” click “One-click Apps” and select the option to install the LEMP stack.

Screen Shot 2016-01-21 at 2.40.26 AM

Scroll down and choose the $5/month VPS.

Screen Shot 2016-01-21 at 2.44.47 AM

As you keep scrolling you’ll see the option to choose a datacenter region–just pick what makes sense for your situation.

Under “Select Additional Options” I always select Backups. It costs 20% extra of the price of your droplet (So $6/month total for a $5/month VPS), but it’s totally worth it. The peace of mind that comes from having an affordable and automatic weekly backup is impossible to state. Not to mention the fact that I’ve had to use their backups and the process is completely seamless.

Screen Shot 2016-01-21 at 2.45.07 AM

Finally, be sure to use an SSH key and type a hostname.

For the hostname, I typically use the URL or web address of the site I’m creating, but it could be anything you want.

Click the “Create” button and you’ll be on your way.

Now comes the fun part–installing WordPress.

Installing WordPress with NGINX

Like I said, Digital Ocean provides useful guides for installing WordPress on NGINX. Here is the page you want to visit:

https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-nginx-on-ubuntu-14-04

If you’ve never done anything from the command line before there is definitely a learning curve. Give this a try–spend a couple of hours learning how this works and you won’t regret it.

(TIP: Try googling for resources, but since you’re working with Digital Ocean and they provide so much information, just tag on “digital ocean” to the end of your search terms to get applicable resources)

Ever since I switched my WordPress installations from a LAMP stack to a LEMP stack I haven’t had any problems with excessive RAM usage or CPU spikes. What’s even better is that people who know more about server configurations and traffic tests than I do have confirmed that WordPress on a LEMP stack and a $5 VPS can handle millions of website visits each day

Extra Steps

There are a couple of other final steps you might want to keep in mind. After installing WordPress on NGINX using the steps listed above, some installations, mine included, have trouble sending email from the WordPress installation.

Just type the following into the command line interface.

sudo apt-get install sendmail

sudo sendmailconfig

Select “y” for all the following configuration options. Then type this next bit.

sudo service sendmail restart

Varnish Page-caching

Another thing you might want to do is install Varnish. Varnish is a super-charged page-caching software that you can install by following the instructions here:

https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-nginx-php-and-varnish-on-ubuntu-12-04

When I install Varnish on one of my websites after following the instructions listed above, I can get my sites to load in less than 300ms. That’s blazing fast.

So by switching your installation from Apache to NGINX, you’re not just avoiding your slow-loading CPU-spiking WordPress issues, you’re actually gearing up for a faster-loading website than you’ve ever experienced before. Varnish just makes the experience even better.

One warning about Varnish–it’s complex stuff. WordPress uses a lot of cookies and configuring Varnish to work properly with a WordPress site more complicated than a regular blog can be difficult.

Varnish also doesn’t work on sites loaded over an SSL connection, so it may not be a great page-caching option for websites that collect sensitive user-information like online stores.

Conclusion

Moving from Apache to NGINX vastly improved my WordPress websites. My sites no longer load slowly, and I have no issues with RAM or CPU spikes. Taking the time to learn how to configure my own web server gave me power to improve the website experience for my end users–my site visitors.

Most of what you read online about speeding up WordPress has to do with page-caching and optimizing Apache. But I recommend making the change from Apache to NGINX. Page-caching options, such as Varnish or a WordPress cache plugin, should be considered if you need things to move even faster.

What strategies do you recommend for managing server resources on your WordPress installation?