Install WordPress with Varnish and Nginx on Ubuntu

 

Varnish is a web application accelerator. It redirects the visitors to static pages whenever it is possible. Varnish can be installed and configured to cache the content in front of any HTTP server and it can speed up the delivery up to 300-1000 times.

In this tutorial, we will show you how to increase the loading speed of your WordPress site by setting up Varnish in front of Nginx on an Ubuntu 16.04 server. We assume that you already have LEMP stack (Nginx, MySQL, and PHP) and WordPress installed on your Ubuntu 16.04 server.

Requirements

For this setup to work, you’ll need:

  • An Ubuntu 16.04 VPS. Preferably SSD so it’s faster.
  • A LEMP stack and WordPress installed on your server.
  • A root user.
  • A text editor.

This tutorial is for WordPress with Varnish and Nginx. If you want to use a different setup, follow our tutorial: Install WordPress with Memcached and Nginx on Ubuntu.

Log in to your server via SSH as user root

ssh root@IP_ADDRESS -p PORT_NUMBER

and make sure that all packages installed on your server are up to date

apt-get update && apt-get upgrade

Install and configure Varnish

Varnish is available in the official Ubuntu 16.04 repository, so we can easily install it using the apt-get command

apt-get install varnish

Once it is installed, we will configure Varnish to listen on port 80 and use the Nginx web server which will be listening on port 8080 as a backend.

Open the /etc/default/varnish file in a text editor

vi /etc/default/varnish

and do the following changes in the ‘## Alternative 2, Configuration with VCL’ section of the file

DAEMON_OPTS="-a :80 
-T localhost:6082 
-f /etc/varnish/default.vcl 
-S /etc/varnish/secret 
-s malloc,256m"

Edit the varnish.service file as well, and append the following configuration options:

systemctl edit varnish.service

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

Save the file and reload the changes

systemctl daemon-reload

Next, edit the default Varnish vcl file

vi /etc/varnish/default.vcl

and make sure that the following blocks look like the ones below

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

sub vcl_recv {
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
}

sub vcl_fetch {
if (!(req.url ~ "wp-(login|admin)")) {
unset beresp.http.set-cookie;
}
}

save the changes and restart Varnish for the changes to take effect

systemctl restart varnish

Configure Nginx

Open the Nginx configuration file of your WordPress site

vi /etc/nginx/sites-enabled/yourdomain.conf

and change the listening port to 8080

server {
...
listen 8080;
...
}

Make the same changes in the /etc/nginx/sites-enabled/default file too.

Stuck somewhere? Get a server from us and we’ll install, configure and optimize your WordPress server, free of charge!

Save the changes and test the Nginx configuration by executing the following command

nginx -t

The output should look like this

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

and finally, restart Nginx for the changes to take effect

systemctl restart nginx

Now once you access your website in a browser, it should be served through Varnish. And that’s it. You’ve successfully configured WordPress with Varnish and Nginx. Your WordPress site should be noticeably faster now. If you want to further speed up your site, follow our instructions here or get a quality, fast SSD VPS.

After you’ve configured everything, you can move onto securing your WordPress and securing your LEMP stack.

Of course, if you are one of our WordPress Hosting customers, you don’t have to do any of this, simply ask our admins, sit back and relax. Our admins will set this up for you immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons below or simply leave a comment in the comments section. Thanks.

Source