LinuxTutorialsUbuntu

How To Install Varnish Cache on Ubuntu 14.04

Install Varnish Cache on Ubuntu 14.04

In this tutorial, we will show you how to install Varnish Cache on Ubuntu 14.04. For those of you who didn’t know, Varnish Cache is a powerful open-source HTTP accelerator that can be installed in front of any Webserver such as Apache or Nginx. Varnish Cache can improve the overall performance of your web server by caching content. The Varnish cache stores the copy of user requests and serves the same page when the user revisits the webpage. It makes your website really fast and accelerates your website performance up to 300 – 1000x which (means 80% or more).

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple. I will show you through the step-by-step installation of Varnish Cache 4 on Ubuntu 14.04.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 14.04 LTS.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install Varnish Cache on Ubuntu 14.04

Step 1. First of all, make sure that all packages are up to date.

sudo apt-get update
sudo apt-get upgrade

Step 2. Install Apache webserver.

For this part, we will be assuming that you have already installed Apache on your server and have it running properly. If not write this command in your terminal:

sudo apt-get install apache2

Step 3. Installing Varnish on Ubuntu 14.04.

Varnish can be installed by adding the Varnish repository to your sources and using apt-get to install it:

sudo apt-get install apt-transport-https
sudo curl https://repo.varnish-cache.org/ubuntu/GPG-key.txt | apt-key add -
echo "deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0" >> /etc/apt/sources.list.d/varnish-cache.list
sudo apt-get update
sudo apt-get install varnish

Step 4. Configuring Varnish.

The varnish is automatically configured to serve content over port 80 and fetch contents from Apache on port 8080, we need to update Apache to serve content over port 8080:

### sudo nano /etc/apache2/ports.conf
Listen 127.0.0.1:8080

If you have any virtual hosts configured, you will need to update these as well – ensure your configuration looks like this:

<VirtualHost 127.0.0.1:8080>

Then we need to configure Varnish to listen on port 80. Edit the file /etc/default/varnish Find the section beginning with “Alternative 2, Configuration with VCL” Make sure it’s uncommented and update the port:

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

Once you save and exit that file, open up the default.vcl file:

### sudo nano /etc/varnish/default.vcl
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Restart the Apache and Varnish service for the changes to take effect:

sudo service apache2 restart
sudo service varnish restart

You can check to see if varnish is working by typing the following command:

varnishstat

Step 5. Testing Varnish.

The test consists of making an HTTP request via curl and verifying that it is handled by Varnish:

[root@idroot.us ~ ]# curl -I 192.168.146.161
 HTTP/1.1 403 Forbidden
 Date: Mon, 02 Jun 2015 24:06:10 GMT
 Server: Apache/2.4.6 (Ubuntu) PHP/5.4.16
 Last-Modified: Thu, 16 Dec 2014 19:30:58 GMT
 ETag: "1321-5058a1e728280"
 Accept-Ranges: bytes
 Content-Length: 4897
 Content-Type: text/html; charset=UTF-8
 X-Varnish: 32779
 Age: 0
 Via: 1.1 varnish-v4
 Connection: keep-alive

Congratulations! You have successfully installed varnish. Thanks for using this tutorial for installing varnish cache on Ubuntu 14.04 system. For additional help or useful information, we recommend you check the official varnish website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

Save

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button