Varnish is an opensource HTTP accelerator. It is usually configured to sit in front of webservers to quickly serve HTTP/HTTPS requests. Varnish can also be used as load balancer to distribute loads across multiple webservers.
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 setup 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 webservers, it can greatly enhance the server performance. This is because Varnish stores web caches in system’s memory ensuring faster retrieval in subsequent requests for the same resource.
To get this working, follow the steps below:
Step 1: Install Apache2
First run the commands below to install Apache2 webserver.
sudo apt-get update sudo 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.
sudo systemctl stop apache2.service sudo systemctl start apache2.service sudo systemctl enable apache2.service
By default apache2 HTTP service automatically is bond to port 80 and 443 for HTTPS. This
Step 2: 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
Step 3: Switch Apache2 default post 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 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.
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 Apache2 default virtualhost config file.
sudo nano /etc/apache2/sites-available/000-default.conf
Then make the highlighted change below.
Save then file and exit.
Then restart Apache2
sudo systemctl restart apache2.service
Now to access Apache2, you’ll have to enter the server IP or hostname followed by port # 8080.
ex. http://localhost:8080
Step 4: 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 location 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 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
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";
}
Save the file and close 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 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
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:
Install WordPress with Nginx and Memcached Support on Ubuntu 17.04 / 17.10
Related posts:
- Students Tutorial – Allow WordPress Permalinks on Nginx or Apache2
- Redirect HTTP to HTTPS using Nginx or Apache2 on Ubuntu 17.04
- Apache2 301 Redirect Without htaccess File on Ubuntu 17.04
- Setup Apache2 Virtual Hosting on Ubuntu 17.04 / 17.10
- Install WordPress on Ubuntu 17.04 / 17.10 with Apache2 HTTP/2 and Let’s Encrypt SSL
- Running Multiple WordPress Sites on Ubuntu 16.04 LTS with Apache2, MariaDB and PHP 7.1 Support
- Install Ghost CMS on Ubuntu 16.04 / 17.10 / 18.04 with MariaDB and Apache2 Proxy
- Install the Latest Apache2 HTTP Server ( 2.4.34 ) on Ubuntu 16.04 / 17.10 / 18.04 LTS Servers
- Apache2 HTTP Server ( 2.4.35 ) Released — Here’s How to Install / Upgrade on Ubuntu 16.04 / 18.04 LTS