If you want to install the Varnish cache server on Ubuntu, quickly fire up the terminal and run the command sudo apt install Varnish. However, doing that will not install your system’s latest version of the Varnish cache.
If you need the latest version of Varnish, you’ll have to compile it and install it manually. this post shows students and new users how to compile and install Varnish cache 6.0 manually, the latest at the time of this post, on Ubuntu 16.04 | 18.04 LTS servers.
For this post, we’ll install the Apache2 web server and configure it to listen on port 8080. then install Varnish and configure it to listen on port 80. Varnish will cache all requests for Apache2 pages when they are made.
To get this working, please follow the steps below:
Install Apache2 HTTP Server
To install Apache2 on Ubuntu, run the commands below.
sudo apt-get update sudo apt-get install apache2
After installing Apache2, the commands below can stop, start and enable Apache2 to always startup whenever the server boots up.
sudo systemctl stop apache2.service sudo systemctl start apache2.service sudo systemctl enable apache2.service
The apache2 HTTP service is automatically bound to ports 80 and 443 for HTTPS by default. However, we want Varnish to communicate over port 80 instead. So continue below to configure Apache2 to talk on Port 8080.
To quickly change the port, run the commands below to open the Apache2 default port configuration file.
sudo nano /etc/apache2/ports.conf
Then make sure the file has these lines. Save when done.
# 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
Listen 127.0.0.1:8080
Next, open the Apache2 default virtual host config file.
sudo nano /etc/apache2/sites-available/000-default.conf
Then make the highlighted change below.
<VirtualHost 127.0.0.1:8080>
Save the file and exit.
Then restart Apache2
sudo systemctl restart apache2.service
To access Apache2, enter the server IP or hostname followed by port # 8080.
ex. http://localhost:8080
Installing Varnish 6.0
Now that Apache2 is installed and communicating over port 8080, follow the steps below to install the latest version of Varnish.
First, install all dependencies for Varnish by running the commands below.
sudo apt-get install make automake autotools-dev libedit-dev libjemalloc-dev libncurses-dev libpcre3-dev libtool pkg-config python-docutils python-sphinx graphviz autoconf-archive curl git
Next, run the commands below to download the Varnish package.
cd /tmp && git clone https://github.com/varnishcache/varnish-cache
After that, change into a varnish-cache folder and begin compiling.
cd varnish-cache sudo sh autogen.sh sudo sh configure sudo make
Next, run the commands below to install the Varnish cache.
sudo make install
It should take a few minutes for it to install. After that, Varnish will now be installed in /usr/local. The varnish binary is in /usr/local/sbin/varnishd. To make sure that the necessary links and caches of the most recent shared libraries are found, run
sudo ldconfig
If Varnish was successfully installed, run the commands below to start it.
sudo varnishd -a :80 -T localhost:6082 -b localhost:8080
That should do it. To test, run the commands below.
curl -I http://localhost
And you should see something like the text below:
HTTP/1.1 200 OK Date: Wed, 25 Jul 2018 17:06:30 GMT Server: Apache/2.4.29 (Ubuntu) Last-Modified: Wed, 25 Jul 2018 16:33:11 GMT Vary: Accept-Encoding Content-Type: text/html X-Varnish: 32770 Age: 0 Via: 1.1 varnish (Varnish/6.0) ETag: W/"2aa6-571d56f3d8a6a-gzip" Accept-Ranges: bytes Connection: keep-alive
Enjoy!
How to make systemctl restart varnishd service?
Who can or will use a configuration without SSL?