Installing Varnish on Ubuntu

Tags: ubuntu (2) ops (29)

The easiest way to install Varnish on Ubuntu is by using our official packages. These packages are hosted on Packagecloud and are also available for other Linux distributions.

1. Choosing the right Varnish version

We advise that you install Varnish Cache 6.0 LTS, which is the stable and supported version of Varnish. It is maintained by Varnish Software and receives frequent updates.

The Varnish Cache community does two releases per year, which are considered fresh releases. These releases are primarily featured-based and do not guarantee backward compatibility. Bugs are also fixed in these releases.

2. Register the package repository

Before we can install Varnish, we need to register the right package repository, otherwise the Varnish version of your Linux distribution’s packages might be installed.

Run the following commands to register the official Varnish Cache 6.0 LTS repository:

sudo apt-get update

This command updates the package list to get information on the latest available packages. This is required to successfully run the following command:

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

This command will install the dependencies that are needed to configure the package repository.

The next command will import the GPG key into the package manager configuration:

curl -s -L https://packagecloud.io/varnishcache/varnish60lts/gpgkey | sudo apt-key add -

Now that the dependencies are in place, we can register the package repository:

. /etc/os-release
sudo tee /etc/apt/sources.list.d/varnishcache_varnish60lts.list > /dev/null <<-EOF
deb https://packagecloud.io/varnishcache/varnish60lts/$ID/ $VERSION_CODENAME main
EOF
sudo tee /etc/apt/preferences.d/varnishcache > /dev/null <<-EOF
Package: varnish varnish-*
Pin: release o=packagecloud.io/varnishcache/*
Pin-Priority: 1000
EOF

And finally, we have to update the package list once again. This will ensure the Packagecloud repository is included:

sudo apt-get update

3. Install Varnish

Now that the repositories are registered and the right repository preferences are configured, you can install Varnish by running the following command:

sudo apt-get install varnish

This command will specifically install the latest version of Varnish Cache 6.0 LTS.

4. Configure Varnish

After installing Varnish, you will need to configure some runtime parameters for the varnishd runtime process.

Systemd configuration.

The varnishd process is managed by systemd and has a unit file in /lib/systemd/system/varnish.service as illustrated below:

[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

If you want to override some of the runtime parameters in the varnish.service file, you can run the following command:

sudo systemctl edit --full varnish

An editor will open in which you can edit the unit file. The content in the file comes from /lib/systemd/system/varnish.service.

After performing the changes, make sure you save the file and exit the editor. As a result the /etc/systemd/system/varnish.service file will be created containing the modified unit file.

Modifying the listening port and cache size

Based on the varnish.service unit file above, you can see that the default Varnish runtime configuration is very conservative: the standard listening port is set to 6081.

We’ll change this to port 80. We’ll also increase the size of the cache to two gigabytes.

After having applied the configuration changes, the ExecStart statement now looks like this:

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

5. Configure the web server to work with Varnish

Now that Varnish is configured to listen on port 80, your web server will have to be reconfigured and listen on an alternative port. The most common alternative port for HTTP is port 8080.

Apache

If you’re using Apache, you have replace the listen port value in /etc/apache2/ports.conf from Listen 80 to Listen 8080. You also need to replace <VirtualHost *:80> with <VirtualHost *:8080> in all virtual host files.

The following command will take care of that for all .conf files in the /etc/httpd folder, including its subfolders:

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

Nginx

If you’re using Nginx, it’s simply a matter of modifying the listening port in the various virtual host configurations.

The following command will replace listen 80; with listen 8080; in all virtual host files:

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

This command will replace listen 80; with listen 8080; in all .conf files in the /etc/nginx/ folder and all of its subfolders.

6. VCL backend configuration

Now that we’ve changed the listening port of the origin web server to 8080, this change needs to be reflected in the backend definition of your VCL file.

Luckily the standard VCL file that is part of the Varnish installation already has a default backend definition that points to 127.0.0.1 on port 8080.

The default VCL file is located in /etc/varnish/default.vcl on your system and contains the following backend definition:

vcl 4.1;

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

7. Restart the services

We have made some changes to various configuration files. For these changes to take effect, we need to restart Varnish and your web server.

Apache

Run the following command if your web server is running Apache:

sudo systemctl restart apache2 varnish

Nginx

Run the following command if you’re using Nginx instead of Apache:

sudo systemctl restart nginx varnish