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:

1
2
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:

1
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:

1
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:

1
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

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

to this:

1
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:

1
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)

 

Now let’s continue, shall we?

STAGE 5 – NGINX CONFIGURATION

Step 1 – Go to /etc/nginx/conf.d/ directory and see if there is configuration file of your public IP:

1
2
cd /etc/nginx/conf.d
ls

2015-07-19_230500

Step 2 – Edit that .conf file. Replace xxx.xxx.xxx.xxx with your own actual public IP:

1
nano xxx.xxx.xxx.xxx.conf

You’ll see something like this:

2015-07-19_230820

Step 3 – Now change port :80 to :8082 as example below:

2015-07-19_230921

Save changes and exit the editor which in Nano it is Control+O then Control+X.

Step 4 – Also edit vesta.conf file located at /usr/local/vesta/conf/. Use your favorite text editor or in my situation I use Nano:

1
nano /usr/local/vesta/conf/vesta.conf

change PROXY_PORT from 80 to 8082

2015-07-19_231405

Save changes and exit (Control+O then Control+X).

Step 5 – Edit nginx.conf file for each Vesta CP user located at /home/user/conf/web. This step is quite not efficient if you have several Vesta CP users as you have to edit them all. In my example I will edit nginx.conf file for user admin:

1
nano /home/admin/conf/web/nginx.conf

Again, change port 80 to 8082 at the listen line. See example below:

2015-07-19_232126

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

STAGE 6 – VESTACP FIREWALL CONFIGURATION

Open up your favorite web browser, login to your Vesta CP dashboard as admin then click the Firewall menu on top of the page.

2015-07-19_232534

Edit the /WEB section of the firewall

2015-07-19_232648

Now ad 8082 in the Port field and hit the green Save button.

2015-07-19_232813

STAGE 7 – GIVE IT A TEST

Step 1 – Before you are running for a test drive, you have to firstly restart Nginx and start Varnish Cache server:

1
2
service nginx restart
service varnish start

2015-07-19_233019

Step 2 – Next, you have to setup your domain to point to your server or you can simply edit your local hosts file in your PC.

Useful posts:

Step 3 – Now you can open up your favorite web browser (like Mozilla Firefox or Chrome) and access your website using your domain. Your website should now be displayed.

2015-07-19_233551

STAGE 8 – MORE VARNISH CONFIGURATION

I can see this stage is optional but you may also need this especially if you want to host complex scripts / CMS on production environment. What I’m talking here is tweaking Varnish. What I told you in Part 1 of this article is just editing default.vcl file defining where the backend is, plus there is also necessary config to forward real visitor’s IP addresses so your script and log files will record actual IP and not your server IP. For your information, there are several ready-to-use Varnish .vcl template to use with specific script like WordPress, Joomla or Drupal. Some what I can recommend are:

  1. https://github.com/pkhamre/wp-varnish
  2. https://github.com/mattiasgeniar/varnish-3.0-configuration-templates
  3. https://github.com/JohnMcLear/Wordpress-Varnish-VCL
  4. https://github.com/mattiasgeniar/varnish-4.0-configuration-templates

Feel free to Googling for more or come with your own configuration.

You can make sure if Varnish is really working on the right port (80) by doing curl:

1
curl -I http://domain.com

2015-07-20_004743

or by using web app like isvarnishworking.com:

2015-07-20_004905

if you see the X-Varnish line in the header respond that meaning Varnish is working correctly. With some advanced config in .vcl you can also get cache HIT or Missed status.

 

http://www.servermom.org/varnish-cache-vesta-cp/2564/

 

Leave a Comment

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

Shares