<\/p>\n
In this tutorial, we are going to show you how to install Varnish on Ubuntu 22.04.<\/p>\n
Varnish cache is a web application accelerator that is used as caching HTTP reverse proxy. The varnish case has many advantages against other caching software and can speed up delivery up to 1000 times. Varnish offers many features such as private CDN, Gzip compression and decompression, HTTP streaming pass & fetch, etc.<\/p>\n
In this blog post for the installation, we are going to configure it with the Apache web server<\/a>. This process is very easy and may take up to 20 minutes. Let’s get started!<\/p>\n
Table of Contents<\/p>\n
If you have a fresh installation of Ubuntu 22.04, update the system packages to the latest versions available.<\/p>\n
sudo apt update -y && sudo apt upgrade -y<\/pre>\n<\/span>Step 2. Install Varnish<\/span><\/h2>\n
First, we are going to add the Varnish repository because it does not exist in the default Varnish repository.<\/p>\n
tee \/etc\/apt\/sources.list.d\/varnishcache_varnish70.list > \/dev\/null <<-EOF\r\ndeb https:\/\/packagecloud.io\/varnishcache\/varnish70\/ubuntu\/ focal main\r\ndeb-src https:\/\/packagecloud.io\/varnishcache\/varnish70\/ubuntu\/ focal main\r\nEOF\r\n<\/pre>\nOnce added, update the system<\/p>\n
and<\/p>\n
sudo apt-update -y<\/pre>\nInstall the varnish cache with the commands below:<\/p>\n
sudo apt install varnish -y<\/pre>\nOnce installed, start and enable the service.<\/p>\n
sudo systemctl start varnish && sudo systemctl start varnish<\/pre>\nTo check if the service is up and running, type the following command:<\/p>\n
sudo systemctl status varnish<\/pre>\nYou should receive the following output:<\/p>\n
root@host:~# systemctl status varnish\r\n\u25cf varnish.service - Varnish Cache, a high-performance HTTP accelerator\r\n Loaded: loaded (\/lib\/systemd\/system\/varnish.service; enabled; vendor preset: enabled)\r\n Active: active (running) since Wed 2022-09-21 16:13:26 CDT; 1 week 5 days ago\r\n Docs: https:\/\/www.varnish-cache.org\/docs\/\r\n man:varnishd\r\n Main PID: 2449 (varnishd)\r\n Tasks: 217 (limit: 4575)\r\n Memory: 92.4M\r\n CPU: 11min 37.355s\r\n CGroup: \/system.slice\/varnish.service\r\n \u251c\u25002449 \/usr\/sbin\/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f \/etc\/varnish\/default.vcl -S \/etc\/varnish\/secret -s malloc,256m\r\n \u2514\u25002471 \/usr\/sbin\/varnishd -j unix,user=vcache -F -a :6081 -T localhost:6082 -f \/etc\/varnish\/default.vcl -S \/etc\/varnish\/secret -s malloc,256m\r\n\r\n<\/pre>\nBy default, the varnish service is running on port 6081<\/b>. You can check this with the command below:<\/p>\n
netstat -tunlp | grep 6081<\/pre>\nYou should receive the following output:<\/p>\n
root@host:~# netstat -tunlp | grep 6081\r\ntcp 0 0 0.0.0.0:6081 0.0.0.0:* LISTEN 2449\/varnishd\r\ntcp6 0 0 :::6081 :::* LISTEN 2449\/varnishd\r\n<\/pre>\nThis is another proof that the varnish cache is installed successfully and the service is up and running.<\/p>\n
<\/span>Step 3. Install Apache2<\/span><\/h2>\n
First, we need to install the Apache Web server to configure Varnish later. To install the Apache Web server execute the following command:<\/p>\n
sudo apt install apache2 -y<\/pre>\nTo start and enable the Apache service execute the following command:<\/p>\n
sudo systemctl start apache2 && sudo systemctl enable apache2<\/pre>\nTo check the status of the service:<\/p>\n
sudo systemctl status apache2<\/pre>\nYou should receive the following output:<\/p>\n
root@host:~# sudo systemctl status apache2\r\n\u25cf apache2.service - The Apache HTTP Server\r\n Loaded: loaded (\/lib\/systemd\/system\/apache2.service; enabled; vendor preset: enabled)\r\n Active: active (running) since Wed 2022-09-21 15:52:40 CDT; 1 week 5 days ago\r\n Docs: https:\/\/httpd.apache.org\/docs\/2.4\/\r\n Main PID: 767 (apache2)\r\n Tasks: 55 (limit: 4575)\r\n Memory: 13.9M\r\n CPU: 1min 15.604s\r\n CGroup: \/system.slice\/apache2.service\r\n \u251c\u2500 767 \/usr\/sbin\/apache2 -k start\r\n \u251c\u2500123603 \/usr\/sbin\/apache2 -k start\r\n \u2514\u2500123604 \/usr\/sbin\/apache2 -k start\r\n\r\n<\/pre>\nBy default, the Apache service is running on port 80<\/b>. We need to change that for we can configure the Varnish cache to work with the Apache web server late.<\/p>\n
To change the Apache port from 80<\/b> to 8080<\/b>, open the file \/etc\/apache2\/ports.conf<\/b> with your favorite text editor and change the Listen 80 to Listen 8080 to look like this:<\/p>\n
Listen 8080\r\n<\/pre>\nSave the file, close it and restart the Apache service.<\/p>\n
sudo systemctl restart apache2<\/pre>\nTo check if everything is OK, execute the following command:<\/p>\n
netstat -tunlp | grep apache2<\/pre>\nroot@host:# netstat -tunlp | grep apache2\r\ntcp6 0 0 :::8080<\/b> :::* LISTEN 127811\/apache2\r\n<\/pre>\n<\/span>Step 4. Configure the Varnish Cache<\/span><\/h2>\n
Now, when Varnish is installed and Apache is listening on port 8080, we can proceed with configuring the Varnish. First of all, we are going to change the default listening Varnish port from 6081 to 80.<\/p>\n
Open the \/lib\/systemd\/system\/varnish.service<\/b> file and change the port to 80, along with the cache size to 1GB to look like this:<\/p>\n
ExecStart=\/usr\/sbin\/varnishd \\\r\n -j unix,user=vcache \\\r\n -F \\\r\n -a :80 \\\r\n -T localhost:6082 \\\r\n -f \/etc\/varnish\/default.vcl \\\r\n -S \/etc\/varnish\/secret \\\r\n -s malloc,1g\r\n<\/pre>\nSave the file, close it and reload the daemon and restart the Varnish service.<\/p>\n
sudo systemctl daemon-reload && sudo systemctl restart varnish service<\/pre>\nNext is to check the Varnish cache backend. Open the file \/etc\/varnish\/default.vcl<\/b> and check if the changes are affected in this file. The lines should look like this:<\/p>\n
backend default {\r\n .host = \"127.0.0.1\";\r\n .port = \"8080\";\r\n}\r\n<\/pre>\nRestart the Apache and Varnish services again:<\/p>\n
sudo systemctl restart apache2 varnish<\/pre>\nThe final command is to check with curl<\/b> command if Varnish is working properly:<\/p>\n
curl -I YourIPAddressHere<\/b><\/pre>\nroot@host:\/etc\/apache2# curl -I YourIPAddressHere<\/b>\r\nHTTP\/1.1 200 OK\r\nDate: Tue, 04 Oct 2022 14:05:59 GMT\r\nServer: Apache\/2.4.52 (Ubuntu)\r\nLast-Modified: Wed, 21 Sep 2022 20:51:19 GMT\r\nVary: Accept-Encoding\r\nContent-Type: text\/html\r\nX-Varnish: 2\r\nAge: 0\r\nVia: 1.1 varnish (Varnish\/6.6)\r\nETag: W\/\"119c-5e9361b575472-gzip\"\r\nAccept-Ranges: bytes\r\nConnection: keep-alive<\/pre>\nCongratulations! You successfully installed and configured the Varnish cache on Ubuntu 22.04<\/p>\n
If you have any difficulties completing this setup on your own, just sign up for one of our NVMe VPS plans<\/a>, and our admins will help you. You can contact us anytime you want. We are available 24\/7 via live chat and support tickets.<\/p>\n