How to speed up your WordPress 100x in 10 minutes

Nowadays, WordPress is a key player in the Internet. Invented as a blog system, evolved to a bigger service used for example by newspapers or even online shops. Because it was purposed to do something really different, sometimes it might seem to be a little tardy. But what if I tell you that it can be made much faster in less than 10 minutes? Let’s see.

WordPress performance

To be completely sure that we are going in the right way, we need to start with inspecting current performance. To do this I have installed a new version of WordPress and added a couple of posts with additional custom fields. I have used an ab tool to analyze the performance. It’s not so important what we put into -c and -n parameters, but it’s necessary to keep them identical before, and after changes. In the example below the ab will send 10 000 requests.

The indicator that is most interesting for us it the number of requests per second: 94.62 [#/sec]. It’s looking better than the raw installation of Magento, but we want to do more. Our blog needs to be prepared for much, much bigger traffic, isn’t it?

before: 94.62 [#/sec]

Let’s start the magic

First of all, we need to know the bottlenecks. The most sensitive parts of all applications as a rule are: a database, business logic and views. Well, usually the applications do not contain much more than that, so let’s assume we can find a performance problem almost everywhere in the code.

What can be done about that? The users can be disallowed to access the executable part of server when it’s not necessary. We need to use some kind of cache, which can handle our requests before they will hit the Apache – and here comes Varnish.

Varnish configuration with WordPress and Apache

We will establish the Varnish as our main http server. All the users will hit it, and when it won’t find any matching record in cache then will ask Apache for the response.

To install Varnish at your Ubuntu or Debian server please use the apt (or yum in other cases).

1

sudo apt-get install varnish

Then we will need to move the Apache from the port 80 to some other – I chose the port 81.

The last thing to do is to tell the Varnish where it should call when any matching records in the cache are found. This is a backend service and could be found in a VCL file.

123456

#/etc/varnish/default.vcl

backend default{
.host="127.0.0.1";
.port="81";}

Is it working 100 times faster now? Not yet. At this point we have almost finished, but we still need to choose whether we want to use one of the WordPress plugins prepared to work with Varnish or make it faster without modifying anything in WordPress. I’ve chosen the second option.

Configuring the VCL

I have prepared a VCL configuration for my WordPress instance responsible for keeping all sites in cache for one hour and clearing the whole cache if I send any changes to the database by POST, PUT or DELETE. That is really simple and doesn’t require any purging or banning the content and I don’t need anything more at this point.

In result, our WordPress website can handle 7041.10 requests per second which is almost 100 times faster than in the beginning.

Conclusion

With Varnish’s VCL configuration’s language we can manage the cache without making any changes in the application’s code. The example was prepared to show how simple it can be for the uncomplicated WordPress websites. Of course, you need to work on your configuration if your code is more sophisticated than a raw WordPress installation but I think it’s a good place to start.

Sadly – very few have servers that allows installing custom software. Most uses virtual or shared servers, and then this kind of configuration is impossible :(.

http://piotrpasich.com Piotr Pasich

Yes, indeed. Ordinary users don’t have an access to apt and apache configuration which makes installing varnish impossible, but you can find a really cheap virtual servers (ex. on ovh) for about 3$ a month. So, problem solved.

gajdaw

I will give it a try the first thing in the morning. Great many thanks!

gajdaw

I have just finished a simple test: default page of Symfony Standard hosted on Digital Ocean.