Home / How To / How to install and install Varnish Cache 6 with Nginx on CentOS 8

How to install and install Varnish Cache 6 with Nginx on CentOS 8



Varnish Cache is a free and open source reverse proxy HTTP accelerator for websites and APIs. It can be installed as a reverse proxy in front of any web server running on HTTP. It sits between the web server and the browser and stores web pages, cookies and other data in memory. This cache will be used to serve all future requests for exactly similar content. This increases the load on the web application and improves the performance of the web server by 300+ times.

In this tutorial, we will set up the Nginx server as a backend server and configure it to listen on port 8080 and then configure Varnish cache to listen on standard HTTP port 80.

Conditions

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

Install and configure Nginx web server

First you need to install the Nginx web server in your system. You can install it with the following command:

dnf install nginx -y

After installing, start the Nginx service and allow it to restart at system startup with the following command:

systemctl start nginx
systemctl enable nginx

By default, Nginx listens on port 80. So you need to configure Nginx to listen on port 80. You can configure it by editing Nginx’s default configuration file:

nano /etc/nginx/nginx.conf

Find the following lines:

        listen       80 default_server;
        listen       [::]:80 default_server;

And replace them with the following lines:

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

Save and close the file when you are done. Then restart the Nginx service to apply the changes:

systemctl restart nginx

At this time, the Nginx Web server is installed and listening on port 8080. You can check it with the following command:

netstat -tpln | grep 8080

You should get the following output:

tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      30367/nginx: master 
tcp6       0      0 :::8080                 :::*                    LISTEN      30367/nginx: master 

Install Varnish Cache

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

dnf module install varnish

After installing, start the Varnish service with the following command:

systemctl start varnish

You can also verify the installed version of Varnish cache with the following command:

varnishd -V

You should see the following output:

varnishd (varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2018 Varnish Software AS

Configure Varnish for Nginx

Next, you need to configure varnish to run on port 80 to receive HTTP requests from clients. You can configure it by editing Varnish’s default configuration file:

systemctl edit --full varnish

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 when you are done.

Next, you need to define the backend server for Varnish. You can do this by editing the Varnish main configuration file /etc/varnish/default.vcl:

nano /etc/varnish/default.vcl

Change the following lines:

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

Save and close the file when you are done. Then reload the systemd daemon with the following command:

systemctl daemon-reload

Then restart the Varnish service and activate it when restarting the system with the following command:

systemctl restart varnish
systemcl enable varnish

You can also verify the status of the paint with the following command:

systemctl status varnish

You should get the following output:

? varnish.service - Varnish Cache, a high-performance HTTP accelerator
   Loaded: loaded (/etc/systemd/system/varnish.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-08-29 09:36:58 EDT; 12s ago
  Process: 30421 ExecStart=/usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m (code=exited, status=0/SUCCESS)
 Main PID: 30424 (varnishd)
    Tasks: 217
   Memory: 91.8M
   CGroup: /system.slice/varnish.service
           ??30424 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m
           ??30434 /usr/sbin/varnishd -a :80 -f /etc/varnish/default.vcl -s malloc,256m

Aug 29 09:36:57 centos8 systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Aug 29 09:36:58 centos8 varnishd[30421]: Debug: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91
Aug 29 09:36:58 centos8 varnishd[30421]: Debug: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Aug 29 09:36:58 centos8 varnishd[30424]: Version: varnish-6.0.2 revision 0458b54db26cfbea79af45ca5c4767c7c2925a91
Aug 29 09:36:58 centos8 varnishd[30424]: Platform: Linux,4.18.0-193.6.3.el8_2.x86_64,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Aug 29 09:36:58 centos8 varnishd[30421]: Debug: Child (30434) Started
Aug 29 09:36:58 centos8 varnishd[30424]: Child (30434) Started
Aug 29 09:36:58 centos8 varnishd[30424]: Child (30434) said Child starts
Aug 29 09:36:58 centos8 systemd[1]: Started Varnish Cache, a high-performance HTTP accelerator.

You can now verify the Nginx and Varnish listening port with the following command:Advertisement

netstat -tpln | grep 80

You should see the following output:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      30424/varnishd      
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      30367/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      30424/varnishd      
tcp6       0      0 :::8080                 :::*                    LISTEN      30367/nginx: master 

Configure SELinux and firewall

By default, SELinux is enabled in CentOS 8. So you need to configure SELinux for Varnish. You can configure it with the following command:

setsebool -P httpd_can_network_connect 1

Next, you must enable port 80 through the firewall. You can do this with the following command:

firewall-cmd --permanent --zone public --add-port 80/tcp
firewall-cmd --reload

When you are done, you can move on to the next step.

Test Varnish Cache

At this time, the Varnish cache is installed and configured with the Nginx web server. Now it’s time to test whether Varnish cache works or not.

You can test it with the curl command shown below:

curl -I http://localhost

You should see the following output:

																																																																																																																																																																																																																																																																																																																																																																		
HTTP/1.1 200 OK
Server: nginx/1.14.1
Date: Sat, 29 Aug 2020 13:53:44 GMT
Content-Type: text/html; charset=UTF-8
X-Powered-By: PHP/7.2.24
X-Varnish: 32800
Age: 0
Via: 1.1 varnish (Varnish/6.0)
Accept-Ranges: bytes
Connection: keep-alive

Conclusion

Congratulations! you have successfully installed Varnish Cache with Nginx on CentOS 8. I hope you can also easily configure Varnish cache with Apache web server. Feel free to ask me if you have any questions.


Source link