Recommended VPS Complete list of best VPS hosting providers.

Configuring Varnish Cache to Run With Vesta Control Panel

A guide with screenshot pics on how to setup Varnish Cache server and get it running together with full Vesta CP which by default comes with LAMP and Nginx as frontend proxy. This article is a follow up of my previous article and here I come with it as promised. I tagged this article as advanced because you have to firstly read previous tutorials on how to install VestaCP and Varnish Cache.

Before we start, you may wondering does we have to really do this? Is installing Varnish on Vesta really necessary? Well, the answer depends whether you really need it or not. Some people say that installing Varnish in front of Nginx is not quite necessary because Nginx itself is pretty good handling static web files. My personal opinion, if you use Varnish setup to store caches in Disk (even it is SSD) then you better ditch it. Varnish with Nginx will be great only if you use RAM-based caching because theoretically it is slightly faster than having to pass the process to disk.

Prerequisites

So, here’s what you gonna need:

  1. A SSH client like Putty and basic knowledge about how to use it.
  2. Basic knowledge about common Unix command to SSH to your server.
  3. A server or VPS with at least 512MB of RAM (1GB recommended). If you not already have one then you can read my list of recommended VPS providers or this top low end cloud server providers. Quick recommendation: Atlantic.net, Digital Ocean and RamNode.
  4. Make sure your server is running one of VestaCP’s supported OS: RHEL 5/6, CentOS 5/6, Debian 7 or Ubuntu 12.04/12.10/13.04/13.10/14.04. Using minimal template is recommended.
  5. About 20 minutes of your time
  6. a cup of coffee or tea.

Stage 1 – Installing VestaCP

Before we continue, I should tell you that the tutorial below done in a VPS with 512MB RAM running CentOS 6.6 32-bit. It is recommended to use higher RAM. You have to adjust the command if your server is running other than CentOS.

Step 1 – Login to your server as root

Step 2 – Install Vesta CP using command below:

curl -O http://vestacp.com/pub/vst-install.sh
bash vst-install.sh

2015-07-19_210455

Step 3 – It will then ask you with series of questions. Just answer it accordingly:

2015-07-19_210718

Step 4 – Once done you’ll see something like this which you can see your login URL along with admin username and some random password (you can change that later).

2015-07-19_211209

More explanation and screenshots:

Stage 2 – VestaCP Basic Configuration

Some tasks you have to firstly proceed are:

  1. Changing default admin password
  2. Adjust hosting package configuration
  3. Add new website on it (DNS entries will be generated automatically via this task)
  4. Add new database on it (if your website need it)
  5. Uploading / migrating your website on it

I don’t explain it again because it is explanatory or you can simply refer to my previous guides:

Configuration I use for the purpose of this tutorial:

  • VestaCP user: admin
  • Domain: servermomtest.org (doesn’t exist, test purpose only)
  • Package: Default (httpd, nginx and named – all default)
  • App: WordPress installed.
  • Web directory: /home/admin/web/servermomtest.org/public_html

Stage 3 – Installing Varnish Cache

In this article I use Varnish Cache v3.x simply because some syntax on its configuration file has been changed in version 4.x – which I don’t quite familiar yet. However feel free to use version 4.x if you wish and I wrote a tutorial about it as well:

Steps below are the short version of my previous guide to install Varnish Cache 3 on CentOS:

Step 1 – Go to Varnish website to see stable version available at
https://www.varnish-cache.org/releases

2015-07-19_221127

In my situation the current stable release is Varnish Cache 4.0.3 while its v3 is Varnish Cache 3.0.7. Click on the Instructions link to see where you can get its package.

Step 2 – Add Varnish repository for the version you want. Command below will add Varnish 3.0.7:

rpm -Uvh https://repo.varnish-cache.org/redhat/varnish-3.0.el5.rpm

pic:

2015-07-19_221539

Step 3 – Now you can install Varnish using yum:

yum install varnish -y

pic:

2015-07-19_221720

Done. Varnish is now installed (but not running yet)

Stage 4 – Varnish Configuration

Step 1 – We have to setup Varnish so it will run on port 80. The scenario will be like this: Varnish listen on port 80, Nginx on port 8082, then Apache on port 8080.

Web request = Varnish (80) -> Nginx (8082) -> Apache (8080)

Now use your favorite text editor to edit Varnish configuration. For me Nano will come in handy:

nano /etc/sysconfig/varnish

The file contains 4 alternative configurations and only 1 is active which is the one with no comment mark (#) which in my situation it is Alternative 3.

2015-07-19_222618

2015-07-19_222702

Step 2 – Change few important lines started with VARNISH_LISTEN_PORT. Change it to 80

2015-07-19_223011

Step 3 – Scroll down the page then find and change VARNISH_STORAGE_SIZE to reasonable amount of RAM you want to allocate it as storage for the cache files made by Varnish. As my VPS has only 512MB of RAM so I will allocate 256MB of it.

Step 4 – Also change

VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"

to this:

VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}"

so it will look like this:

2015-07-19_223713

Now save changes and exit text editor. In Nano it is Control+O then Control+X.

Step 5 – Next, configure Varnish default VCL file located in /etc/varnish/. That VCL file holds configuration to tell varnish where to look for the webserver content:

nano /etc/varnish/default.vcl

Here what you gonna do:

  1. change .host ip from 127.0.0.1 to your server public IP
  2. change .port 80 to .port 8082 which where nginx should listening.
  3. uncomment (remove the # symbol) at sub vcl_recv section
  4. add one } symbol at the end of that section

make sure it looks similar to this

2015-07-19_235715

Save changes and exit the editor (Control+O then Control+X)

That’s it for now. Coming up in the Part 2 of this tutorial: Stage 5 and Stage 6 (Vesta CP configuration).

Next: VestaCP Nginx Configuration for Varnish (Part 2: Stage 5 and so on)

7 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.