This is the part two of my “Load Balancing Server For Dummies” tutorial. Previously, I’ve covered some basic knowledge you may need to know about what load balancing is and why you may need to setup one for your very favorite / popular website.
As a reminder, the purpose of this guide is to build a simple, cheap but working load balancing server. So we’ll gonna use low end boxes (few affordable virtual private servers).
E. Setting Up Web Server
Step 1 – Install all necessary software you may need to host your website’s files. You can use either Apache, Nginx, Lighttpd or a bundle control panel software like zPanel or VestaCP. I don’t think I have to re-explain how to do that again. Please refer to this page for Ubuntu and this page for CentOS.
Step 2 – Make sure you did the step 1 above on all your VPS that serve as web hosting. In this example I use 2 (two) vps boxes to host my files (1 from GreenValueHost and 1 from WeLoveServer) and both installed Apache + PHP 5.3.
For your information, the setup of each VPS does not need to be the same. For example, you can install zpanel in the first vps, other panel in second vps, or simply Apache + PHP in the next vps. The key point here is make sure you know its web root path (like /var/www/domain.com/public_html, or /var/zpanel/zadmin/hostdata/domain_tld/public_html/, etc).
Step 3 – Now upload all your website’s files to one of those VPS or simply upload it to the VPS you think the most great one (in terms of reliability, uptime, and speed). In this example I’m going to install WordPress so this what I did once logged in to my first vps:
mv * .[^.]* ..
Step 4 – We’re also going to setup passwordless SSH login so each VPS can communicate between each other without having to enter password. This is needed for rsync purpose (the task to sync all files between all VPS boxes). Lets say you have 3 boxes of low end vps which are VPS-A, VPS-B and VPS-C. The VPS-C must have ability to access VPS-B. VPS-B must have access to VPS-A. Read how to setup password-less SSH login.
Step 5 – Install rSync app on VPS-B and VPS-C.
## Debian or Ubuntu
apt-get install rsync
## Fedora or CentOS
yum install rsync
Q: Why don’t you install rSync on VPS-A?
A: Good question. In this example we’ll use VPS-A as the main server to host all website’s files. This is the VPS where you will upload your files to while other (VPS-B and C) will sync the files from that main VPS.
Step 6 – Once rSync is successfully installed, run rSync for the very first time on VPS-B and VPS-C to firstly copy all your site’s files as well as to test whether rsync is working or not. The command is:
rsync -avzhe 'ssh -p 22' --exclude 'wp-config.php' firstname.lastname@example.org:/path/to/web/directory/ /path/to/web/directory/
- Change 22 to another port if you closed port 22 already.
- Change wp-config.php to another file you wish to exclude, or simply remove the
--exclude 'wp-config.php' part if you have no file to exclude.
- Change uservpsa to username of your VPS-A
- Replace xxx.xxx.xxx.xx1 with the IP of VPS-A
in my example it is:
rsync -avzhe 'ssh -p 22' --exclude 'wp-config.php' email@example.com:/home/servermo/public_html/ /var/www/public_html/servermom.org/
The example above I use to sync a cPanel Shared Hosting with my VPS.
Step 7 – Everything goes as expected? Great! Now it’s time to automate the task. Add new cronjob in VPS-B and VPS-C with the same command. Issue this command to add/edit cron:
It will open default editor as current user. Then add the rsync command above in there. If you are using vi editor, press a button on your keyboard to enter editing mode. Once done adding new line, simply hit Esc key to exit editing mode. Now press Shift key + ZZ to save and exit vi editor.
You can run the cron as frequent as you wish (daily, hourly, or every x minutes). Quick reference about Crontab can be found here.
That’s it for now. Sorry I put no pictures of screenshot. But you may ask me in which part of the tutorial you feel it still unclear enough.
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