Install Varnish Cache 5.1 for Nginx on Debian and Ubuntu

Varnish Cache (also called Varnish) is an open source, HTTP accelerator which stores web pages in memory so web servers don’t have to create the same web page over and over again when requested by a client. You can configure Varnish to work in front of a web server to serve pages in a much faster way thus giving the websites a significant speed up.

In our last article, we have explained how to setup a Varnish Cache for Apache on a Debian and Ubuntu system.

In this article, we will explain how to install and configure Varnish Cache 5 as a front-end to Nginx HTTP server on a Debian and Ubuntu systems.

Requirements:

  1. A Ubuntu system installed with LEMP Stack
  2. A Debian system installed with LEMP Stack
  3. A Debian/Ubuntu system with static IP address

Step 1: Install Varnish Cache on Debian and Ubuntu

1. Unfortunately, there are no pre-compiled packages for latest version of Varnish Cache 5 (i.e 5.1.2 at the time of writing), so you need to build it from its source files as shown below.

Start by installing the dependencies for compiling it from source using the apt command like this.

$ sudo apt install python-docutils libedit-dev libpcre3-dev pkg-config automake libtool autoconf libncurses5-dev libncurses5

2. Now download Varnish and compile it from source as follows.

$ wget https://repo.varnish-cache.org/source/varnish-5.1.2.tar.gz
$ tar -zxvf varnish-5.1.2.tar.gz
$ cd varnish-5.1.2
$ sh autogen.sh
$ sh configure
$ make
$ sudo make install
$ sudo ldconfig

3. After compiling Varnish Cache from source, the main executable will be installed as /usr/local/sbin/varnishd. To confirm that the Varnish installation was successful, run the following command to see the its version.

$ /usr/local/sbin/varnishd -V
Verify Varnish Cache Version

Verify Varnish Cache Version

Step 2: Configure Nginx to Work With Varnish Cache

4. Now you need to configure Nginx to work with Varnish Cache. By default Nginx listens on port 80, you need change the default Nginx port to 8080 so it runs behind Varnish caching.

Therefore open the Nginx configuration file /etc/nginx/nginx.conf and find the line listen 80, then change it to listen 8080 as the server block as shown in the screen shot below.

$ sudo vi /etc/nginx/nginx.conf
Change Nginx Port

Change Nginx Port

5. Once port has been changed, you can restart Nginx services as follows.

$ sudo systemctl restart nginx

6. Now start Varnish daemon manually by typing following command instead of calling systemctl start varnish, since certain configurations are not in place when its installed from source:

$ sudo /usr/local/sbin/varnishd -a :80 -b localhost:8080
Start Varnish Cache Daemon

Start Varnish Cache Daemon

Step 3: Test Varnish Cache on Nginx

7. Finally, test if Varnish cache is enabled and working with the Nginx HTTP server using the cURL command below to view the HTTP header.

$ curl -I http://localhost
Test Varnish Cache on Nginx

Test Varnish Cache on Nginx

You can find additional information from the Varnish Cache Github Repository: https://github.com/varnishcache/varnish-cache

In this tutorial, we have showed how to setup Varnish Cache 5.1 for Nginx HTTP server on Debian and Ubuntu systems. You can share any thoughts or queries with us via the feedback from below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.