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 overall performance of your web server by caching contents. The Varnish cache stores the copy of user request’s and serves the same page when the user revisits the webpage. It makes your website really fast and accelerate your web site performance up-to 300 – 1000x (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 Varnish Cache 4 on Ubuntu 14.04.
Step 1. First of all make sure that all packages are up to date.
Step 2. Install Apache web server.
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:
Step 3. Install Varnish using apt-get command.
Varnish can be installed by adding the Varnish repository to your sources and using apt-get to install it:
sudocurlhttps://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
Step 4. Configuring Varnish.
Varnish is automatically configured to server 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
If you have any virtual hosts configured, you will need to update these as well – ensure your configuration looks like this:
Then we need to configure Varnish to listen on port 80. Edit the file
/etc/default/varnish Find the section begin 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 \
Once you save and exit out of that file, open up the default.vcl file:
### sudo nano /etc/varnish/default.vcl
Restart the Apache and Varnish service for the changes to take effect:
You can check to see if varnish is working by typing the following command:
Step 5. Testing Varnish.
The test consists in making a HTTP request via curl and verifying that it is handled by Varnish:
[firstname.lastname@example.org~]# curl -I 192.168.146.161
Congratulation’s! You have successfully installed varnish cache. Thanks for using this tutorial for installing Varnish 4 on Ubuntu 14.04 system.