By Francis Ndungu, Alibaba Cloud Community Blog author.
Varnish cache is one of the best HTTP accelerators designed to speed up websites and Application Programming Interfaces(APIs).
Caching can greatly improve the performance of a highly accessed Apache web server by a factor of between 300 and 1000 times. Varnish achieves this by saving frequently accessed resources in memory and retrieving them at a blazing speed if identical resources are requested again.
In this guide, we will take you through the steps of setting up Varnish HTTP Cache to speed up your Apache web server on an Ubuntu 18.04 Elastic Compute Service (ECS) instance hosted on Alibaba Cloud.
To follow along with this guide, you will require the following:
The first step is installing Apache webserver. We will pull this package from the default Ubuntu package repository.
First, update the package information index.
$ sudo apt-get update
Next, install the
$ sudo apt-get install -y apache2
To verify that the web server is working like expected, enter your ECS instance public/internet IP address in a browser.
You should get the below output:
With Apache installed and running, you can move to the next step and change the web server's default listening port.
By default, Apache listens for incoming HTTP requests on port 80. You are going to change this port to 8080. To do this, open Apache ports configuration file
$ sudo nano /etc/apache2/ports.conf
Find the value of the
Listen parameter and change its value to
8080 from 80 as shown below
... Listen 8080 <IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Save the file by pressing CTRL+X, Y, and Enter.
Next, edit the default virtual host file
$ sudo nano /etc/apache2/sites-available/000-default.conf
The configurations will be similar to the one shown below. Change
<VirtualHost *:80> to
<VirtualHost *:8080> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html ... </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
Then, restart the Apache web server for the changes to take place.
$ sudo systemctl restart apache2
Once you are sure Apache is listening on port 8080, you can move over to installing Varnish HTTP Cache.
Just like we did with the Apache web server, we are going to install the varnish package with the Linux
$ sudo apt-get install -y varnish
You can confirm the status of the varnish service by running the command below
$ varnishd -V
You will see a version number displayed as shown below.
varnishd (varnish-5.2.1 revision 67e562482) Copyright (c) 2006 Verdens Gang AS Copyright (c) 2006-2015 Varnish Software AS
This means Varnish is working like expected. In the next step, you will modify the Varnish service to listen on port 80 and forward traffic to Apache on port 8080.
Varnish listens on port 6081 by default. However, you need to direct all HTTP traffic requested on port 80 to this service.
To do this, open the
/etc/default/varnish file using the nano text editor
$ sudo nano /etc/default/varnish
DAEMON_OPTS="-a :6081 \ and change it to
DAEMON_OPTS="-a :80 \. The changed part of the file should be similar to the one shown below.
... DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m" ...
Save the file by pressing CTRL+X, Y and Enter. Next, you are going to ensure that the Varnish service has the same port configuration settings. To do this, open
$ sudo nano /lib/systemd/system/varnish.service
-a :6081 to -a :80 as shown below
[Unit] Description=Varnish HTTP accelerator Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd [Service] Type=simple LimitNOFILE=131072 LimitMEMLOCK=82000 ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/sec$ ExecReload=/usr/share/varnish/varnishreload ...
Save and exit from the nano text editor by pressing CTRL+X, Y and hit Enter. Then, run the command below to reload and restart Varnish.
$ sudo systemctl daemon-reload $ sudo systemctl restart varnish
Varnish HTTP Cache is now configured to listen for incoming traffic on port 80 and ready to forward traffic to Apache web server on port 8080.
You can use Curl to examine the HTTP headers to verify that data is forwarded to Apache via Varnish Cache.
$ curl -I http://localhost:80
You should get the below output
... Server: Apache/2.4.29 (Ubuntu) Last-Modified: Sun, 06 Oct 2019 10:39:29 GMT Vary: Accept-Encoding Content-Type: text/html X-Varnish: 4 Age: 0 Via: 1.1 varnish (Varnish/5.2) ...
The above output confirms that the Varnish HTTP cache is working as expected. You can now enjoy increased load speed from your Apache webserver.
In this article, we took you through all the steps of configuring Varnish HTTP Cache on Apache web server running on Ubuntu 18.04 hosted on Alibaba Cloud Elastic Compute Service. If you are more concerned about the speed of your website, you might also consider adopting technologies such as Redis, Memcached and MySQL Cache on top of Varnish HTTP Cache.
Alibaba Clouder - July 8, 2020
francisndungu - December 10, 2019
Alibaba Clouder - December 18, 2017
Alibaba Clouder - January 3, 2018
Marketplace - April 21, 2021
Alibaba Clouder - April 12, 2019
Alibaba Cloud offers an accelerated global networking solution that makes distance learning just the same as in-class teaching.Learn More
Connect your business globally with our stable network anytime anywhere.Learn More
Alibaba Cloud (in partnership with Whale Cloud) helps telcos build an all-in-one telecommunication and digital lifestyle platform based on DingTalk.Learn More
Deploy custom Alibaba Cloud solutions for business-critical scenarios with Quick Start templates.Learn More
More Posts by francisndungu