How to setup Varnish and Apache on Ubuntu Linux

|

|

This article is a tutorial on installing and configuring the open-source HTTP accelerator, Varnish, with Apache2 on Ubuntu 17.04 | 17.10. The guide provides step-by-step instructions to set up Varnish as the doorway to Apache2 to serve HTTP requests quickly, which enhances server performance by storing web caches in the system’s memory for faster retrieval…

Varnish is an open-source HTTP accelerator. It is usually configured to sit in front of web servers to serve HTTP/HTTPS requests quickly. Varnish can also be a load balancer to distribute loads across multiple web servers.

This brief tutorial will 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 as the doorway or front-end to Apache2 to serve HTTP requests quickly.

Configuring Varnish as the front end to Apache2 or other web servers can significantly enhance the server’s performance. 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.

sudo apt-get update
sudo apt-get install apache2

After installing Apache2, the commands below can stop, start, and enable Apache2 always to start whenever the server boots up.

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

The apache2 HTTP service is automatically bound to ports 80 and 443 for HTTPS by default. This

Install Varnish

Now that Apache2 is installed run the commands below to install Varnish

sudo 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

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

Switch the Apache default port to 8080

Since we want Varnish to listen for all traffic coming to port 80, which is Apache2’s 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.

sudo 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

NameVirtualHost 127.0.0.1:8080
Listen 127.0.0.1:8080

Next, open the Apache default virtual host config file.

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

Then, make the highlighted change below.

<VirtualHost 127.0.0.1:8080>

Save the file and exit.

Then restart Apache2

sudo systemctl restart apache2.service

To access Apache2, 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:

sudo nano /etc/default/varnish

Then look for the config block under Alternative 2 and make the highlighted changes 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

sudo 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 = "127.0.0.1";
.port = "8080";
}

Please save the file and close it out.

After that, restart both Apache2 and Varnish.

sudo systemctl restart apache2.service
sudo systemctl restart varnish.service

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

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

If everything is set up 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

HTTP/1.1 200 OK
Date: Sun, 23 Jul 2017 17:45:49 GMT
Server: Apache/2.4.25 (Ubuntu)
Last-Modified: Sun, 23 Jul 2017 17:01:05 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 10 3
Age: 9
Via: 1.1 varnish (Varnish/5.0)
ETag: W/"2aa6-554ff0b3c88c9-gzip"
Accept-Ranges: bytes
Connection: keep-alive

Congratulations! You’ve just installed Apache2 with Varnish support.

You may also like the post below:


Discover more from Geek Rewind

Subscribe to get the latest posts to your email.

Like this:



One response to “How to setup Varnish and Apache on Ubuntu Linux”

  1. Vijayan Avatar
    Vijayan

    Nice and easy tutorial. Keep it up.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Discover more from Geek Rewind

Subscribe now to keep reading and get access to the full archive.

Continue reading