Advance

How to install varnish cache for Apache on CentOS/RHEL 8

How to install varnish cache for Apache on CentOS/RHEL 8
1
(1)

In the following of the CentOS 8 tutorials, we are going to learn you how to install varnish cache for Apache on CentOS/RHEL 8. But first, let’s know what is the Varnish Cache. it is a free open source, modern and high-performance web application accelerator that is also a fast reverse HTTP proxy that caches content to speed up your web server performance, by storing web content in server memory – in a cache. It is configured to run in front of an origin server such as Apache (HTTPD) webserver.

You can securely reduce the response time and network bandwidth consumption on future equivalent requests as Varnish could accept the HTTP request any time a client requests for content. Then it sends the request to the origin server, caches the returned objects, and replies to the client request. The next time the client requests for the same content, Varnish will serve it from the cache.

The other useful feature of Varnish is to work as an HTTP request router, web application firewall, load balancer, and more.

Recommended Article: How to upgrade php version on Linux server and DirectAdmin

Prerequisites:

  • A server with CentOS 8.
  • A server with RHEL 8 installation with enabled Red Hat subscription on your system

How to install varnish cache for Apache on CentOS/RHEL 8

In this article, you will show you how to install Apache HTTPD web server and Varnish Cache 6 on a fresh CentOS/RHEL 8 server, including configuring Varnish to run in front of HTTPD server.

 

 

1- Installing Apache Web Server on CentOS/RHEL 8

First, update all installed software packages on the system.

dnf update

Install the Apache HTTP web server from the AppStream repository, by the following command.

dnf install httpd

Once the installation is complete, start httpd service and enable it to automatically start during system boot. Remember to check its status to confirm that it is up and running.

systemctl start httpd  systemctl enable httpd  systemctl status httpd

You need to open access to the HTTP service in the firewall to allow users access websites or applications running over HTTP, and also reload the firewalld settings to apply the new changes because CentOS/RHEL 8 includes a fully locked down firewall.

firewall-cmd --zone=public --permanent --add-service=http  firewall-cmd --reload

 

2- Installing Varnish Cache 6.4 on CentOS/RHEL 8

Install Varnish Cache on the system, since the Apache webserver is running.

dnf module install varnish  

After you completed the installation, you can verify the version of Varnish installed on your system.

varnishd -V  

Then, you must install the main executable as /usr/sbin/varnishd, and you see the Varnish configuration files are stored under the /etc/varnish directory, where:

1- /etc/varnish/default.vcl – is the main varnish configuration file written using VCL.

2- /etc/varnish/secret – is the varnish secret file.

Next, you can start the varnish service and enable it to automatically start during system boot in case of a server restart and check its status to ensure that it up and running as follows.

systemctl start varnish  systemctl enable varnish  systemctl status varnish

Buy virtual private server

 

3- Configuring Apache to Work with Varnish Cache

In this step, you must configure Varnish Cache to run in front of the Apache service. The Apache server is configured to listen on port 80 by default. As you see below, this is defined in the main configuration file /etc/httpd/conf/httpd.conf. Then, use your preferred text editor and open it to edit.

vi /etc/httpd/conf/httpd.conf  

Find the Listen parameter. Change the default port 80 to 8080 or any other port of your choice to run Varnish in front of the Apache server. (This port will be added as the backend server’s port in the Varnish configuration file later on)

Please consider that the virtual host configuration for each web site/application that will serve via Varnish should be configured to listen to the above port.

configure the above port to listen to any virtual host configuration for each web site/application that will serve via Varnish. 

<VirtualHost *:8080>      DocumentRoot "/var/www/html/eldernode.lan/"      ServerName www.eldernode.lan      # Other directives here  </VirtualHost>  

Please Note: You should comment out all the lines in the file /etc/httpd/conf.d/welcome.conf or simply delete the file, to prevent the default Apache HTTP server test page from ever being used.

rm /etc/httpd/conf.d/welcome.conf

Now you can test httpd configuration syntax for any errors to restart the httpd service to apply the new changes if all is OK.

httpd -t  systemctl restart httpd

Configuring Varnish for Systemd

Configure Varnish to listen to client requests in the default HTTP port 80 as explained below and let it deploy in front of HTTPD.

Remember that have to set the port varnish server listens on in the Varnish service file for systemd in Varnish Cache 6.0 and higher. So open it for editing.

systemctl edit --full  varnish

find the  ExecStart line, then change the value of the -a switch from:6081 to:80.

Note:  varnishd will listen on all available IPv4 and IPv6 interfaces active on the server if you do not specify an address.

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

You can save and close the file now.

Configuring Varnish Backend Servers using VCL

it is time to configure the origin server, known in Varnish terminology as a backend. the server understands HTTP and Varnish talks to, to fetch content – httpd in this case. It is configured in the main configuration file /etc/varnish/default.vcl.

vi /etc/varnish/default.vcl   

You can also change “default” to server1 as there is a default backend configuration section called default. the host parameter points to the localhost, on assumption that the backend server is running on the localhost by default. Now, set the port to 8080 which is the Apache virtual host configuration file.

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

In case your backend server is running on a different host ( another server with address 10.42.1.10) the host parameter should point to this IP address.

backend server1 {      .host = "10.42.1.10";      .port = "8080";  }

You can save and close the file now.

Next, reload the systemd manager configuration to reflect the new changes in the Varnish service file and also restart the Varnish service to apply the overall changes.

systemctl daemon-reload  systemctl restart varnish

Now, Varnish and Apache should now be listening on port 80 and 8080 respectively

ss -tpln

 

 

4- Testing Varnish Cache and Apache Setup

Open a web browser, and navigate using the server IP or FQDN, o test the Varnish Cache-HTTPD setup.

http://10.42.0.144  OR  http://www.eldernode.lan

At this point, you need to check if web pages are being served via Varnish Cache or not. to do this act as below.

1- Check the HTTP headers by right-clicking on the displayed web page.

2- Select Inspect to open the developer tools

3- Click the Network tab, and reload the page

4- Select a request to view the HTTP headers to confirm this

Also, you can use the following command, alternatively to verify it.

curl -I http:///10.42.0.144  OR  curl -I http:///www.eldernode.lan

 

 

Useful Varnish Cache Utility Programs

Here are some of the useful programs that the Varnish Cache distribution comes with, including utilities for varnish cache administration, displaying detailed log records, and view varnish performance statistics as described below.

 

varnishadm

Use the varnishadm to administer a running Varnish instance. It works by establishing a command-line interface connection to varnishd.

Also, it can affect a running instance of Varnish by starting and stopping varnishd, changing configuration parameters, reloading the VCL, listing backends, and more

varnishadm  > backend.list

 

varnishlog

you can use the varnishlog to access request-specific data as information about specific clients and requests. It is better to filter it as it provides large amounts of information.

varnishlog  

 

varnishstat

You can use the varnishstat, to access overall statistics such as the number of total requests, number of objects, and more.

varnishstat  

 

varnishtop

And the varnishtop is a utility that reads the Varnish log and presents a continuously updated list of the most commonly occurring log entries.

varnishtop   

 

varnishhist

Also, you can use the varnishhist as a useful utility, which reads Varnish logs and presents a continuously updated histogram showing the distribution of the last N requests by their processing.

# varnishhist

Recommended Article: Answer To MySQL Queries Troubleshooting

Good job! You have successfully deployed Varnish Cache to accelerate your web application content served using Apache HTTP Server on CentOS/RHEL 8.

How useful was this post?

Click on a star to rate it!

Average rating 1 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

View More Posts
Tom Veitch
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 a Reply

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

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