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.

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

bashvst-install.sh

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

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

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:

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.

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

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.

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

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:

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.

Edit the /WEB section of the firewall

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

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

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.

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.

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: