Recommended VPS Complete list of best VPS hosting providers.

Load Balancing Server For Dummies: Tutorial Part 3

This post is the third part of “Load Balancing Server For Dummies” series. So if you interested, please catch up from the very beginning.

F. Setting Up MySQL Server

I’m sure the script you use will need at least one MySQL server, otherwise, simply ignore this part.

MySQL server is a place where to store all your site’s database. This does not have to be dedicated vps as you can also host your site’s database in the same server where all your site’s files are hosted. But you’ll then need to setup MySQL’s master and slave connection which is a bit confusing if you don’t familiar handling and setting up such thing.  In the other hand, putting/hosting your database in a separate server (dedicate a VPS specially to handle database) will surely reduce your main server’s load.

Remember the tips to select a VPS for this job. It must be:

  • A reliable VPS with great uptime
  • Located close enough to your hosting servers
  • If you can, pick an SSD-powered VPS with great I/O

Several VPS providers like BuyVM (for $1), RaidLogic ($2), and FlipHost (free) provide offloaded SQL server powered with SSD. In that case you don’t have to setup/build MYSQL server, otherwise you can follow my previous guide on how to install MySQL.

Once you have a MySQL server, now follow this steps to enable MySQL remote access from your web hosting servers.

Step 1 – Note down all IPs of your web hosting servers.

Step 2 – Enable remote mysql access using this command:

## login to mysql as root
mysql -u root -p

## once logged in, issue this command
GRANT ALL ON dbname.* TO dbuser@'' IDENTIFIED BY 'dbupassword';


  1. dbname to your database actual database name
  2. dbuser = MySQL username other than root
  3. = IP of your actual hosting server
  4. dbupassword = define password for new MYSQL user (point 2)

You’ll need to execute that command few times (once for each web hosting server). Just replace the IP address.

Step 3 – Once done, you can now upload your site’s database there.

Step 4 – Now login back to your web hosting servers and change/adjust your site’s setting to point to that MySQL server. For example in WordPress you’ll need to edit wp-config.php file and change database host from localhost to your MySQL’s server IP.

Step 5 – Do not forget to give it a test.

G. Setting Up Varnish Cache

Now you can leave all of your servers minimized but keep this one open: a vps dedicated to load balance your traffic a.k.a the one where Varnish Cache will be installed on.

Now login to that server and follow these steps:

Step 1 – Install Varnish following this guide. The method of caching (RAM/Disk) and how much of cache storage size to allocate is up to you. Do not touch/edit Varnish default.vcl file yet.

Step 2 – Now copy default.vcl file to make backup

cd /etc/varnish
cp default.vcl default.vcl.bak

Step 3 – Then edit that file using your favorite editor like Nano

nano default.vcl

Step 4 – Next, you can erase all of it content and replace with these lines:

# Define the list of backends (web servers).
# Port 80 Backend Servers
backend web1 { .host = ""; .probe = { .url = "/index.php"; .interval = 5s; .timeout = 1s; .window = 5;.threshold = 3; }}
backend web2 { .host = ""; .probe = { .url = "/index.php"; .interval = 5s; .timeout = 1s; .window = 5;.threshold = 3; }}

# Port 443 Backend Servers for SSL
backend web1_ssl { .host = ""; .port = "443"; .probe = { .url = "/index.php"; .interval = 5s; .timeout = 1 s; .window = 5;.threshold = 3; }}
backend web2_ssl { .host = ""; .port = "443"; .probe = { .url = "/index.php"; .interval = 5s; .timeout = 1 s; .window = 5;.threshold = 3; }}

# Define the director that determines how to distribute incoming requests.
director default_director round-robin {
  { .backend = web1; }
  { .backend = web2; }

director ssl_director round-robin {
  { .backend = web1_ssl; }
  { .backend = web2_ssl; }

# Respond to incoming requests.
sub vcl_recv {
  # Set the director to cycle between web servers.
  if (server.port == 443) {
    set req.backend = ssl_director;
  else {
   set req.backend = default_director;

Step 5 – Save and exit the editor (Control+O then Control+X). Once done, you can restart varnish:

service varnish restart

That’s it. Default.vcl file we use above will:

Have two backends, you can add more (web3, web4, webn, …)

Have probing feature that checks for backend’s healthy status. It means Varnish will check whether your server is accessible/up or not, and how fast its respond is. Varnish will then distribute traffic to the fastest responding server first. Basic explanation about the probe option:

What URL should varnish request.

How often should we poll

What is the timeout of the probe

Varnish will maintain a sliding window of the results. Here the window has five checks.

How many of the .window last polls must be good for the backend to be declared healthy.

How many of the of the probes a good when Varnish starts – defaults to the same amount as the threshold.

Of course you can change value of each option.

However, the VCL above only define needed backends where Varnish will fetch the content from and deliver the traffic to. Varnish will then use default settings except if you define another. You can add few other lines of configuration like excluding login page, defining what files should be cached or not, excluding cookies, defining grace time, custom error message, etc.  Need ready to use .vcl template? Check this one on GitHub.

In certain and rare situation, it is possible for the entire backends to go “down” due to any number of causes like a programming error, a database connection failure, or just plain excessive amounts of traffic. In such scenario, Varnish will continue to serve up the outdated cached copies it has until Apache becomes available again even the cache is expired. Thanks to “Grace” which is a setting that allows Varnish to serve up cached copies of the page even after the expiration period if Apache is down – usually Varnish automatically discards outdated content and retrieves a fresh copy when it hits the expiration time. To enable this feature, you just need to specify the setting in vcl_recv and in vcl_fetch:

# Respond to incoming requests.
sub vcl_recv {
  # Allow the backend to serve up stale content if it is responding slowly.
  set req.grace = 6h;

# Code determining what to do when serving items from the Apache servers.
sub vcl_fetch {
  # Allow items to be stale if needed.
  set beresp.grace = 6h;

Of course you can change its value from 6h to any number you want but the setting in vcl_fetch must be longer than the setting in vcl_recv.

H. Pointing Domain to Load Balancing Server

Instead of using your own DNS server, it is cheaper to use third-party DNS server. But keep in mind to use only the most reliable one and if you can, pick the one has round robin feature. Need recommendation? Go with CloudFlare or CloudNS (more free DNS hosting).

Next, add A record for your domain pointing to IP addresses of vps-a, vps-b, vps-c, and so on.

Again, sorry for not putting screenshot pics. I just put together what I can remember about setting up a load-balancing server. But you may ask me in which part of the tutorial you feel it still unclear enough. Good luck.

Article Index:

Part 1 – Introduction, Low End Setup, Requirements and Basic Knowledge

Part 2 – Setup Web The Server (Apache, PHP, rSync, etc)

Part 3 – Setup MySQL server, Varnish Cache, Pointing Domain


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.