Configure Apache Web Server Load Balancing

This is a tutorial to configure Apache Web Server Load Balancing in Linux using the mod_proxy_balancer module. So additional software isn’t required. This tutorial is written for Linux but this can also be applied to windows systems running Apache. This setup makes use of four computers. The first computer is the proxy load balancer, the second and third computers are the normal web servers which will respond to the requests and the fourth computer hosts the web content that is to be served by the web servers. The reason for separating the content is to make sure the same content is served by both the web servers. The fourth computer is optional, you can use tools like rsync to keep the content of both web servers synchronized, but that add up to network traffic. The number of web servers can be increased as per requirement.

Configure proper IP address settings on all the systems. Except the load balancer other systems have only eth0. After saving the files restart the network service.

service network restart ifconfig

the ifconfig command will display the IP address of each adapter. After configuration ping each other systems’ 10.0.0.X (replace X with the appropriate number) IP address to see whether they are accessible.

Configure mod_proxy_balancer in Load Balancer

On the load balancer system open the Apache configuration file

/etc/httpd/conf/httpd.conf

Make sure the following lines are uncommented so that the proper modules load

Save the file and run a configtest to check if there are any syntax errors

service httpd configtest

If there you see Syntax OK restart the service

service httpd restart

Configure NFS on storage server

By default NFS is installed while installing Linux. So you just have to configure it. Create a directory that will host the web content.

mkdir /docroot

Configure the /etc/exports file to share the docroot folder. Add the following line in the /etc/exports file

/docroot 10.0.0.*(rw,sync)

You can choose whether to allow other systems to have write access on this folder. If you the root user of the other systems have to write to this location use the option no_root_squash.

Configure firewall rules and SELinux policies on all systems

If you’re not comfortable editing iptables through the command line use

system-config-firewall-tui

to allow port 80 on the load balancer and web servers. Others use the following command for load balancer

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

Web servers

iptables -I INPUT -p tcp --dport 80 -i eth0 -j ACCEPT

Storage Server

iptables -I INPUT -p tcp --dport 2049 -i eth0 -j ACCEPT

All systems

iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -j REJECT

Save the rules on all systems

service iptables save

On the Web Servers (NOT load balancer) configure SELinux booleans

setsebool -P httpd_use_nfs 1

This will allow Apache to read from the NFS directories

Mount the NFS share in the web servers as the DocumentRoot

On the web servers add the following line in the /etc/fstab file to mount NFS share

10.0.0.4:/docroot /var/www nfs soft,nfsvers=4 0 0

Here 10.0.0.4 is the IP address of the NFS server. After saving the file remount all mount points

mount -a

Now in the NFS server create the folders www and cgi-bin

mkdir /docroot/{www,cgi-bin}

Create a welcome screen inside the www folder

echo "<h1>Welcome to our cluster</h1>" > /docroot/www/index.html

From a computer connected to the same network as the load balancer (192.168.0.0/24) open the browser and type the IP address of the load balancer

http://192.168.0.2/

If everything was successfully configured you should see the welcome page. Now to check from which web server is serving your requests you need to create a CGI script which displays the hostname of the server. On the NFS server create a file in the /docroot/cgi-bin directory and add the content

Save the file and point the browser of the computer which you used to test earlier

http://192.168.0.2/cgi-bin/script.cgi

When you keep refreshing this page you should see different hostnames being displayed. Take a look at the screenshot below

Refreshing the browser displays two difeerent hostnames

Now its time to check Failover clustering. To test this go to either of the web servers and bring the network interface down

ifdown eth0

Go to the browser and refresh it it will take a few seconds then it will display the hostname of only the server whose network interface is up. All this while did you notice that we keep seeing the hostnames of the web servers and not the load balancer or the storage server. This is because the load balancer is merely a proxy forwarding requests to the web servers and the storage server just hosts the CGI script so the actual execution of the secript takes place on the web servers.

Some things to take note when using clustering

If you plan to use a server side scripting language such as PHP the session variables are stored in the web server itself so this poses a problem as a session variable might not be same on all pages. To prevent this you can place the session save path on the storage server it self and mount it on /var/lib/php/sessions/

Another problem you might face is with the upload temp dir. The default is usually the /tmp directory. By editing the /etc/php.ini file you can change it to something else and mount that directory to the storage server.

The Apache load balancer has three algorithms, to know more about these refer to the Apache documentation at http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html. To use a specific algorithm open the /etc/httpd/conf/httpd.conf file in the Load Balancer and add the following line in between <Proxy> directive