How To Install and Configure Varnish with Apache on Ubuntu 16.04

(Last Updated On: 07 May 2016)

web-accelerators-varnish

About Varnish

Varnish is an HTTP accelerator and a useful tool for speeding up a server, especially during a times when there is high traffic to a site. It works by redirecting visitors to static pages whenever possible and only drawing on the virtual private server itself if there is a need for an active process.

 

How Does It Work? Link

The diagram below shows how a blog post might be served when all requests go to the Apache Web server. This example shows five browsers all requesting the same page, which uses PHP and MySQL.

no-varnish-500-opt

Every HTTP request is served by Apache — images, CSS, JavaScript and HTML files. If a file is PHP, then it is parsed by PHP. And if content is required from the database, then a database connection is made, SQL queries are run, and the page is assembled from the returned data before being served to the browser via Apache.

If we place Varnish in front of Apache, we would instead see the following:with-varnish-500-opt

If the page and assets requested are already cached, then Varnish serves them from memory — Apache, PHP and MySQL would never be touched. If a browser requests something that is not cached, then Varnish hands it over to Apache so that it can do the job detailed above. The key point is that Apache needs to do that job only once, because the result is then stored in memory, and when a second request is made, Varnish can serve it.

The tool has other benefits. In Varnish terminology, when you configure Apache as your Web server, you are configuring a “back end.” Varnish allows you to configure multiple back ends. So, you might want to run two Web servers — for example, using Apache for PHP pages while serving static assets (such as CSS files) from nginx. You can set this up in Varnish, which will pass the request through to the correct server. In this tutorial, we will look at the simplest use case.

 

1. Install Apache

Apache can be installed on your VPS with a single command from the apt-get repository.

 

2. Install Varnish

Varnish can be installed on your VPS with a single command from the apt-get repository.

 

3. Configure Varnish

Once you have both apache and varnish installed, you can start to configure them to ease the load on your server from future visitors.

Varnish will serve the content on port 80, while fetching it from apache which will run on port 8080.

In another words, Varnish running on Port 80 and Apache running on Port 8080. This means that every request made to your server will flow through Varnish.

Let’s go ahead and start setting that up by opening the /etc/default/varnish file:

Uncomment all of the lines under “DAEMON_OPTS”—under Alternative 2, and make the configuration match the following code:

Once you save and exit out of that file, open up the /etc/varnish/default.vcl file:

This file tells varnish where to look for the webserver content. Although Apache listens on port 80 by default, we will change the settings for it later. Within this file, we will tell varnish to look for the content on port 8080.

The configuration should like this:

 

4. Configure Apache

So far we have told varnish that apache ports will be running on 8080. However the default settings for apache are still on port 80. We will correct the discrepancy now. Open up the apache ports file:

Change the port number for both the NameVirtualHost and the Listen line to port 8080, and the virtual host should only be accessible from the localhost. The configuration should look like this:

Change these settings in the default virtual host file as well:

The Virtual Host should also be set to port 8080, and updated line looks like this:

Save and exit the file and proceed to restart Apache and Varnish to make the changes effective.

 

Make Varnish listen port 80 with systemd

Want to know the problem? Just have a look at the active processes related to varnish:

Note something familiar? Varnish still listen port 6081  So, what happened? Obviously, Varnish didn’t take our new configuration. But why? We followed the official tutorial…

 

The real problem

The official tutorial is a little bit outdated. Or, better, doesn’t take into account the testing version of Ubuntu, which uses systemd instead of init.d as init system. And this makes a huge difference, explained in a bug report. Basically, /etc/default/varnish is only read by the /etc/init.d/varnish script, not by the systemd init script (/lib/systemd/system/varnish.service).

Now that we know this little detail not reported in the documentation, it’s easy for us to solve the problem.

 

The easy solution

All we have to do is override the systemd init script of varnish and change something.

first backup file /lib/systemd/system/varnish.service

then

We come up with something like this:

Which is similar to what we saw before. We already now that we have to change the port passed as a value to the -a flag:

Then

reloading Varnish

if you got error like this:

just Run systemctl daemon-reload  Then type this cmd systemctl restart varnish

Check active processes related to varnish

Now Varnish listent port 80

Testing Varnish.

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

you will got like this

 

 

Congratulation’s! You have successfully installed varnish cache. Thanks for using this tutorial for installing Varnish 4 on Ubuntu 16.04 system.

 

Source:

  1. Source-1
  2. Source-2
  3. Source-3

 

…..