Install Varnish Cache 6 for Apache/Nginx on CentOS 8

Install Varnish Cache 6 for Apache/Nginx on CentOS 8

Varnish Cache is a powerful reverse HTTP proxy that can be used to speed up web applications (available as open source software). Varnish caches static and dynamic content. Before all inbound requests reach your web server backend, it will process all inbound requests. It is located between the web browser and the Apache or Nginx web server. Varnish cache stores all incoming page requests in memory, so the web server does not have to create the same web page again and again.

In this article, we will show you how to install Varnish cache using Apache and Nginx on CentOS 8.

prerequisites

  • Server running CentOS 8.
  • A root password is configured on your server.

getting Started

Before starting, it is recommended to update the system software package to the latest version. You can update them by running the following command:

dnf update -y

After all packages have been updated, you can proceed to the next step.

Install varnish

By default, the Varnish package is included in the CentOS 8 default repository. You can install it by running the following command:

dnf install varnish -y

After installing Varnish, edit the default configuration file of Varnish and define the addresses supported by Nginx or Apache web server.

nano /etc/varnish/default.vcl

Change the following line:

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

Save and close the file when you are done.

Configure varnish

By default, Varnish listens on port 6081. Therefore, you need to configure Varnish to listen on port 80. You can configure it by editing the varnish.service file:

nano /lib/systemd/system/varnish.service

Find the following line:

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

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 to apply the changes:

systemctl daemon-reload

Next, start the Varnish service and use the following command to enable it to start when the system reboots:

systemctl start varnishsystemctl enable varnish

You can also use the following command to check the status of the varnish:

systemctl status varnish

You should get the following output:

? varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/usr/lib/systemd/system/varnish.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-03-12 04:17:32 EST; 4s ago
  Process: 6391 ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
 Main PID: 6392 (varnishd)
    Tasks: 217
   Memory: 99.5M
   CGroup: /system.slice/varnish.service
           ??6392 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
           ??6402 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

Mar 12 04:17:32 masternode systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Mar 12 04:17:32 masternode varnishd[6391]: Debug: Version: varnish-6.0.6 revision 29a1a8243dbef3d973aec28dc90403188c1dc8e7
Mar 12 04:17:32 masternode varnishd[6391]: Debug: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Mar 12 04:17:32 masternode varnishd[6392]: Version: varnish-6.0.6 revision 29a1a8243dbef3d973aec28dc90403188c1dc8e7
Mar 12 04:17:32 masternode varnishd[6392]: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Mar 12 04:17:32 masternode varnishd[6391]: Debug: Child (6402) Started
Mar 12 04:17:32 masternode varnishd[6392]: Child (6402) Started
Mar 12 04:17:32 masternode varnishd[6392]: Child (6402) said Child starts
Mar 12 04:17:32 masternode systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

At this point, Varnish has started and is listening on port 80. You can check it with the following command:

ss -antpl | grep varnishd

You should get the following output:

LISTEN    0         10               127.0.0.1:34977            0.0.0.0:*        users:(("varnishd",pid=1532,fd=11))                                            
LISTEN    0         128                0.0.0.0:80               0.0.0.0:*        users:(("cache-main",pid=1542,fd=6),("varnishd",pid=1532,fd=6))                
LISTEN    0         128                   [::]:80                  [::]:*        users:(("cache-main",pid=1542,fd=7),("varnishd",pid=1532,fd=7))                
LISTEN    0         10                   [::1]:34909               [::]:*        users:(("varnishd",pid=1532,fd=10))                                            

At this point, Varnish will be installed and configured. You can now proceed to the next step.

Install and configure Nginx to use Varnish

First, you need to install the Nginx package on the server. You can use the following command to install:

dnf install nginx -y

After the installation is complete, you need to change the Nginx listening port from 80 to 8080. You can do this by editing the Nginx default configuration file:

nano /etc/nginx/nginx.conf

Change the following line:

        listen       8080 default_server;
        listen       [::]:8080 default_server;

Save and close the file, and then use the following command to verify if there is a syntax error in Nginx:

nginx -t

You should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Next, start the Nginx service and enable it to start when the system reboots:

systemctl start nginxsystemctl enable nginx

You can also use the following command to verify the Nginx listening port:

ss -antpl | grep 8080

You should get the following output:

LISTEN    0         128                0.0.0.0:8080             0.0.0.0:*        users:(("nginx",pid=5569,fd=8),("nginx",pid=5568,fd=8))                        
LISTEN    0         128                   [::]:8080                [::]:*        users:(("nginx",pid=5569,fd=9),("nginx",pid=5568,fd=9))                        

At this point, Varnish cache has been installed and configured to forward all requests from port 80 to the Nginx web server that is listening on port 8080.

You can use the following command to verify that the Varnish cache is working properly:

curl -I https://localhost

You should get the following output:

HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Fri, 12 Mar 2021 09:18:32 GMT
Content-Type: text/html
Content-Length: 4057
Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT
ETag: "5d9bab28-fd9"
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

Install and configure Apache to use Varnish

First, you need to install the Apache Webserver package into the system. You can use the following command to install:

dnf install httpd -y

After installing Apache, edit the Apache default configuration file and change the listening port from 80 to 8080.

nano /etc/httpd/conf/httpd.conf

Change the following line:

Listen 8080

Save and close the file, then start the Apache service, and use the following command to start it when the system restarts:

systemctl start httpdsystemctl enable httpd

You can also use the following command to verify the Apache listening port:

ss -antpl | grep httpd

You should get the following output:

LISTEN    0         128                      *:8080                   *:*        users:(("httpd",pid=7556,fd=4),("httpd",pid=7555,fd=4),("httpd",pid=7554,fd=4),("httpd",pid=7552,fd=4))

At this point, the Varnish cache has been installed and configured to forward all requests from port 80 to the Apache web server that is listening on port 8080.

You can use the following command to verify that the Varnish cache is working properly:

curl -I https://localhost

You should get the following output:

Date: Fri, 12 Mar 2021 09:21:14 GMT
Server: Apache/2.4.37 (centos)
Content-Location: index.html.zh-CN
Vary: negotiate,accept-language
TCN: choice
Last-Modified: Fri, 14 Jun 2019 03:37:43 GMT
ETag: "fa6-58b405e7d6fc0;5bd5365602e05"
Accept-Ranges: bytes
Content-Length: 4006
Content-Type: text/html; charset=UTF-8
Content-Language: zh-cn
X-Varnish: 2
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Connection: keep-alive

Use Varnish command line

Varnish comes with several useful utilities to help you monitor and control Varnish.

Varnishadm is a command line utility that lists the configured backends.

varnishadm

You should see the following output:

200        
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
varnish-6.0.6 revision 29a1a8243dbef3d973aec28dc90403188c1dc8e7

Type 'help' for command list.
Type 'quit' to close CLI session.

Now, run the following command to list all backends:

varnish> backend.list

You should see the following output:

200        
Backend name                   Admin      Probe                Last updated
boot.default                   probe      Healthy (no probe)   Sat, 13 Mar 2021 03:00:02 GMT

varnishlog

The varnishlog utility is used to display logs of all user requests.

varnishlog

You should get the following output:

*   << BeReq    >> 6         
-   Begin          bereq 5 fetch
-   VCL_use        boot
-   Timestamp      Start: 1615604631.507187 0.000000 0.000000
-   BereqMethod    GET
-   BereqURL       /
-   BereqProtocol  HTTP/1.1
-   BereqHeader    Host: 69.87.218.214
-   BereqHeader    Upgrade-Insecure-Requests: 1
-   BereqHeader    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
-   BereqHeader    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
-   BereqHeader    Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
-   BereqHeader    X-Forwarded-For: 106.213.193.241
-   BereqHeader    Accept-Encoding: gzip
-   BereqHeader    X-Varnish: 6
-   VCL_call       BACKEND_FETCH
-   VCL_return     fetch
-   BackendOpen    31 boot.default 127.0.0.1 8080 127.0.0.1 58860
-   BackendStart   127.0.0.1 8080
-   Timestamp      Bereq: 1615604631.507458 0.000270 0.000270
-   Timestamp      Beresp: 1615604631.507844 0.000657 0.000386
-   BerespProtocol HTTP/1.1
-   BerespStatus   200
-   BerespReason   OK
-   BerespHeader   Server: nginx/1.14.1
-   BerespHeader   Date: Sat, 13 Mar 2021 03:03:51 GMT
-   BerespHeader   Content-Type: text/html
-   BerespHeader   Content-Length: 4057
-   BerespHeader   Last-Modified: Mon, 07 Oct 2019 21:16:24 GMT

Varnish

The varnishstat command provides you with information about in-memory statistics, storage, threads created, and objects deleted.

varnishstat

You should see the following screen:

Varnish

The varnishtop command reads the painted shared memory log and displays a continuously updated list of the most frequently occurring log entries.

varnishtop

You should see the following screen:

Varnish

Varnished

Varnishhist parses the varnish log and outputs the last n requests through processing.

varnishhist

You should see the following screen:

Varnished

in conclusion

In the above guide, you learned how to install and configure Varnish cache using Apache and Nginx web server. Now you can implement Varnish caching in a production environment and speed up website performance. If you have any questions, please feel free to ask me.

Related Posts