How to Install and configure Varnish in Ubuntu 16.04

August 5, 2017 4.6k views
LEMP Nginx WordPress Server Optimization Caching Ubuntu 16.04
sp234sp
By:
sp234sp

I already install NGINX, PHP-FPM, MariaDB and Wordpress in Ubuntu 16.04. Also, I have Let's Encrypt SSL.

Now I want to install Varnish and configure it in the backend of NGINX since I used SSL all request from 443.

My current configuration is

User ==> Cloudflare ==> NGINX

how to configure varnish in following way

User ==> Cloudflare ==> NGINX ==>Varnish

2 Answers
kamaln7 MOD February 20, 2018
Accepted Answer

Hi,

Since Varnish does not support SSL termination, you will need to configure nginx to do so, forward requests to Varnish, which would then forward them back to nginx over HTTP. Follow this tutorial with a few adjustments:

  1. Use the following instructions to install Varnish: https://packagecloud.io/varnishcache/varnish41/install#manual-deb replacing trusty with xenial (16.04)

  2. Configure the backend like so:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}
  1. Replace DAEMON_OPTS="-a :6081 \ with `DAEMON_OPTS="-a 127.0.0.1:6081 \.

Once you've done that, you will have Varnish configured to listen locally on port 6081 and proxy requests to 127.0.0.1:8080. Now, you'll need to (1) move your existing nginx configuration to 127.0.0.1:8080, and (2) configure a new virtual host that handles SSL termination.

(2) is also part of the tutorial that I linked. Make sure you set proxy_pass to http://127.0.0.1:6081;. Finally, go into your existing virtual host, remove the SSL bits, and configure it to listen on 127.0.0.1:8080 like so:

server {
    listen 127.0.0.1:80;
    server_name your_domain_name;
    ...
}

Restart nginx and you should be able to browse to https://yourdomain with everything working properly.

Varnish Cache is a caching HTTP reverse proxy, or HTTP accelerator, which reduces the time it takes to serve content to a user. In this tutorial, we will cover how to use Varnish Cache 4.0 to improve the performance of your existing web server. We will also show you a way to add HTTPS support to Varnish, with Nginx performing the SSL termination.

Thank you, I already figure it out few days ago !! Anyway thanks.

Is there any minimum requirement for running varnish?

I have deoplet of $5 i.e. 1 vcpu, 1GB RAM and 25 GB storage.

I run already 3 wordpress site here.

I don't want any caching plugin to use. Is varnish will be good?

What is your opinion about server side caching vs cache plugin.

Please explain

  • Ah, that's great! I don't have a lot of experience running multiple production Wordpress websites on the same Droplet myself, so unfortunately I won't be able to say if that will be enough. Try it out, do some load testing, and see what is best for your specific setup :)

    If I am not mistaken, one of the most popular caching plugins, W3 Total Cache, works just fine alongside Varnish. So if you really want to try that out, you can install it and see if it helps.

    Another option if you do end up installing W3 Total Cache is Redis. Redis is a fast memory-based storage system and W3 Total Cache can use it to cache parts of your website so that they are served even quicker. If you are interested, here's a tutorial on installing Redis.

    You can always take a snapshot of your Droplet so you have a backup in case things go wrong!

    Redis is an in-memory, key-value store known for its flexibility, performance, and wide language support. In this guide, we will demonstrate how to install and configure Redis on an Ubuntu 16.04 server. To complete this guide, you will need access to an...
Have another answer? Share your knowledge.