Load Balancing Server For Dummies: Tutorial Part 2
| |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:
cd /var/www/servermom.org/public_html wget http://wordpress.org/latest.zip unzip latest.zip cd wordpress 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' uservpsa@xxx.xxx.xxx.xx1:/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' servermo@192.xxx.xxx.xxx:/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:
crontab -e
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.
Article Index:
Part 1 – Introduction, Low End Setup, Requirements and Basic Knowledge
first ; the new comments and new posts don’t show to us cause the aggressive caching system you use ..
for example i accessed this post cause i subscribe for your mail list .. but the link for it not exist on home page ..
i use this command you told about :
rsync -avzhe
but after transfer files each time . it require to change the owner of the files with this command
chown -R user:user folder
cause the user of server one is diffident from server two ..
so we can use
rsync -aogvzhe
i added “o” and “g” to not change the owner and groups for folders and files
so no need to use chown after that..
and also if we need to sync the deleting files from server to another we can add –delete
so it will like this
rsync -aogvzhe –delete
but i have a question
when we use rsync we login to remote server via root user .. but what if we Not premit root user to login directly from login to ssh and use another user to login to ssh and then we use su to change account to root! as you explain here :
http://servermom.wpengine.com/basic-ubuntu-setup-before-building-a-working-server/85/
how we can handle this with rSync
Nice and thanks for your suggestion 🙂 great thanks.
This blog is mainly hosted at CrocWeb (shared hosting) so I don’t have access to root and yes rsync is still able to work.
Regarding aggressive caching, we can define the rule at .vcl file of Varnish.
you “Sawiyati” are the Virtual Mom for all new servers admins “babies ” as you like to say 😀
Can’t access Part 1:
http://servermom.wpengine.com/load-balancing-guide/1411/
Thanks for time in putting this series!
I opened that page again and it’s fine. Try pressing Control+F5 to force refresh your browser. I’m currently aware that few pages can not be opened directly still looking to it. thx
If i may ask. why did you add “/* */”
in the code above
rsync -avzhe ‘ssh -p 22’ –exclude ‘wp-config.php’ servermo@192.xxx.xxx.xxx
/* */
:/home/servermo/public_html/ /var/www/public_html/servermom.org/
I tried to make a mirror copy of my public_html files from cpanel of one of my domain to a new vps but some how I am gettit error /script directory not found ? can you tell me as where am i getting it wrong . I have made all the changes in the code to meet my needs as mentioned above . So can you tell me as how to make a cpanel public_html folder copy to a new server / vps using vestacp ? also if possible can you make an additional tutorial about rsync only if possible ??
I am getting this error while running the code to copy public_html files from cpanel to another vps –
bash: /script: No such file or directory
you should firstly create your web root directory in your vps (e.g: /var/www/public_html)
There is a concept issue I am not understanding. You said that VPS A would be the main server… but when you’re load balancing there is no way for the person inputing information in to know if they are on server A B or C… Thus making this Rsync method useless. Am I wrong about that?
Hi Sawiyati,
My website use Nginx, MySQL, Redis and Solr. With $200/month, What do server I need to buy? I know 1 Varnish, 2 Nginx, 1 MySQL (like your post). Please help me to setup for Redis and Solr server.
Thank you very much.
Hello!
I’m from Brazil and I follow your site often.
I have two VPS with VestaCP and another with CentosWP. I understand I would have to have another VPS to function properly. That’s right?
Success!
PS – I am using Google translator