In this tutorial, we will show you how to install Varnish on Ubuntu 18.04 LTS. For those of you who didn’t know, Varnish Cache is a powerful open-source HTTP accelerator that can be installed in front of any Webserver such as Apache or Nginx. Varnish Cache can improve the overall performance of your web server by caching content. The Varnish cache stores the copy of user requests and serves the same page when the user revisits the webpage. It makes your website really fast and accelerates your website performance up to 300 – 1000x (means 80% or more).
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of Varnish Cache on a Ubuntu 18.04 (Bionic Beaver) server.
Prerequisites
- A server running one of the following operating systems: Ubuntu 18.04 and any other Debian-based distribution like Linux Mint.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Varnish Cache on Ubuntu 18.04 LTS Bionic Beaver
Step 1. First, make sure that all your system packages are up-to-date by running the following apt-get
commands in the terminal.
sudo apt-get update sudo apt-get upgrade
Step 2. Install the Apache webserver.
For this part, we will be assuming that you have already installed Apache on your server and have it running properly. If not write this command in your terminal:
sudo apt-get install apache2
Step 3. Installing Varnish on Ubuntu 18.04 LTS.
Install Varnish using apt-get command:
apt-get install varnish
After the installation is finished, start, and enable varnish.service using the systemctl
command:
systemctl start varnish.service systemctl enable varnish.service
Step 4. Configuring Varnish Cache on Ubuntu 18.04 Bionic Beaver.
The varnish is automatically configured to serve content over port 80 and fetch contents from Apache on port 8080, we need to update Apache to serve content over port 8080:
# If you just change the port or add more ports here, you will likely also # have to change the VirtualHost statement in # /etc/apache2/sites-enabled/000-default.conf NameVirtualHost 127.0.0.1:8080 Listen 127.0.0.1:8080
If you have any virtual hosts configured, you will need to update these as well – ensure your configuration looks like this:
<VirtualHost 127.0.0.1:8080>
We need to configure varnish to run on port 80. First, create a file called varnish.service
inside the /etc/systemd/
system directory:
### nano /etc/systemd/system/varnish.service
Then, add the following configuration:
[Service] ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m
Once you save and exit out of that file, open up the default.vcl
file:
### nano /etc/varnish/default.vcl backend default { .host = "127.0.0.1"; .port = "8080"; }
Restart the Apache and Varnish service for the changes to take effect:
systemctl restart apache2.service systemctl restart varnish.service
You can check to see if varnish is working by typing the following command:
varnishstat
Step 5. Testing Varnish.
The test consists of making an HTTP request via curl and verifying that it is handled by Varnish:
[root@idroot.us ~ ]# curl -I 192.168.146.161 HTTP/1.1 403 Forbidden Date: Mon, 17 Jul 2018 24:06:10 GMT Server: Apache/2.4.6 (Ubuntu) PHP/7.0.16 Last-Modified: Thu, 16 Dec 2017 19:30:58 GMT ETag: "1321-5758ramona728280" Accept-Ranges: bytes Content-Length: 4897 Content-Type: text/html; charset=UTF-8 X-Varnish: 32779 Age: 4 Via: 1.1 varnish-v5 Connection: keep-alive
Congratulations! You have successfully installed Varnish. Thanks for using this tutorial for installing Varnish Cache on Ubuntu 18.04 LTS (Bionic Beaver) system. For additional help or useful information, we recommend you check the official Varnish website.