Installing Varnish on Ubuntu 18.04

Reading Time: 3 mins

Overview

The Varnish is a web application accelerator and caching HTTP reverse proxy installed to reduce the loading time of web pages and thereby, to increase the web server’s speed and performance. Generally, Varnish will be installed in front of your web server to act as an absolute boon for content-heavy dynamic sites. It caches the responses from a server (website/application) in its memory so as to retrieve it the next time, a user hits the same request. 
Further, Magento highly recommends using Varnish as the web application accelerator. It also states that the built-in full page caching (in the case of either the file system or database) is slower when compared to Varnish (as Varnish is exclusively designed for accelerating HTTP traffic). 
To know more, visit, https://devdocs.magento.com/guides/v2.3/config-guide/varnish/config-varnish.html 

Preconditions

  • It is to be noted that the given instructions are applied to installing Varnish server on Ubuntu 18.04.
  • Presuming that you are logged in as a non-root user (in which sudo privileges is necessary).

Step 1: Updating the default packages 

It is necessary to check whether the default packages are up to date.
To update the package index on your server, use the command:

sudo apt-get update

Now, it is ready for the installation process.

Step 2: Installing Varnish

You can install the necessary packages from the official repository.
Type the following command to ensure the availability of apt_transport_https

sudo apt-get install apt-transport-https

To include your GPG key, use the command:

curl https://repo.varnish-cache.org/ubuntu/GPG-key.txt | sudo apt-key add –

It’s time to include the Varnish repository to the apt sources list.

sudo sh -c ‘echo “deb https://repo.varnish-cache.org/ubuntu/ trusty varnish-4.0” >>  /etc/apt/sources.list.d/varnish-cache.list’

Now, update and install Varnish by using the below command:

sudo apt-get update
sudo apt-get install varnish

By default, the Varnish listens to the port 6081 as:
http://varnish_VPS_public_IP:6081
When we configure Varnish to listen on port 80, it throws an error as web server also listens to the same port 80. 

 
503-backend-error-varnish-installation-ubuntu
 

To overcome this hurdle, first, you need to change the web server port to 8080 and then you can configure Varnish to listen on port 80. Let us now start configuring Varnish to listen on port 80 in the below sections.

Step 3: Configuring Varnish

To start Varnish configuration, run the below command.

cd /etc/varnish

Let’s configure the default.vcl file.

sudo nano /etc/varnish/default.vcl

The default configuration file will look like the below,

backend default {
.host = “127.0.0.1”;
.port = “8080”;
}

Now, change the Host according to your IP address.

backend default {
.host = “LAMP_VPS_private_IP”;
.port = “8080”;
}

To enable the changes into effect, restart Varnish by the command:

sudo service varnish restart

Let’s edit the Varnish configuration file located in the Varnish configuration directory.

sudo nano /etc/default/varnish

Identify the below line,

DAEMON_OPTS=”-a :6081 \

To change the default port to 80, use the command:

DAEMON_OPTS=”-a :80 \

To enable the changes into effect, restart Varnish by the command:

sudo service varnish restart

Let’s edit the varnish.service file.

sudo nano /lib/systemd/system/varnish.service
-a :6081

Which is configured to the port 80 as:

-a :80

To reflect the changes, you need to give systemctl daemon-reload as it is a system file.
Note: After configuring Varnish, you need to enable the Varnish cache in the Magento back end and upload the generated varnish.vcl file in the web-root directory. Post which, you need to remove the default.vcl file located at etc/varnish/default.vcl file and then create a symlink like:

sudo ln -s /var/www/sample.com/public_html/varnish.vcl /etc/varnish/default.vcl

You can also enable the changes (using varnish restart command) at the end of the configuration process too. Here, we have installed and configured Varnish for general purpose, to know about Varnish configuration with Magento, visit, Enhancing Magento’s Performance with Varnish Cache

Step 4: Testing Varnish

To verify the running status of the Varnish on your server, use the command:

sudo service varnish status

To start/stop the Varnish service, use the command:

sudo service varnish start
sudo service varnish stop
 

The Wind-Up

Now that your website is installed with Varnish, you can achieve the desired speed and performance. It is to be remembered that the maximum benefit of the Varnish can be achieved only by proper tuning and additional tweaks at the times of need such as high-traffic days. If you go with Magento, which encompasses large chunks of data, a website accelerator like Varnish is significant to overcome these trivial such as slow page loading and poor website performance.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top