Advance

Tutorial Install Varnish Cache on Debian with Apache

Richard 11 Min Read
Tutorial Install Varnish Cache on Debian with Apache

Varnish Cache is open source, therefore, the software belongs to the family of free software licenses and allows use and distribution with minimal restrictions. In this article, we are going to teach you How to Install Varnish Cache on Debian with Apache. You can visit the packages available in Eldernode if you wish to purchase a Linux VPS server.

 

How to Install Varnish Cache on Debian Linux

Varnish Cache Features

Varnish takes and saves a copy of the page provided by the web server the first time a user visits that page. This will speed up the website. The next time a user requests the same page, Varnish provides a copy instead of requesting the page from the web server.
This way, your web server will manage less traffic and your website performance and scalability will be higher. Varnish cache, depending on your architecture, increases the delivery of your web content by 80% or more.

Install Varnish Cache on Debian with Apache

Step 1) Installing Apache

To install and configure Varnish Cache, we must first install Apache on our node. The first step in this process is to update the debian operating system:

apt update
apt upgrade

After downloading the package, update the operating system and install Apache 2.4:

apt -y install apache2

Once the installation is complete, enable the Apache service:

systemctl enable apache2 && systemctl start apache2

If you enable UFW, you can use the following commands. If UFW is not enabled, skip the following commands:

ufw allow 80/tcp
ufw allow 443/tcp
ufw reload

Step 2) Install and configure Varnish Cache

After completing the Apache installation, it is time to install the Varnish Cache.
The first step is to download the GPG key and then add it:

curl -L https://packagecloud.io/varnishcache/varnish5/gpgkey | sudo apt-key add -

Next, the package requirements must be installed:

apt install -y debian-archive-keyring apt-transport-https

The repository must then be added to the source list:

echo "deb https://packagecloud.io/varnishcache/varnish5/debian/ stretch main" >
/etc/apt/sources.list.d/varnishcache5.list

The package manager also needs to be updated to add a new repository:

apt update

Now that the repository is installed and the necessary components are updated, Varnish Cache will probably be installed:

apt install -y varnish

At the end of each installation, it is important to check that the installed version matches what you expected. It is necessary to check the installation of Varnish Cache and its appropriate version:

varnishd -V

After confirming the installation, configure Varnish Cache to listen to port 80 by editing the following file and replace port 80 with port 6081:

nano /lib/systemd/system/varnish.service

From:

ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

To:

ExecStart=/usr/sbin/varnishd -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

After editing the file, we must reload the system daemon to update the file:

systemctl daemon-reload

After reloading, the default configuration should be modified and tell the system where to direct the received Varnish Cache requests:

nano /etc/varnish/default.vcl backend default { .host = "127.0.0.1"; .port = "8080"; }

Apache 2.4 also needs minor changes, allowing it to listen on port 8080 instead of port 80:

nano /etc/apache2/ports.conf
Listen 8080
nano /etc/apache2/sites-enabled/000-default.conf <VirtualHost *:8080>

After the fixes, it is time to restart Apache 2.4 and Varnish Cache. After a full reboot, enable Varnish Cache:

systemctl restart apache2
systemctl restart varnish
systemctl enable varnish

At this point, we test Varnish Cache and Apache 2.4 to make sure they both work as expected:

curl -I http://127.0.0.1 HTTP/1.1 200 OK Server: Apache/2.4.25 (Debian) Vary: Accept-Encoding Content-Type: text/html X-Varnish: 2 Age: 0 Via: 1.1 varnish (Varnish/5.1) EW/"29cd-55771150d08a7-gzip" Accept-Ranges: bytes Connection: keep-alive

If necessary, you can check the Varnish Cache statistics using the following command:

varnishstat

 

Conclusion

Well, you have successfully installed and configured Apache 2.4 and Varnish Cache on your Debian Linux. This installation allows you to simplify the distribution of your web page and allows users to access information faster and more efficiently than before.

View More Posts
Richard
Eldernode Writer
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

    +8595670151

    7 days a week, 24 hours a day