Varnish Cache is a free and open-source HTTP proxy. It is an HTTP accelerator designed for high-traffic dynamic websites. It places between client and Apache web server to serve web traffic very fast. It can also be used as a load balancer if you are using multiple servers. It caches contents frequently accessed by users and stores them in its memory. This increases the website performance.
In this tutorial, we'll explain to you how to configure Apache with Varnish Cache on Ubuntu 20.04.
Prerequisites
- A cloud server running with Ubuntu 20.04.
- A non-root user with Sudo privileges.
Install Apache Web Server
By default, the Apache package is included in the Ubuntu default repository. You can install it using the following command:
# apt-get install apache2 -y
Once the Apache is installed, start the Apache service and enable it to start at system reboot:
# systemctl start apache2
# systemctl enable apache2
Now, open your web browser and access the Apache test page using the URL http://your-server-ip. You should see the Apache test page in the following screen:
Change Apache Default Port
By default, Varnish uses the HTTP port 80 and we'll use Varnish as a front-end server. So you'll need to configure Apache to listen on port 8080.
In order to do so, first, edit the Apache ports.conf file and change the default port from 80 to 8080:
# nano /etc/apache2/ports.conf
Change the following line:
Listen 8080
Save and close the file then edit the Apache default virtual host configuration file:
# nano /etc/apache2/sites-available/000-default.conf
Change the following line:
<VirtualHost *:8080>
Save and close the file then restart the Apache service to apply the changes:
# systemctl restart apache2
Now, verify the Apache listening port using the following command:
# ss -antpl | grep 8080
You should get the following output:
LISTEN 0 511 *:8080 *:* users:(("apache2",pid=2194,fd=4),("apache2",pid=2193,fd=4),("apache2",pid=2192,fd=4))
Install Varnish
You can now install the Varnish package using the following command:
# apt-get install varnish -y
Once the Varnish is installed, start the Varnish service and enable it to start at system reboot:
# systemctl start varnish
# systemctl enable varnish
By default, Varnish listens on ports 6081 and 6082. You can check it with the following command:
# ss -antpl | grep varnish
This is the output that you should get:
LISTEN 0 1024 0.0.0.0:6081 0.0.0.0:* users:(("cache-main",pid=3003,fd=3),("varnishd",pid=2974,fd=3))
LISTEN 0 10 127.0.0.1:6082 0.0.0.0:* users:(("varnishd",pid=2974,fd=8))
LISTEN 0 1024 [::]:6081 [::]:* users:(("cache-main",pid=3003,fd=4),("varnishd",pid=2974,fd=4))
LISTEN 0 10 [::1]:6082 [::]:* users:(("varnishd",pid=2974,fd=7))
Configure Varnish as a Reverse Proxy for Apache
In this section, you'll configure Varnish to listen on port 80, and every request from the client being sent to the Apache server listening on port 8080.
First, edit the Varnish default.vcl configuration file:
# nano /etc/varnish/default.vcl
Change the following lines:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Save and close the file.
Then, edit the /etc/default/varnish file and change the default port from 6081 to 80:
# nano /etc/default/varnish
Change the port from 6081 to 80:
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
Save and close the file.
Next, you will need to edit the Varnish service file /lib/systemd/system/varnish.service:
# nano /lib/systemd/system/varnish.service
Change the port from 6081 to 80 as shown below:
ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
Save and close the file then reload the systemd daemon to apply the changes:
# systemctl daemon-reload
Finally, restart the Varnish service to apply the changes:
# systemctl restart varnish
Now, check the Varnish listening port using the following command:
# ss -antpl | grep 80
You should get the following output:
LISTEN 0 1024 0.0.0.0:80 0.0.0.0:* users:(("cache-main",pid=8522,fd=3),("varnishd",pid=8494,fd=3))
LISTEN 0 1024 [::]:80 [::]:* users:(("cache-main",pid=8522,fd=4),("varnishd",pid=8494,fd=4))
Verify Varnish Cache
At this point, Varnish is installed and configured as a reverse proxy for Apache.
You can verify this by using the following command:
# curl -I http://your-server-ip
If everything is ok, you should see the Varnish in the HTTP header:
HTTP/1.1 200 OK
Date: Tue, 27 Jul 2021 15:57:01 GMT
Server: Apache/2.4.41 (Ubuntu)
Last-Modified: Tue, 27 Jul 2021 15:50:01 GMT
Vary: Accept-Encoding
Content-Type: text/html
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.2)
ETag: W/"2aa6-5c81cd2a0a40c-gzip"
Accept-Ranges: bytes
Content-Length: 10918
Connection: keep-alive
You can also check the Varnish log for more information:
# varnishncsa
This is the output that you should get:
106.213.219.132 - - [27/Jul/2021:15:57:31 +0000] "GET http://69.87.221.34/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
106.213.219.132 - - [27/Jul/2021:15:57:41 +0000] "GET http://69.87.221.34/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
Conclusion
You've successfully configured Varnish as a reverse proxy for Apache on Ubuntu 20.04!
You can now implement this setup in the production environment to speed up your website.