Simple HA for Apache on Linux

I have a very simple high-availability setup for Apache on Linux.  This setup works really well for small sites running of VMware or other virtualization setup.
First install two exactly same Linux VM.  This example uses CentOS.  You can setup one VM, and copy/clone it to create a second VM.
Log in to each host and make sure they have proper host names and FQDN.  Here is an example of /etc/hosts, you may have to edit /etc/sysconfig/network file as well.

192.168.10.135           www1  www1.example.com
192.168.10.128           www2  www2.example.com

Setup password less login between each node.

ssh-keygen -t dsa
cat .ssh/id_dsa.pub | ssh [email protected] 'mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys'

The next steps are to be performed on each node, unless noted.  We have to add a repository to install some clustering packages.  Add ‘clusterlabs’ repo to the yum in: /etc/yum.repos.d/clusterlabs.repo

[clusterlabs]
name=High Availability/Clustering server technologies (epel-5)
baseurl=http://www.clusterlabs.org/rpm/epel-5
type=rpm-md
gpgcheck=0
enabled=1

Now, install the following packages.  This will take some time to complete.

yum -y intall httpd*
yum -y install glibc*
yum -y install gcc*
yum -y install lib*
yum -y install flex*
yum -y install net-snmp*
yum -y install OpenIPMI*
yum -y install python-devel
yum -y install perl*
yum -y install openhpi*
yum -y install cluster-glue*
yum -y install heartbeat*
yum -y install resource-agents-1.0.4-1.1.el5.x86_64
yum -y install resource-agents-debuginfo-1.0.4-1.1.el5.x86_64
##  If all goes well then we copy the ha conf files
cp `rpm -q heartbeat -d | awk -F/ '{print $1"/"$2"/"$3"/"$4"/"$5}'| head -1`/authkeys /etc/ha.d/
cp `rpm -q heartbeat -d | awk -F/ '{print $1"/"$2"/"$3"/"$4"/"$5}'| head -1`/ha.cf /etc/ha.d/
cp `rpm -q heartbeat -d | awk -F/ '{print $1"/"$2"/"$3"/"$4"/"$5}'| head -1`/haresources /etc/ha.d/
#There may be a better way of printing all this $ fields in awk!!

Once all the packages have been installed and files copied over, we now have to edit the three conf files.  Edit authkeys, just un-comment these lines.  You can pick either authentication, but sha and md5 are preferred.  For sha1, the file should look like this:

auth 2
#1 crc
2 sha1 Testing123
#3 md5 Hello!

Once done, change access permission on authkeys.  This file should not be readable by anyone for security purposes.

chmod 600 authkeys

Edit the ha.cf file, un-comment following lines:

logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 15
warntime 10
initdead 120
udpport 694
bcast eth0
auto_failback on
node www1.example.com # make user uname -n shows FQDN
node www1.example.com # if it just shows hostname, then put hostname!

We now have to make some changes to the httpd.conf file in each node.  Find and change this line:

Listen 80
to
Listen 192.168.10.222:80  # Setup any IP here, make sure no other host or interface is using it

If you start the httpd, it will fail.

service httpd start

This is normal, because Apache is looking for 192.168.10.222 on local interface(s).  We also have to make sure that Apache starts on reboots.

chkconfig httpd on

We will stick to some more default settings for now.  The default “DocumentRoot” for Apache is is /var/www/html.
Lets create a simple index.html page in /var/www/html in each node.  Here is sample page for first node:

<HTML>
<BODY>
<HEAD>
<TITLE>Sample Page</TITLE>
</HEAD>
<BODY BGCOLOR=”BLUE”>.<CENTER>.<H1>Web Page on node1</H1>
</BODY>
</HTML>

Copy the above page to node2, change “Web Page on node1” to “Web Page on node2”.  All set now, we just have to start the heartbeat service on each node.  You may have to start and stop it couple times.

/etc/init.d/heartbeat start

Now enter the IP address, 192.168.10.222,  in your browser.  If all goes well, you should see a blue page from node1.  Shutdown heartbeat on node1 and refresh the page in browser.  This time you will see a page served from second node.

🙂

Leave a comment

Your comment