Varnish is a high performance caching module commonly used for Apache web server. Here’s how to install Varnish in Ubuntu. In this article we will see how to setup varnish, how to change port on varnish, how to configure varnish and how to restart varnish. You can also use these steps to install Varnish in CentOS, Debian, Redhat and other Linux systems.
How to Install Varnish in Ubuntu
Here are the steps to install Varnish in Ubuntu.
1. Install Apache
If you have already installed Apache web server, you can skip to step 3.
Open terminal, and run the following command to update Ubuntu modules.
$ sudo apt-get update
Then run the following command to install Apache on Ubuntu.
$ sudo apt-get install apache2
Enter Y if asked for confirmation.
Bonus Read : Find Apache Document Root in Ubuntu
2. Test Apache installation
Open web browser and type the following in address bar
http://127.0.0.1
You will see the default Apache web page with message “It works!”
3. Install Varnish HTTP Cache
Run the following command to install Varnish in Ubuntu.
$ sudo apt-get install varnish
Enter Y when asked for confirmation.
Bonus Read : How to Fix 503 Service Temporarily Available Error
4. Change ports for Apache
By default, Apache runs on port 80. We need to make Varnish run on port 80 and Apache run on another port (e.g 8080) to install Varnish in Ubuntu.
So open Apache’s port configuration file using the following command
$ sudo nano /etc/apache2/ports.conf
Change the line Listen 80 to Listen 8080 so it will look as shown below
Listen 8080 <IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule>
Save and exit the file.
We also need to change listening port for Apache virtual hosts. Open Apache’s default virtual host file
$ sudo nano /etc/apache2/sites-enabled/000-default.conf
Change <VirtualHost *:80> to <VirtualHost *:8080> to make it as shown below.
<VirtualHost *:8080> ... </VirtualHost>
Save and exit the file.
Restart Apache web server to apply changes and make it run on port 8080.
$ sudo service apache2 restart
Bonus Read : How to Redirect Without Changing URL
5. Change Varnish Port to 80
Next we configure Varnish to listen to port 80 and forward requests to port 8080. For that, open Varnish configuration file
$ sudo nano /etc/default/varnish
Look for DAEMON_OPTS directive and change the listening port from 6081 to 80
DAEMON_OPTS="-a :80 \ -T localhost:6082 \ -f /etc/varnish/default.vcl \ -S /etc/varnish/secret \ -s malloc,256m"
Save and close the file
Next, open /etc/varnish/default.vcl using text editor.
$ sudo nano /etc/varnish/default.vcl
Its contents will look something like the following. Make sure the port value is 8080 as shown in bold
# Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; }
Lastly, we need to edit the port number in /lib/systemd/system/varnish.service. Open it in a text editor
$ sudo nano /lib/systemd/system/varnish.service
Change the port from 6081 to 80 as show in bold
[Unit] Description=Varnish HTTP accelerator Documentation=https://www.varnish-cache.org/docs/4.1/ man:varnishd [Service] Type=simple LimitNOFILE=131072 LimitMEMLOCK=82000 ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f$ ExecReload=/usr/share/varnish/varnishreload ProtectSystem=full ProtectHome=true PrivateTmp=true PrivateDevices=true [Install] WantedBy=multi-user.target
Bonus Read : How to Fix 500 Internal Server Error in Apache
6. Restart Apache, Varnish and Systemd Daemon
Open terminal and run the following commands to restart Apache, Varnish and Systemd Daemon. This will apply all changes.
$ sudo systemctl restart apache2 $ sudo systemctl daemon-reload $ sudo systemctl restart varnish
7. Test the Varnish Configuration
Run the following curl command to get the output from our web server
$ curl -I server_IP
You will see response similar to following. The response header parts in bold indicates that Varnish cache is working.
HTTP/1.1 200 OK Date: Thu, 05 Jul 2018 20:56:11 GMT Server: Apache/2.4.29 (Ubuntu) Last-Modified: Fri, 29 Jun 2018 07:19:34 GMT Vary: Accept-Encoding Content-Type: text/html X-Varnish: 7 Age: 0 Via: 1.1 varnish (Varnish/5.2) ETag: W/"2aa6-56fc2ab77545d-gzip" Accept-Ranges: bytes Connection: keep-alive
That’s it! You have installed Varnish cache in Ubuntu.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.