Varnish is an open source cache server which stores content from a web server. It is installed in front of a web server such as Apache or Nginx. The caching mechanism offered by Varnish allows content delivery with high performance.
In this tutorial, you will learn how to install Varnish cache 5 as a front end to the Nginx web server on FreeBSD 11.
Prerequisites
- A Vultr 64-bit FreeBSD 11 server instance.
- A sudo user.
Step 1: Perform a system update
Before installing any packages on the FreeBSD server instance, it is recommended to update the system. Log in using the sudo user and run the following commands to update the system.
sudo pkg update
sudo pkg upgrade
Step 2: Install Nginx
As we are going to install Varnish in front of Nginx, start by installing the Nginx web server.
sudo pkg install nginx
The above command will install the most recent stable port of Nginx available from the default repository of FreeBSD.
To enable Nginx to automatically start at boot time, run the following command.
sudo sysrc nginx_enable=yes
Start Nginx by running the following command.
sudo service nginx start
You will see the following output when Nginx starts.
$ sudo service nginx start
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.
Try to visit the default web page of Nginx by visiting http://Vultr_Server_IP
to verify your installation.
Step 3: Configure Nginx
By default, Nginx listens to port 80, which is the default port for HTTP. As we plan to run Nginx behind Varnish, we will need to change the port of Nginx. Open the default Nginx configuration file using your favorite editor.
sudo nano /usr/local/etc/nginx/nginx.conf
Run sudo pkg install nano
if you wish to install nano
. Find the lines:
server {
listen 80;
server_name localhost;
Change the “listen” port from 80
to 8080
.
Restart the Nginx server.
sudo service nginx restart
Step 4: Install and configure Varnish
Now that we have configured Nginx, install Varnish 5 by typing:
sudo pkg install varnish5
Once installed, add Varnish cache to the system’s rc.conf
file by running the following commands:
sudo sysrc varnishd_enable=YES
sudo sysrc varnishd_listen=":80"
sudo sysrc varnishd_backend="localhost:8080"
sudo sysrc varnishd_storage="malloc,512M"
sudo sysrc varnishd_admin=":8081"
The above commands will create few entries in the rc.conf
file. varnishd_listen
specifies that the Varnish cache server will listen to port ’80’. It will enable Varnish cache to automatically start at boot time. varnishd_backend
specifies the server running on the backend. In our case, it is Nginx web server running on port 8080. varnishd_storage
specifies the cache storage method. You can use malloc
or file
method. malloc
is fast as it uses memory to store the cache. Here, you can specify the amount of RAM varnish can use to store its cache.
Finally, start the server by running:
/usr/local/etc/rc.d/varnishd start
You can now check if varnish is running successfully by browsing to: http://Vultr_Server_IP
.
You can stop the server by running:
/usr/local/etc/rc.d/varnishd stop
Varnish cache 5 with Nginx as the backend server is now installed on your FreeBSD 11 server.
Want to contribute?
You could earn up to $300 by adding new articles
Suggest an update
Request an article