According to statistics, even a small delay in the page loading speed of your website can significantly reduce a user’s session and lead to high bounce rates.
If you want to boost your website speed, you can install Varnish cache, an open source HTTP cache accelerator that ameliorates your site’s speed with a factor of 300 – 1000x depending on your architecture. Varnish cache is a web application accelerator also known as a caching HTTP reverse proxy.
How it works? You install it in front of any server that speaks HTTP and configure it to cache the contents. It speeds a website by caching content that is frequently accessed by users and storing it in the memory, thereby guaranteeing faster retrieval of webpages.
We will show you how to install Varnish cache on Ubuntu 18.04 and therefore to improve your Google ranking and get a higher SEO score.
– Update the system
Begin by updating the packages in the system. Access your server as root and run the following command:
# apt update && apt upgrade
– Install Apache webserver
Since Varnish cache is located in front of a webserver, you’ll need to install Apache webserver for demonstration.
Run the following command to install Apache:
# apt install apache2
When Apache web server installation is complete, start the webserver and test its status using the following commands:
# systemctl start apache2 # systemctl status apache2
The above commands should give a relevant output confirming that the Apache webserver is up and running.
– Install Varnish HTTP accelerator
Once Apache webserver was installed, install Varnish HTTP accelerator by running the following command:
# apt install varnish
# systemctl start varnish # systemctl status varnish
– Configure Apache and Varnish HTTP cache
As Apache webserver listens on HTTP port 80 for incoming connections, we need to make some adjustments. Since Varnish will be forwarding HTTP requests to the Apache webserver, we will configure the Varnish Accelerator to listen to port 80 and then configure Apache to listen to port 8080.
Open the configuration file using the following command:
# vim /etc/apache2/ports.conf
Save and exit the configuration file.
To configure Apache to listen to port 8080, we are going to make changes to the default Apache virtual host file:
# vim /etc/apache2/sites-emabled/000-default.conf
Save and exit the configuration file.
Restart the Apache webserver for the changes to come into effect. Use the following command:
# systemctl restart apache2
Now the webserver can only be accessed via port 8080.
– Set up Varnish to listen to port 80
In order to forward HTTP requests to the webserver, we also need to configure Varnish to listen to port 80. This will also eliminate the need to appending 8080 at the end of the URL when accessing the web browser.
Launch your preferred text editor and open the /etc/default/varnish file.
# vim /etc/default/varnish
Scroll and locate the attribute ‘DAEMON_OPTS’. Be sure to change the port from 6081 to port 80
Save and close the text editor.
By checking the /etc/varnish/default.vcl file you should get an output with the port number.
You need to edit the /lib/systemd/system/varnish.service and modify the port in ExecStart directive from port 6081 to 80.
Then Save and exit the text editor.
For the changes to come to effect, you need to restart Apache web servers, reload the system and restart Varnish, in the following order:
# systemctl restart apache2 # systemctl daemon-reload # systemctl restart varnish
– Test the configuration
Use the curl command to check if your configuration is all good:
# curl -I server_IP
In the given output look for this line Via: 1.1 varnish (Varnish/5.2) which shows that Varnish works as expected.
Now that you have installed super-fast Varnish HTTP accelerator, your webserver should work faster than ever before by caching frequently accessed webpages and serving them at incredible speeds!