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:
- A SSH client like Putty and basic knowledge about how to use it.
- Basic knowledge about common Unix command to SSH to your server.
- 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.
- 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.
- About 20 minutes of your time
- 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
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).
More explanation and screenshots:
Stage 2 – VestaCP Basic Configuration
Some tasks you have to firstly proceed are:
- Changing default admin password
- Adjust hosting package configuration
- Add new website on it (DNS entries will be generated automatically via this task)
- Add new database on it (if your website need it)
- 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
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:
Step 3 – Now you can install Varnish using yum:
yum install varnish -y
pic:
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.
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.
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:
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:
- change .host ip from 127.0.0.1 to your server public IP
- change .port 80 to .port 8082 which where nginx should listening.
- uncomment (remove the # symbol) at sub vcl_recv section
- add one } symbol at the end of that section
make sure it looks similar to this
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)
I have try to install varnish on vestacp under centos 6.7 x64 with 512MB Ram. There is a lot of error while installing it related to “Cannot allocate memory”. Is this problem has connection with low Ram? Should I resize my VPS to 1GB Ram?
Vestacp itself needs at least 512MB RAM, you better add more RAM or install Varnish at different VPS.
Ok can I install varnish on a different VPS and use it for the website on a separate VPS.
Like I have 2 VPS, one uses 512mb and the other had 4GB ram. But I want to use varnish on the 512mb ram.
Also I cant find the folder you specified /etc/sysconfig/varnish
THe OS on the server is Ubuntu
Yes definitely. That’s what I do with this blog. Any way, that’s a file not a folder
Sorry Mom, for varnish configuration I think it’s in directory /etc/varnish/varnish.params. I use 1GB RAM VPS. Is there any tools to test the difference between using varnish and not using it? Thanks.
Hai, I have tried to install Vestacp on CentOS 6.8 x32 on digital ocean droplet. It gives error while installation ” yum install failed”. Please help me I am unable to find reason
Hi
And this is just a bunch of runs
nginx +-phpfpm VestaCP?