Pair Networks Blog How To,Tutorials,WordPress How to Put Varnish Cache on Your WordPress Site

How to Put Varnish Cache on Your WordPress Site

How to Put Varnish Cache on Your WordPress Site post thumbnail image

How to Put Varnish Cache on WordPress Header Image

What is Varnish Cache?

Varnish Cache is a web accelerator, which is used to speed up site loading times. So, how does it work? Well, a web accelerator acts as a proxy server that reduces the time it takes to access a website. A proxy server operates as a go-between for requests to the server. When a computer wants to load a site, it will contact the server and request the site’s information and resources. A proxy server intercepts those requests and processes them according to its instructions.

So, Varnish Cache is a proxy that processes requests in a way that boosts speed, resulting in website loads up to 1000 times faster.

Varnish is installed on all of pair’s WP accounts, right out of the box.

What Does Varnish Do?

Varnish Cache uses caching to store data in virtual memory. Instead of having each requesting computer cache the website resources, the cache is already there and ready to be used in the proxy. This can cut load time drastically, especially if your site has a complex theme or lots of traffic.

Varnish Cache is used by big-name companies, like the New York Time’s website and Twitch. Consequently, to accommodate demand, we have Varnish Cache built into our WP Hosting packages so your sites are always at their best.

Why Would I Want Varnish Cache on My WordPress Site?

Why would I want to use Varnish Cache on my WordPress site? The main reason is that it makes your site load faster for visitors. In a society so saturated with virtual influences, people expect immediate gratification when surfing the web. If your page doesn’t load immediately, they may very well click away in search of a faster loading site.

If you have complex themes or lots of traffic, Varnish Cache can help keep your website from being bogged down.

How Do I Set Up Varnish Cache?

So how do you get Varnish Cache on your WordPress site? We have the instructions here. However, if you don’t want to bother with all this setup, you can instead just use one our WP Hosting packages. They come with Varnish Cache built-in. That means no installation needed. It’s already there and ready to speed up your site.

Step 1: Installing Varnish

The first thing you have to do is install Varnish Cache. Go to Varnish Cache’s website and download the Varnish Cache package for your server’s operating system. Once you’ve downloaded it, install it onto your server according to your package’s instructions. Using a package manager may be helpful here. 
If you want to use Varnish Cache Plus, or any of Varnish Software’s premium content, check out the Varnish Software website for details about installing them.

Step 2: Add the Plugins

For Varnish Cache to work, you need to add some plugins to your WordPress. These plugins help keep Varnish Cache up and functioning properly.

The first plugin that should be added is meant to purge cached data whenever you make changes to the site. If you don’t have something to purge the cache, the site won’t update with your changes! So check out a Varnish Purge plugin. You can use the Varnish VCaching plugin for this.

In addition, you can also use  W3 Total Cache plugin if you’re using a CDN and WPBase Cache to optimize Varnish Cache on WordPress.

Step 3: Enable Custom Permalinks

To enable custom permalinks, go into the WordPress dashboard and click Settings and then click the Permalink in the drop-down. In the Permalinks Settings section, select Custom Structure. In the box next to Custom Structure, input /%year%/%monthnum%/%post_id% and click Save Changes.

After this, open a command prompt (Windows) or terminal (Mac) and log into your website as root and run a2enmod rewrite.

Step 4: Change Apache Port

Next, you need to change the Apache port of your site from port 80 to port 8080 in the /etc/apache2/ports.conf file and all files in the /etc/apache2/sites-enabled/ folder. Open all the files in this folder and change all instances of port 80 to port 8080.

Note: Using CTRL-F will help speed this process up considerably.

Step 5: Varnish Binary

For the next step, you have two options:

Step 6: Configuring the Backend

Next, pen your /etc/varnish/default.vcl file. Make sure the following is present in the file:

backend default {
       .host = “127.0.0.1”;
       .port = “80”;
}

If it is not there, add it.

Step 7: Restart Apache/Varnish

Restarting both Apache and Varnish allows Varnish to start functioning after it reboots. At this point, Varnish will start to intercept all the traffic before it makes it to your server. As a result, your site will load faster.

To restart Apache and Varnish, open a Command Prompt (Windows) or terminal (Mac) and log into your website as root. Run:

sudo systemctl restart varnish.service
sudo systemctl restart apache2.service

Note: Depending on your hosting operating system and how it’s set up, you may use “service” instead of “systemctl.”

Step 8: No Cookies Needed

WordPress uses many tracking cookies that are no longer needed when using Varnish. For this reason, we want to disable them by going to /etc/varnish/default.vcl file and inputting the following code inside of sub vcl_recv. Add:

set req.http.cookie = regsuball(req.http.cookie, "wp-settings-\d+=[^;]+(; )?", "");

  set req.http.cookie = regsuball(req.http.cookie, "wp-settings-time-\d+=[^;]+(; )?", "");

  set req.http.cookie = regsuball(req.http.cookie, "wordpress_test_cookie=[^;]+(; )?", "");

  if (req.http.cookie == "") {

  unset req.http.cookie;
  }

Step 9: Don’t Cache Login URLs

While Varnish does a great job caching your pages and speeding up delivery, you don’t want it to cache your login pages. Caching a login page can result in a lot of login troubles down the road.

In order to keep Varnish from caching your login URL, open the /etc/varnish/default.vcl file and add the following code to the sub vcl_recv section:

if (req.url ~ "wp-admin|wp-login") {

return (pass);
}

Step 10: Establish a Cache Time

Now, we need to establish how long Varnish will cache information for. If we don’t make any changes, Varnish will refresh the cache every two minutes! While we do want Varnish to refresh to pick up on new changes, every two minutes is too often. 

Instead, we can change the refresh rate by opening /etc/varnish/default.vcl and inserting the following code:

sub vcl_backend_response {

  if (beresp.ttl == 120s) {

    set beresp.ttl = 1h;

  }
}

This will change the refresh rate time to an hour.

Step 11: Setting Up Purge Request Procedure

Next, we want to prime the Varnish code to work with Varnish HTTP plugin in WordPress. Together, they clean out old content and make sure the cache is up-to-date with your newest changes.

So, open  /etc/varnish/default.vcl and add the following code to the top of vcl_recv. This code establishes which IP addresses are allowed to access the configuration files. For this reason, be sure to input your own IP addresses here. You can add more than what is listed, just make sure to separate with semicolons:

acl internal {
  "192.x.x.x"/24;
  xxx.xxx.xx.xx;
}

This next section of code establishes how purge requests are handled. It should be added to vcl_recv.

if (req.method == "PURGE") {

  if (client.ip !~ purge) {
   return (synth(405, “Not allowed.”));

  } else {

    return (purge);

  }
  }

The code will look something like this after your additions:

acl purge {

    "localhost";

    "127.0.0.1";

}


acl local {

    "127.0.0.1";

    "::1";

}


sub vcl_recv {

  if (req.method == "PURGE") {

    if (!client.ip ~ purge) {

    return(synth(405,"Not allowed."));

    }


} else {

    return (purge);

  }
  }

Step 12: Adding Purge Security

This next addition establishes the hostname allowed to instigate a purge. To do this, in  /etc/varnish/default.vcl file, after backend default{…}, add:

acl purge {
  "localhost";
  server ip address or hostname;
}

Be sure to replace the “server IP address or hostname” with your IP address or server hostname!

Step 13: Reloading Varnish

Don’t worry! We’re almost done. There are only a few tasks left. First, we need to reload Varnish so that all it will use all the changes we just made. To do this, open command prompt (Windows) or terminal (Mac) and log into your website as root. Run:

Sudo systemctl reload varnish.service

This will reload Varnish so it can use our previous changes.

Step 14: Removing Any Cache Buildup

While we were working, a cache may have already built up on your site. To start fresh, visit your Varnish HTTP Purge plugin and click Purge Varnish at the top.

Step 15: Check Your Website Traffic

You can check the requests Varnish is processing by running varnishlog in command prompt (Windows) or terminal (Mac) as root.

All Done!

Varnish Cache is a great way to speed up slower sites and keep your content up-to-par with visitors that expect quick load times. Now you can use that complex theme or plugin you had your eye on without worrying about severely slowing down your site.

Want Varnish Cache, but don’t want the hassle of installing it on your server? Grab one of our WP Hosting packages! They come with Varnish Cache already installed, so there’s virtually no work on your end. Also, right now, your first month with WP Hosting is free!

Already have a host? No problem. We have a handy guide on how to migrate over. If you still have questions, you can call our 24/7, Pittsburgh-based support team. They’ll be happy to help you with anything you may need.

Whether you’re installing it yourself or using the built-in version on our WP Hosting packages, Varnish is a great asset to have.

Related Post