Advance

Install Varnish Cache For Apache On Ubuntu 20.04

Install Varnish Cache For Apache On Ubuntu 20.04

Varnish Cache is a popular and powerful open-source HTTP engine/reverse HTTP proxy that is suitable for high traffic websites. The Varnish Cache, known as Varnish is an HTTP accelerator designed for content-heavy dynamic websites as well as APIs. Varnish is designed as an HTTP accelerator. It is not similar to Squid which began life as a client-side cache and even is in contrast with Apache and Nginx, which are primarily origin servers. If you are looking for a solution to speed up your Apache server, join us with this article to learn How to Install Varnish Cache For Apache on Ubuntu 20.04. Find your considered package on Eldernode and purchase your own Ubuntu VPS and enjoy our technical team support.

Tutorial Install Varnish Cache For Apache On Ubuntu 20.04

Previously, you have learned about Apache installation on Ubuntu 20.04. So, we assume that you have Apache installed and running. In the following, we will run Apache on the non-standard port 8080 and then run Varnish, as a reverse proxy, on port 80. Also, we will be using Apache as the backend server, and it will not run under the standard HTTP port 80. Stay with us to learn How to Install and Configure Varnish on Ubuntu 20.04.

Recommended Article: Tutorial Configure MongoDB Remote Access on Ubuntu 20.04

Prerequisites to Install Varnish Cache on Ubuntu 20.04

To let this tutorial work better, please consider the below Prerequisites:

_ Ubuntu 20.04 LTS

_ A non-root user with Sudo privileges.

_ To set up, follow our Initial server setup on Ubuntu 20.04.

How to Install Apache Webserver on Ubuntu 20.04

If you value speed, performance, scalability, and want to save on server infrastructure you will like Varnish and find it necessary for your business. When you install Varnish in front of any server that speaks HTTP and configures it to cache the contents, Varnish speeds a website by caching content that is frequently accessed by users and storing it in the memory, thereby guaranteeing faster retrieval of webpages. Let’s go through the steps of this guide to install Varnish cache for Apache on Ubuntu 20.04 and boost your website speed.

Step 1:

First, you need to install the Apache webserver. Since it is available in the Ubuntu repository by default. So, use the following commands to update the Ubuntu repository and then install Apache:

apt update
apt install -y apache2

Step 2:

Once the installation is completed, you are ready to start the Apache service and enable it to run automatically at system boot time. So, run:

systemctl start apache2
systemctl enable apache2

Step 3:

In this step, you must allow everyone to access the webserver,  HTTP, and HTTPS on our firewall rule. Open new SSH, HTTP, and HTTPS ports using the following UFW commands:

ufw allow ssh
ufw allow http
ufw allow https

Then, use the command below to start the firewall service and enable it to launch automatically at system boot. Also, type y and press Enter to confirm:

ufw enable

In this way, new ports for HTTP, HTTPS, and SSH have been opened, and are accessible from outside of the network.

How to change Apache default Port on Ubuntu 20.04

The Apache web server will be running on port 8080 as backend, and the standard HTTP port 80 will use by ‘Varnish’. So, in this section, you need to change the default apache port. To do this, you should edit the apache configuration ‘ports.conf‘ and all of the virtual host configurations under the ”sites-available” directory.

First of all, go to the Apache configuration directory and run:

cd /etc/apache2

Note: You also need to replace port ’80’ with ‘8080’ in the Apache configuration ‘ports.conf‘ and all virtual host files under the ”sites-available” directory.

To do this, use the command below:

sed -i -e 's/80/8080/g' ports.conf
sed -i -e 's/80/8080/g' sites-available/*

Now, you can test the Apache configuration. If you faced no error, you can restart the Apache service.

apachectl configtest
systemctl restart apache2

Since Apache is running under HTTP port ‘8080’, you can use the following command to check it:

netstat -plntu

How To Install Varnish On Ubuntu 20.04

In this section, you can install Varnish from the Ubuntu repository using the following command:

apt install -y varnish

Once the installation is finished, run the following command to start Varnish and enable it to launch automatically at system boot:

systemctl start varnish
systemctl enable varnish

Now, you should have Varnish installed on your Ubuntu 20.04 and it is running under ports ‘6081’ for public address and ‘6082’ for localhost address by default. To check it, run:

netstat -plntu

How to Configure Varnish HTTP Cache to listen on port 80

Since Varnish will be forwarding HTTP requests to the Apache webserver, in this step, you will configure the Varnish Accelerator to listen to port 80 and then configure Apache to listen to port 8080.

Step 1: Backend Configuration

Go to the ‘varnish’ configuration directory and backup the default file ‘default.vcl‘:

cd /etc/varnish/
cp default.vcl default.vcl.aseli

Here, we use the vim editor to edit the varnish configuration ‘default.vcl’:

vim default.vcl

Define the ‘backend’ configuration on line 16. The backend for our setup is Apache, and it’s’ running on port 8080 and save and close it.

Step 2: Running Varnish on Port 80

Now, you must change the ‘6081’ and ‘6082’ ports that are the default ports. Also, you need to change the port to HTTP port 80 for the public address.

Again, edit the varnish parameter configuration in ”/etc/default’ directory”.

cd /etc/default/
vim varnish

Then, Edit the ‘DAEMON_OPTS‘ line, change the default port ‘6081’ for public address with standard HTTP port ’80’, and save and close it.

Now, it is time to edit the varnish service file ‘varnish.service‘. To do this, go to the ‘/lib/systemd/system‘ directory and edit the service file using vim:

cd /lib/systemd/system/
vim varnish.service

Next, on the ‘ExecStart‘ line, change the varnish start command as 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

When you are finished, save and close it. To reload the systemd service configuration and then restart varnish, type:

systemctl daemon-reload
systemctl restart varnish

Finally, you can check varnish and make sure it’s running on the HTTP port 80. So, run:

netstat -plntu

If all is done correctly, Varnish should be running under HTTP port 80 as front-end for Apache web server, which is on port 8080.

Recommended Article: How To Install Nginx on Ubuntu 20.04 LTS

How to test Varnish Cache on Apache

In the last section of this guide, you can test if Varnish cache is enabled and working with the Apache HTTP server. To do this, run the following command:

curl -I http://localhost

Finally, you will get Apache content.

 

How to test Varnish Cache on Apache

 

The below command allows you to check the varnish log:

varnishncsa

Conclusion

In this article, you learned How to Install Varnish Cache for Apache on Ubuntu 20.04. Not different you are a user who needs basic functions or a very advanced user who has the expertise, time, and resources to build customized modules. However, Varnish will be useful for you. If you are interested in learning more, find our related article on Install Varnish cache for Apache on CentOS 8.

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave Your Comment

Your email address will not be published.

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+18054214518

7 days a week, 24 hours a day