This brief tutorial is going to show students and new users how to install and configure Varnish with Apache2 on Ubuntu 17.04 | 17.10. In this post, we’ll set up Varnish to be the doorway or front-end to Apache2 to quickly serve HTTP requests. When you configure Varnish to be the font-end to Apache2 or other web servers, it can greatly enhance the server’s performance. This is because Varnish stores web caches in the system’s memory ensuring faster retrieval in subsequent requests for the same resource. To get this working, follow the steps below:
Install Apache2
First, run the commands below to install the Apache webserver. After installing Apache2, the commands below can be used to stop, start and enable Apache2 to always startup every time the server boots up. By default apache2 HTTP service automatically is bound to ports 80 and 443 for HTTPS. This
Install Varnish
Now that Apache2 is installed, run the commands below to install Varnish After installing Varnish, the commands below can be used to start, stop and enable Varnish to always start up when the server boots
Switch the Apache default port to 8080
Since we want Varnish to listen for all traffic coming to port 80 which is Apache2 default port, let’s configure Apache2 to use another port number. You can open the Apache default port configuration file at /etc/apache2/ports.conf and change the Listen value to 8080. To quickly change the port run the commands below to open the Apache default port configuration file. Then make sure the file has these lines. Save when done. Next, open the Apache default virtual host config file. Then make the highlighted change below. Save the file and exit. Then restart Apache2 Now to access Apache2, you’ll have to enter the server IP or hostname followed by port # 8080. ex. http://localhost:8080
Configure Varnish to use Port 80
Now that port 80 is free, let’s configure Varnish to use that post instead. To assign port 80 to Varnish, run the commands below. Varnish default configure file is located at /etc/default/varnish Open it by running the commands below: Then look for the config block under Alternative 2 and make the highlighted changes as shown below. Save the file when you’re done. Next, run the commands below to open the default.vcl file Then verify the line shown below is what you see. Save the file and closeout. After that, restart both Apache2 and Varnish Next, run the commands below to start Varnish if it won’t start. If everything is set up correctly, Varnish should be the default listener of port 80. To test, run the commands below. The results should be something like the one below Congratulations! You’ve just installed Apache2 with Varnish support. You may also like the post below: