We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Configure Varnish Cache 7 on Ubuntu 22.04|20.04|18.04

Learn how to – Configure Varnish Cache 7 on Ubuntu 22.04|20.04|18.04.

Varnish Cache is an open-source powerful HTTP and reverse-proxy engine designed for high-load dynamic websites as well as APIs. Varnish Cache as opposed to other web accelerators such as Squid began as a client-side cache. It grew with the main focus on HTTP, unlike other proxy servers which have added support for SMTP, FTP e.t.c.

Normally, Varnish Cache sits behind a web server and speeds the website by approximately between 300-1000 times. This is achieved by caching requested websites in memory and serving them when requested without building the same information. More so Varnish Cache can act as a load balancer when running multiple servers.

Varnish Cache features the following:

  • DNS, Random, hashing and client IP-based directors
  • HTTP streaming pass & fetch
  • Plugin support with Varnish Modules/VMODs
  • Support for Edge Side Includes including stitching together compressed ESI fragments
  • Supports Gzip Compression and Decompression
  • Support for Persistent Storage, without LRU eviction
  • Varnish Private CDN
  • Varnish API and Web Acceleration

This guide provides you with the required steps on how to configure Varnish Cache 7 for Apache/Nginx on Ubuntu 22.04|20.04|18.04.

Getting Started

For this guide, you will require the following:

  • Ubuntu 22.04|20.04|18.04 installed on your system.
  • User with sudo access.

Update your system packages to their latest available versions.

sudo apt update && sudo apt -y full-upgrade

Perform a reboot if it’s required:

[ -f /var/run/reboot-required ] && sudo reboot -f

Step 1 – Register Varnish Cache 7 repo on Ubuntu 22.04|20.04|18.04

We will begin by registering the Varnish Cache 7 repository otherwise the right packages may not be installed.

Install the required package dependencies.

sudo apt install debian-archive-keyring curl gnupg apt-transport-https -y

Now import the GPG key signing.

curl -fsSL https://packagecloud.io/varnishcache/varnish70/gpgkey|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/varnish.gpg

Now add the Varnish Cache 7 repository on your Ubuntu 22.04|20.04|18.04.

Ubuntu 20.04 / 18.04:

. /etc/os-release
sudo tee /etc/apt/sources.list.d/varnishcache_varnish70.list > /dev/null <<-EOF
deb https://packagecloud.io/varnishcache/varnish70/$ID/ $VERSION_CODENAME main
deb-src https://packagecloud.io/varnishcache/varnish70/$ID/ $VERSION_CODENAME main
EOF

Ubuntu 22.04:

Before official repository release for Ubuntu 22.04, you’ll have to use 20.04 repository:

sudo tee /etc/apt/sources.list.d/varnishcache_varnish70.list > /dev/null <<-EOF
deb https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main
deb-src https://packagecloud.io/varnishcache/varnish70/ubuntu/ focal main
EOF

Once added update your package index.

sudo apt update

Step 2 – Install Varnish Cache 7 on Ubuntu 22.04|20.04|18.04

Once the repository has been added, install Varnish Cache 7 using the command below.

sudo apt install varnish

Dependency tree:

Reading state information... Done
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-9 gcc gcc-9
  gcc-9-base libasan5 libatomic1 libbinutils libc-dev-bin libc6-dev
  libcc1-0 libcrypt-dev libctf-nobfd0 libctf0 libgcc-9-dev libgomp1
  libisl22 libitm1 libjemalloc2 liblsan0 libmpc3 libquadmath0 libtsan0
  libubsan1 linux-libc-dev manpages-dev
Suggested packages:
  binutils-doc cpp-doc gcc-9-locales gcc-multilib make autoconf automake
  libtool flex bison gdb gcc-doc gcc-9-multilib gcc-9-doc glibc-doc
  varnish-dev
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-9 gcc gcc-9
  gcc-9-base libasan5 libatomic1 libbinutils libc-dev-bin libc6-dev
  libcc1-0 libcrypt-dev libctf-nobfd0 libctf0 libgcc-9-dev libgomp1
  libisl22 libitm1 libjemalloc2 liblsan0 libmpc3 libquadmath0 libtsan0
  libubsan1 linux-libc-dev manpages-dev varnish
0 upgraded, 30 newly installed, 0 to remove and 73 not upgraded.
Need to get 35.5 MB of archives.
After this operation, 154 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Step 3 – Create Systemd service file for Varnish Cache 7

To be able to manage Varnish Cache 7 like other system services, we will adjust the systemd service as below.

sudo cp /lib/systemd/system/varnish.service /etc/systemd/system/
cat /etc/systemd/system/varnish.service

The service file should appear as shown.

[Unit]
Description=Varnish Cache, a high-performance HTTP accelerator
After=network-online.target nss-lookup.target

[Service]
Type=forking
KillMode=process

# Maximum number of open files (for ulimit -n)
LimitNOFILE=131072

# Locked shared memory - should suffice to lock the shared memory log
# (varnishd -l argument)
# Default log size is 80MB vsl + 1M vsm + header -> 82MB
# unit is bytes
LimitMEMLOCK=85983232

# Enable this to avoid "fork failed" on reload.
TasksMax=infinity

# Maximum size of the corefile.
LimitCORE=infinity

ExecStart=/usr/sbin/varnishd 
	  -a :6081 
	  -a localhost:8443,PROXY 
	  -p feature=+http2 
	  -f /etc/varnish/default.vcl 
	  -s malloc,256m
ExecReload=/usr/sbin/varnishreload

[Install]
WantedBy=multi-user.target

Step 4 – Configure the listening port and cache size

From the service file, the default listening port is set to 6081, so we need to change this port to port 80 and cache size to 2GB.

In the /etc/systemd/system/varnish.service, edit the below part and add the desired details.

$ sudo vim /etc/systemd/system/varnish.service
ExecStart=/usr/sbin/varnishd 
	  -a :80 
	  -a localhost:8443,PROXY 
	  -p feature=+http2 
	  -f /etc/varnish/default.vcl 
	  -s malloc,2g

Reload the system daemon.

sudo systemctl daemon-reload

Start and enable Varnish cache.

sudo systemctl start varnish

Check the status of the service.

$ systemctl status varnish
 varnish.service - Varnish Cache, a high-performance HTTP accelerator
     Loaded: loaded (/etc/systemd/system/varnish.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-02-08 09:22:45 UTC; 7s ago
    Process: 9945 ExecStart=/usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,2g (c>
   Main PID: 9954 (varnishd)
      Tasks: 217
     Memory: 91.6M
     CGroup: /system.slice/varnish.service
             ├─9954 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,2g
             └─9980 /usr/sbin/varnishd -a :80 -a localhost:8443,PROXY -p feature=+http2 -f /etc/varnish/default.vcl -s malloc,2g

Feb 08 09:22:45 ubuntu systemd[1]: Starting Varnish Cache, a high-performance HTTP accelerator...
Feb 08 09:22:45 ubuntu varnishd[9954]: Version: varnish-7.0.2 revision 9b5f68e19ca0ab60010641e305fd12822f18d42c
Feb 08 09:22:45 ubuntu varnishd[9954]: Platform: Linux,5.4.0-90-generic,x86_64,-junix,-smalloc,-sdefault,-hcritbit
Feb 08 09:22:45 ubuntu varnishd[9954]: Child (9980) Started
Feb 08 09:22:45 ubuntu varnishd[9954]: Child (9980) said Child starts

Step 5 – Configure the web server to work with Varnish

With Varnish cache set to listen on port 80, we have to set the Web server to listen on another port say 8080. Make the configurations for the desired web server.

1. Nginx

Ensure the Nginx web server is installed.

sudo apt install nginx

Now edit the Virtual hosts and replace the port 80 to 8080 using the below command:

sudo find /etc/nginx/sites-enabled -name '*.conf' -exec sed -r -i 's/blisten ([^:]+:)?80b([^;]*);/listen 180802;/g' {} ';'

The above script will edit any config in the path /etc/nginx/sites-enabled . You may also need to change the default Nginx site to listen on port 8080.

$ sudo vim /etc/nginx/sites-enabled/default
......
server {
        listen 8080 default_server;
        #listen [::]:80 default_server;

        # SSL configuration
        #

Restart nginx service after the change:

sudo systemctl restart nginx

2. Apache

With the Apache Web server installed.

sudo apt install apache2

Also here, you need to edit the virtual hosts files and change the listen port to 8080.

sudo find /etc/apache2 -name '*.conf' -exec sed -r -i 's/bListen 80b/Listen 8080/g; s/<VirtualHost ([^:]+):80>/<VirtualHost 1:8080>/g' {} ';'

Restart Apache web server after the change:

sudo systemctl restart apache2

Step 6 – Configure the VCL backend

The changes made above need to be reflected in the VLC backend. By default, the VLC backend is configured to point on the set port 8080. This file is found under /etc/varnish/default.vcl.

$ sudo vim /etc/varnish/default.vcl
......
# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}
......

Now restart the services.

##For Nginx
sudo systemctl restart nginx varnish

##For Apache
sudo systemctl restart apache2 varnish

Step 7 – Verify Varnish Cache 7 Server

To verify ig the server is working appropriately, we will use the cURL command below.

curl -I server-ip

For example.

curl -I 192.168.205.11

Output:

From the output, you should see the web server being used with Vanish Cache.

Conclusion

With that, we are safe to conclude that this guide on how to configure Varnish Cache 7 for Apache/Nginx on Ubuntu 22.04|20.04|18.04 is a success. Now you should be able to boost your web page access speeds with Varnish Cache.

See more:

Cart

Your Cart is Empty

Back To Shop