One of the most used tools for sites with huge visitors is Varnish, it is an open source reverse HTTP proxy, an HTTP accelerator, and a useful tool for speeding up an Apache server. Varnish is known as front-end web caching software that you put in front of an Apache web server to speed it up.
Varnish can increase dramatically the performance of your website and prevent the Apache server from overloading in case of high server traffic.
What are the Varnish Features
- Reduces CPU load on the Apache web server.
- Web browser will load your website faster since the cache is stored in RAM.
- Supports load balancing.
In this post we will explain how to install and use Varnish as a front-end to an Apache web server. maybe later we can add how to user varnish with Nginx
My System
- A server running CentOS 8 with Apache installed
- A static IP address for your server 192.168.2.70
- HTTPD web server installed
How to Install Varnish in Centos 8
Before installing Varnish, you will have to install the EPEL repository. You can do this by running the following command.
#yum install epel-release
Once the installation ofEPEL repositories finished you will be able to install Varnish. Like this
sudo yum install -y varnish
After installation, you will need to start Varnish and enable it to start on boot.
To do this, run:
systemctl start varnish systemctl enable varnish
To check the status of Varnish, run the following command:
sudo systemctl status varnish
Check the version of Varnish that is running:
sudo varnishd -V
How to Configure Varnish
The Varnish configuration located in the /etc/varnish
directory in CentOS 8. To make Varnish work in front of Apache, you will need to set up some basic configurations.
By default Varnish listens on port 6081
. You will need to change port 6081
to 80
so that website requests access the Varnish cache first. You can do this by editing the varnish.service config file.
sudo vi /usr/lib/systemd/system/varnish.service
Change VARNISH_LISTEN_PORT from 6081
to 80
:
Please Edit the file
Reload the systemd configuration
systemctl daemon-reload
Go to the Varnish configuration directory and edit the default configuration in ‘default.vcl’.
cd /etc/varnish nano default.vcl
backend default { .host = "127.0.0.1"; .port = "8080"; }
Configure Apache To Work With Varnish
By default Apache listens on port 80
. Now, you need to make Apache to run behind Varnish caching by changing the default Apache port to 8080
.
we have to edit the httpd.conf
config file:
sudo vi /etc/httpd/conf/httpd.conf
Search for Listen 80
and replace it with Listen 8080
:
Listen 8080
Save and close the file, then restart Apache and Varnish to reflect the changes.
systemctl restart httpd systemctl restart varnish
Test Varnish
Check the Ports :
Now you should have Varnish and Apache running together. To verify that Varnish is on and working, you can use the curl
command to view the HTTP header:
curl -I http://localhost
You should see the output something like this:
Enjoy The Speed of your Web server