In my previous post, we have setup Ajenti and setup MySql master to master and files replication.

For load balancing two server running with Ajenti, we need to install Varnish in different VPS.

Setup Load Balancing with Varnish

Here, I use Centos in third VPS for install Varnish.

rpm -Uvh  

yum install varnish

Change port 6081 to 80 in config:

nano /etc/sysconfig/varnish

Setup default.vcl:

nano /etc/varnish/default.vcl

Delete and replace all contents with this:

backend ajenti01 {
.host = "";

backend ajenti02 {
.host = "";

director ajenti round-robin {
{ .backend = ajenti01; }
{ .backend = ajenti02; }

sub vcl_recv {
set req.backend = ajenti;
set req.http.X-Forwarded-For = client.ip;
if( req.url ~ "^/wp-(login|admin)" || req.http.Cookie ~ "wordpress_logged_in_" ){
return (pass);

Note: if you hide wp-admin / wp-login with others link, change "^wp-(login|admin)" with your link.

Now, execute varnish to start:

service varnish start

Install CMS Web in Ajenti V Panel

Login to Ajenti from Server 1 and Server 2, and click menu "Website" to create vhost. Here I will install Wordpress, no different configuration for other CMS. For Joomla, rewrite rule below also works.

We have to add vhost in both server with same configuration. After click "Website" Menu, see figures below:

Name it with "loadbalancing" or whatever you name it. Then clik "Create". Then click "Manage".

In General tab, uncheck "Maintenance Mode", and fill name of Directory you want Wordpress to be installed. Then click "Create Directory" and "Set". For button Fix File Permissions you can click after Unzip or Copy of Wordpress installation files.

Click "Domains" tab and fill with your domain. and fill it with without www and with www.

Click "Content" tab and Choose "PHP FastCGI then click Create:

Now click "Advanced tab" for insert Nginx rewrite rules and others:

Insert this codes to Custom Configuration column:

location / {
try_files $uri $uri/ /index.php?$args;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
   access_log off; log_not_found off; expires max;

location = /favicon.ico {
log_not_found off;
access_log off;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
location ~ /\. {
deny all;
location ~* /(?:uploads|files)/.*\.php$ {
deny all;

Now Click to "MySql" tab:

In Server 1: insert name for database and username then click "Create".
In Server 2: Because database will automatically replicates to Server 2 from Server 1, no need to create Mysql, just input name of mysql created from Server 1 and click Attach.

Then, the final touch is Click Save or Apply Change.

Transfer all wordpress files to your directory. Or you can download it from wordpress source.

Insert IP Address of Varnish Server to DNS

The last step is to insert IP Address of Varnish Server to DNS Editor. Here I am using Cloudflare for DNS Editor of my domain:

Add A Type and insert Varnish Server IP Address then click Add Record.

Test Load Balancing

Now run Wordpress installation until finish. For checking Load Balancing, just switch off the power of Server 1 and the Web will still live handled by Server 2. Then, switch off Server 2 and Switch on Server 1, web will handled by Server 1. And switch off both servers, web still alive handled by Varnish Cache.

Now, the all process for load balancing has finished. Good luck!