How to Install Varnish Cache with Apache on CentOS 8

How to Install Varnish Cache with Apache on CentOS 8

Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as APIs. In contrast to other web accelerators, such as Squid, which began life as a client-side cache, or Apache and nginx, which are primarily origin servers, Varnish was designed as an HTTP accelerator.

Varnish Cache is a powerful, open source HTTP engine/reverse HTTP proxy that can speed up a website by up to 1000 percent by doing exactly what its name implies: caching (or storing) a copy of a webpage the first time a user visits. And Varnish Cache stores content in pluggable modules called storage backends. It does this via its internal stevedore interface.

Varnish can be used for caching both the static and dynamic content on websites. In other words, it is a web application accelerator. Simply, Varnish cache is an intermediator between the client and the webserver. It serves the stored content in its memory.

Install varnish cache with apache on CentOS 8; Through this tutorial, we will learn how to set up Varnish caching with Apache on CentOS 8. And also will set up Apache to listen on port 8080 and work as a backend server, then configure Varnish to listen on default HTTP port 80.

How to Install Varnish Cache with Apache on CentOS 8

Follow the following steps to install and configure varnish cache with apache on CentOS 8:

  • Step 1 – Update System Packages
  •  Step 2 – Install Varnish Cache
  • Step 3 – Install and Configure Apache
  • Step 4 – Configure Varnish to Work with Apache
  • Step 5 – Configure Apache as a Backend Server for Varnish
  • Step 6 – Verify Varnish Cache

Step 1 – Update System Packages

First of all, open terminal or command line and execute the following command to update base system with the latest available packages:

dnf update -y

 Step 2 – Install Varnish Cache

By default, the Varnish package is available in the CentOS 8 system. Also, we can install it by just executing the following command on command line or terminal:

dnf install varnish -y

Once the Varnish installation is done, execute the following command to start the Varnish service and enable it to start after system reboot:

systemctl start varnish
systemctl enable varnish

After that, execute the following command on command on command line to verify the status of Varnish:

systemctl status varnish

By default, Varnish listens on port 6081. We can verify it using the following command:

netstat -ltnp | grep 6081

We should get the following output:

tcp        0      0 0.0.0.0:6081            0.0.0.0:*               LISTEN      1508/varnishd
tcp6       0      0 :::6081                 :::*                    LISTEN      1508/varnishd

Step 3 – Install and Configure Apache

Now, we will need to install the Apache webserver and configure it to work with the Varnish cache.

So, execute the following command on command line or terminal to install the Apache server:

dnf install httpd -y

After installing the Apache webserver, we will need to change the Apache web server listening port from 80 to 8080.

We can do it by editing the file /etc/httpd/conf/httpd.conf.

nano /etc/httpd/conf/httpd.conf

Find the following line:

Listen 80

And replace it with the following line:

Listen 8080

Save and close the file, then start the Apache service and enable it to start after system reboot:

systemctl start httpd
systemctl enable httpd

Next, copy the sample index.html page to the Apache default document root directory:

cp /usr/share/httpd/noindex/index.html.en-US /var/www/html/index.html

Note: If we are using Virtual hosting, then configure the relevant configuration file.

Step 4 – Configure Varnish to Work with Apache

By default, Varnish is configured to listen on port 6081, so we will need to configure it to listen on port 80. We can configure it by editing varnish.service configuration file:

nano /usr/lib/systemd/system/varnish.service

Find the following line:

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

And replace it with the following line:

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

Save and close the file, then reload the systemd daemon with the following command:

systemctl daemon-reload

Next, restart Varnish service to apply the changes:

systemctl restart varnish

Step 5 – Configure Apache as a Backend Server for Varnish

Next, we will need to configure Apache as a backend server for the Varnish proxy.

We can configure it by editing the file /etc/varnish/default.vcl:

nano /etc/varnish/default.vcl

Uncomment the following lines if commented:

backend default {
.host = "127.0.0.1";
.port = "8080";
}

Save and close the file when we are finished.

Note: Change the line 127.0.0.1 with our Apache server IP address if our Apache server resides on the other host. Change the port 8080 with a port number of our Apache server.

Step 6 – Verify Varnish Cache

Finally, the Varnish cache is configured to work with the Apache webserver. It’s time to test whether caching is working or not.

We can test it using the curl command:

curl -I http://your-server-ip

Conclusion

Through this tutorial, we have successfully installed and configured Varnish cache with Apache web server on CentOS 8.

Recommended CentOS Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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