How To Install and Configure Varnish with Apache on Ubuntu 16.04
Varnish is an HTTP accelerator and it’s a tool for speeding up a server. It used as a reverse proxy for your server, as Varnish as front and Apache as back.
Note : You should have sudo access to the server in order to install Varnish.
First step, executes the below commands this will take care installation of varnish.
After installing the Varnish basically you will get two configuration files 1) /etc/default/varnish 2) /etc/varnish/default.vcl
You can check both files mentioned above to see what in there. Search for DAEMON_OPTS, in file /etc/default/varnish, you see the below results. Here come the varnish listening port as 6081 with value of parameter -a. Do not edit any files now, just keep on reading.
Here’s the breakdown of the options we’ve used above:
-a – this is the address and the port Varnish listens to mainly. In the configuration above, we didn’t specify the address. This means that Varnish will listen for requests on the same server where the web server is installed. Usually, people will have to install Varnish on a different server, so that it’s not subjected to the load as the same web server. It’s a best practice to reduce the server load.
-T – the address and the port the management interface listens to.
-f – allows you to specify your own VCL configuration file instead of the default.
S – the path to the file containing the secret used for authorizing access to the management port.
-s – used for specifying the storage backend. You can use any of the options on this page. Usually its malloc, which uses memory as the cache. This asks for the maximum memory size to be specified. In this case, its 256m which is 256MB.
Configuring the Varnish.
What we have to do is let Varnish sit on the front and apache on the back. It means all the HTTP request will hit to the Varnish cache, if cache match exists it will supply that relevant page, otherwise the request will move to the backup server, that is apache.
By default, Varnish listens on port 6081, which is not useful for production sites. So what we want is Varnish should listen port 80(default HTTP port) and apache listens to other ports such as 8080.
To achieve this first change /etc/apache2/ports.conf ,change this Listen 80 to Listen 8080. So apache won’t be your first door.
Now, you have to update your VirtualHost file accordingly. Edit /etc/apache2/sites-available/000-default.conf (or any other VirtualHost config file you use), replace the line <VirtualHost :80> with <VirtualHost :8080>.
Varnish to listen port 80, so we would change DAEMON_OPTS=”-a :6081 to DAEMON_OPTS=”-a :80 in /etc/default/varnish as I said before.
I was stuck here. Varnish wasn’t taking that config file.
It’s a tricky part, I followed the official tutorial of Varnish and I didn’t have any luck with that. But from searching a lot in google I got this article, such a life saver http://deshack.net/how-to-varnish-listen-port-80-systemd/ . Great thanks to Mattia Migliorini!.
The easy solution
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 restart the varnish
# systemctl daemon-reload # systemctl reload varnish.serviceThen restart your apache service.
sudo service apache2 restart
Create one php file test.php, displaying some records from a database. Try with http://yourdomain.com/test.php. Test your headers you can see Varnish headers there.