Setup Varnish and Apache 2 on Ubuntu 17.04 / 17.10

Step 1: Install Apache2

First run the commands below to install Apache2 webserver.

 apt-get update
 apt-get install apache2

After installing Apache2, the commands below can be used to stop, start and enable Apache2 to always startup everytime the server boots up.

 systemctl stop apache2.service
 systemctl start apache2.service
 systemctl enable apache2.service

By default apache2 HTTP service automatically is bound to port 80 and 443 for HTTPS.

Step 2: Install Varnish

Run the commands below to install Varnish

 apt-get 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

 systemctl stop varnish.service
 systemctl start varnish.service
 systemctl enable varnish.service

Step 3: Switch Apache 2 default post To 8080

Open Apache2 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 Apache2 default port configuration file.

 nano /etc/apache2/ports.conf

Then make sure the file has these lines. Save when done.

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf


Next, open the Apache2 default virtualhost config file.

 nano /etc/apache2/sites-available/000-default.conf

Then make the change below.


Save then file and exit.

Then restart Apache2

 systemctl restart apache2.service

Now to access Apache2, you’ll have to enter the server IP or hostname followed by port # 8080.

ex. http://VPS_IP_ADDRESS:8080

Step 4: Configure Varnish to use port 80

To assign port 80 to Varnish, run the commands below.

Varnish default configure file is location at /etc/default/varnish

Open it by running the commands below:

 nano /etc/default/varnish

Then look for the config block under Alternative 2 and make the highlighted changes as shown below.

## Alternative 2, Configuration with VCL
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"

Save the file when you’re done.

Next, run the commands below to open the default.vcl file

 nano /etc/varnish/default.vcl

Then verify the line shown below is what you see.

# Default backend definition. Set this to point to your content server.
backend default {
.host = "";
.port = "8080";

Save the file and close out.

Next, run the commands below to start Varnish if it won’t start.

 /usr/sbin/varnishd -a :80 -b localhost:8080

After that, restart both Apache2 and Varnish

 systemctl restart apache2.service
 systemctl restart varnish.service

If everything is setup correctly, Varnish should be the default listener of port 80. To test, run the commands below.

curl -I http://localhost

The results should be something like the one below

[email protected]:~# curl -I http://localhost 
HTTP/1.1 2OO OK 
Date: Tue, 07 Nov 2017 17:04:09 GMT 
Server: Apache/2.4.Z7 (Ubuntu) 
Last-Modified: Tue, 07 Nov 2017 16:41:16 GMT 
Vary: Accept-Encoding 
Content-Type: text/html 
X-Varnish: 32770 4 
Age: 63 
Via: 1.1 varnish (Varnish/5.0) 
ETag: W/"2aa6-55d673d5f2da7-gzip" 
Accept-Ranges: bytes 
Connection: keep-alive 
[email protected]:~#

Last modified: November 07 2017 19:27:04