Installing WordPress with Varnish, Nginx on Ubuntu

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

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


For this installation to work, you need:

  • Ubuntu 16.04 VPS. An SSD is preferable as it is faster.
  • LEMP stack and WordPress installed on your server.
  • The root user.
  • Text editor.

This tutorial is for WordPress with Varnish and Nginx.

Login to the server via SSH as a superuser

ssh [email protected]_ADDRESS -p PORT_NUMBER

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

apt-get update && apt-get upgrade

Installing and Configuring 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 listen on port 8080 as the backend.

Let’s open the / etc / default / varnish file in a text editor

vi /etc/default/varnish

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

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

Edit the varnish.service file and add the following configuration parameters:

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

Then change the default Varnish VCL file

vi /etc/varnish/default.vcl

and make sure the following blocks look the same

backend default {
.host = "";
.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 your changes and restart Varnish for the changes to take effect

systemctl restart varnish

Configuring 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 to the file /etc/nginx/sites-enabled/default also.

Save your changes and check your Nginx configuration by running 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, as soon as you access the website in a browser, it should be served through Varnish. That’s all. You have successfully configured WordPress with Varnish and Nginx. Your WordPress site should be noticeably faster.

Once you’ve configured everything, you can move on to securing your WordPress and securing your LEMP stack.