Install WordPress with Varnish and Nginx on Ubuntu

Install WordPress with Varnish and Nginx on Ubuntu

We’ll show you how to 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.

1. 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

2. 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

3. 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.

[ecko_alert color=”blue”]Stuck somewhere? Get a server from us and we’ll install, configure and optimize your WordPress server, free of charge![/ecko_alert]

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

4. Test Nginx Configuration

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

5. Restart Nginx Server

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 on how to speed up and optimize WordPress 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 Install WordPress with Varnish and Nginx on Ubuntu, simply ask our admins, sit back and relax. Our admins will Install WordPress with Varnish and Nginx on Ubuntu, for you immediately.

PS. If you liked this post on how to Install WordPress with Varnish and Nginx on Ubuntu, please share it with your friends on the social networks using the buttons below or simply leave a comment in the comments section. Thanks.

5 thoughts on “Install WordPress with Varnish and Nginx on Ubuntu”

  1. Thank you for the very informative article!
    Just want to ask, should i also change the 443 nginx listening port to 8080?
    Should i clear the varnish cash from wordpress?
    Thank you for your time.

    Reply
    • You need to edit the vanish default.vcl file and add the following code at the end of the file:
      unset resp.http.X-Varnish; unset resp.http.Server; unset resp.http.Via; unset resp.http.Link;

      Reply

Leave a Comment