Load-balancing LAMP Dedicated Web Cluster Server Setup
November 24th, 2008Load-balancing dedicated server Hosting solution and cheap pricing are two important concepts of a budget-corporate client. LAMP ( Linux, Apache, MySQL, and PHP/Perl/Python ) Load-balancing is a solution which can help moving database web server to secondary server. LAMP is not the same as standard cluster setup. Also, it does not include high-availability features such as fail-over. It shared load and distrbiutes to another server which acts as secondary server which is known to be more cost effective.
Applications and softwares required to setup LAMP clusters are packaged along with linux distributions. Following is an example where two servers run DNS which is the primary server and backup. This is ditributes between 3 web servers and 2 database servers.
The initial stage includes round-robin where DNS is a load-balancing solution which serves web requests for a hostname from different dedicated web servers. In this case, each web server has it’s own Public IP address.
Following is an example where the domain assignd the same hostname to each of three dedicated web servers but the IP addresses are completely different :
;
; Domain database for foo.com
;
domain.com. IN SOA ns1.domain.com. hostmaster.domain.com. (
2006032801 ; serial
10800 ; refresh
3600 ; retry
86400 ; expire
86400 ; default_ttl
)
;
; Name servers
;
domain.com. IN NS ns1.domain.com.
domain.com. IN NS ns2.domain.com.
;
; Web servers
;
www IN A 10.10.10.11
www IN A 10.10.10.12
www IN A 10.10.10.13
In DNS Server received requests from domain.com, one IP address will return for the first time, then a different IP address for the next request. In this case, Web server traffic is distributed among 3 web servers. However, due to DNS cache, resources may vary. This is just an solution to minimize load-balancing setup cost.
Web Server Configurations that is used in a cluster is the same as Apache Web Server Configuration with only one statement that content is the same with sycnhronization. Many use the option which is known as “rsync”.
We suggest you also create a new user account on each dedicated web server and it needs to have write permissions for Web content directory on each web server. Also, create SSH keys for the account and distribue the public keys to /home/syncer/.ssh directory on other 2 web servers. It also allows login without password to the user account and update data at each intervals.
The following rsync updates web content :
#!/bin/bash
rsync -r -a -v -e “ssh -l syncer” –delete /var/www/ webtwo:/var/www/
rsync -r -a -v -e “ssh -l syncer” –delete /var/www/ webthree:/var/www/
When a LAMP Cluster is setup, cookies needs to be checked as Apache stored cookies in /tmp directory. In case, a visitor views a session on Web Servers and if HTTP requests are managed by a different web server, the cookie won’t exist and it won’t function as required. Solution to this is shared cookie directory on Web Servers and should be done before setting up LAMP Clusters.
Another requirement of the setup is to send the data to the database master server and should be distributed between master and slave server.
Now, if we focus on Database servers, MySQL has a feature to maintain database on different servers. It is known as “log replay” which means a log is created on the master server which is read by a slave server and then applied to the database.
In this example, we will assign 2 database servers, one is Database Server 1 and Database Server 2.
In order to setup Master database server, you will need to create a replication account which is the user ID in MySQL which is utilized by slave servers which read the logs.
Following is an example :
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO copyslave@”10.10.10.0/255.255.255.0″ IDENTIFIED BY ‘copypass’;
You can also edit MySQL configuration which is located in /etc/my.cnf and then add the following :
# Replication Master Server (default)
# binary logging is required for replication
log-bin ( binary log file - required for applications )
# required unique id
server-id = 1 ( Master Server )
You can view new binary log file in MySQL directory with $HOSTNAME-bin.001. Here, MySQL will create new log files. In order to setup Slave Server, edit /etc/my.cnf and add the following :
# required unique id
server-id = 2
#
# The replication master for this slave - required
# (Master Database Web Server IP)
master-host = 10.1.1.21
#
# Slave Username
# to the master - required
master-user = copy
# Slave Password
# the master - required
master-password = copypass
# Lost Connection Check
master-connect-retry = 15
# binary logs
log-bin
Restart MySQL, slave server will connect to the master server and begin the replication process. At initial stage, it will create master.info file with all settings in the default directory which is /var/lib/mysql
In order to check if the replication is working, log in to the MySQL monitor and run show master status and then show slave status. There you need to check Slave_IO_Running and Slave_MySQL_Running. If both are Yes, then the replication process is working.
In case, the database web server loosed network connectivity, you can stop MySQL on the master as well as slave server, then dump master database and reload the database on the slave server and then start MySQL on master and slave server.
If there are any issues with the master database server, the slave database server can be configured as master database server by simply updating the IP address and MySQL configuration file. It is possible to easily setup LAMP cluster on dedicated web server hosting



















