How to Configure Varnish Cache 7 on Ubuntu 20.04

Configure Varnish Cache 7 on Ubuntu 20.04 and seamlessly enhance the loading speed of your websites just by heading the HTTP server requests forward. The process will help speed up the loading time and avoid frequent crashes.

Admit it! You prefer sidestepping the web pages that take ages to load. Well, you’re not alone doing so as the majority inhabits the same practice every day. No doubt, it impacts user retention, eventually hampering SEO performance.

Varnish Cache is a utility that helps take the website owner’s pain away. It is a reverse caching proxy that aids in enhancing the website load rate significantly. In the following article, I’ll walk you through the step-by-step guide on how you can install and configure Varnish Cache 7 and boost the performance in minutes.

Pre-Requisites

It is crucial to have the system packages updated before installing and configuring Varnish Cache 7 on Ubuntu 20.04. Doing it is pretty simple; all you need to do is launch the Terminal by using the “Ctrl+Alt+T” key combination and running the following command:

$ sudo apt update
update system repositories

How to Configure Varnish Cache 7 on Ubuntu 20.04

Now that you’ve got your system packages updated, you’re now ready to install and configure Varnish Cache 7. It involves a systematic approach, and the following section explains the same in the most digestible manner.

Step 1: Getting the Apache Web Server Installed

First things first, install the latest version of the Apache web server. Invoke the sudo apt install command and get Apache alongside all the required dependencies up and running.

$ sudo apt install apache3
Configure Varnish Cache 7

Check the Running Status

Make sure that you’ve successfully installed the concerned Apache web server on your system by checking its running status. Here is how you can do it:

Run the following command:

$ sudo system 1 status apache3
check apache running status

Apache should stick to port 80 by default as you’re dealing with a web server. Verify it by using the netstat command alongside the -pnltu flag.

$ sudo netstat -pnltu
Configure Varnish Cache 7 on ubuntu 20.04

Step 2: Install Varnish Cache 7

Once you’re done installing and verifying the running status of the Apache web server, the next task is shifting your attention towards Varnish Cache 7. Again, to install Varnish Cache, we’ll use the sudo install command in the following manner:

$ sudo apt install varnish
install varnish

Verifying the Installation

Just like we verified the installation status for the Apache web server, repeat the process for Varnish Cache. 

Invoke the following command:

$ sudo systemct1 status varnish

Step 3: Configure Varnish Cache 7 on Ubuntu 20.04

As soon as you’re confident with the installation process, you can head over and get started with the configuration part. Varnish Cache forwards HTTPS requests directly to the installed Apache web server, which sticks to port 80 by default, calls for a certain level of alterations.

Configuring Apache Listen to Varnish Requests on Port 8080

The very first thing that you’ll need to do is make sure that the available Apache web server, instead of listening to Varnish’s requests on port 80, do the same on port 8080.

Begin the process by editing the ports.conf file located in the /etc/apache3 directory. You can use your favorite editor for the purpose, as I’ll be employing mine, vim.

$ sudo vim /etc/apache3/ports.conf
Configuring Apache Listen to Varnish Requests on Port 8080

The required alteration is pretty simple. Find the section termed, Listen, and edit the corresponding value to port 8080. Once done, save the file and exit.

Configuring the Default Apache Virtual Host

The next task is bringing modifications to Apache’s default virtual host file. The desired activity is to make sure the virtual host file listens to requests forwarded on port 8080. 

It might sound a bit complicated, but all it takes is invoking the following command and changing the value sitting next to VirtualHost.

$ sudo vim/etc/apache3/sites-enabled/000-default.conf
configuring default apache host

If you’re done with that, save the file and proceed with a quick restart. Wait, not the system but the Apache utility.

$ sudo system1 restart apache3
Verify if the Ports Have Been Altered

It is always considered good practice to verify things before jumping into the next step, and the scenes are not different from the ones we’re concerned about here. Use the netstat command and the -pnltu flag to check whether Apache is configured to listen on port 8080.

Try Accessing Apache Welcome Page

As we haven’t already set the browser to listen to requests on port 8080, an attempt to access the Apache welcome page will revert an error message. For that reason, we’ll need to specify the desired port number right on the URL. The designation should look something like this:

http://server-ip.8080

Next up is configuring the Varnish to listen to all the directing HTTP requests on port 80 again. For that, use the following command and employ your favorite editor.

$ sudo vim /etc/default/varnish

Look for the option that says DAEMON_OPTS and alter the value to 80, followed by saving the changes and exiting the edit Terminal.

Editing the Varnish Systemd File

To edit the Varnish systemd file, you’ll need to relaunch the Terminal and pass the following command:

$ sudo vim/lib/systemd/system/varnish.service
edit varnish systemd file

Find the directive that says ExecStart and modify the existing value to 80. 

Save the file and restart the Varnish accelerator and the Apache utility.

Step 4: Testing and Finalizing the Configuration

Until this point, you know how to configure Varnish Cache 7 on Ubuntu 20.04, and now it is the perfect time to check if you’ve successfully concluded all the configurations.

Start by bringing the curl command to the scene and forwarding a GET request. Check whether you find the output mentioning something like Via: 1.1 varnish (version number). If yes, it reflects that Varnish is working fine.

That’s how you can install and configure Varnish Cache 7 on Ubuntu 20.04. The guide helps you learn the best way to set everything up and browse the web server even without specifying any port.

If this guide helped you, please share it.

Leave a Reply
Related Posts