Skip to content

Setup Apache Load Balancer using mod_proxy_balancer

We will learn how to setup Apache Load Balancer using mod_proxy_balancer module. This tutorial is written for Linux but this can also be applied to windows systems running Apache. We will use four servers for the Load Balancer setup. The first server will be the proxy load balancer, the second and third servers will be the web servers which will respond to the requests and the fourth server will be the storage server containing 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 number of web servers can be increased as per requirement.

The entire setup is shown in the diagram below (eth0)
Load Balancer
——————— (eth1)
|                            |                         |      
——————-    ——————-    ———————
Web Server 1     Web Server 2       Storage Server
——————–    ——————-    ———————-

The settings for each server is specified below

Load Balancer
eth0 -
eth1 -

Web Server 1
eth0 -

Web Server 2
eth0 -

Storage Server
eth0 -

The steps for setting up an Apache Load Balancing Cluster are as follows

Install Apache HTTP Server on load balancer, Web Server 1 and Web Server 2

We will use the yum command to install Apache in the load balancer and Web Servers

yum -y install httpd

Make sure the service starts at boot time using chkconfig

chkconfig httpd on

Start the httpd service

service httpd start

Check is it works by typing http://localhost in the respective systems. If there is no browser installed use wget

wget http://localhost/

The Apache test page should be downloaded. At this step you cannot access other systems’ web server because firewall rules are not yet configured.

Configure network settings on all systems

The Load Balancer should have two network adapters. Configure the network adapters to use the correct IP address by editing files


The template of the file (for load balancer) is provided below


For other three computers set the gateway as


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

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


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

LoadModule proxy_module  modules/
LoadModule proxy_balancer_module modules/
LoadModule proxy_http_module modules/

Add the following lines to the end of the httpd.conf file

echo "ProxyRequests Off" >> /etc/httpd/conf/httpd.conf
echo "<Proxy balancer://mycluster>" >> /etc/httpd/conf/httpd.conf
echo "BalancerMember" >> /etc/httpd/conf/httpd.conf
echo "BalancerMember" >> /etc/httpd/conf/httpd.conf
echo "</Proxy>" >> /etc/httpd/conf/httpd.conf

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 on all systems

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


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


Make sure to disable Selinux on all the systems before the setup using the steps given below.

1. Edit the file /etc/selinux/config using your favorite editor

2. Change the line SELINUX=permissive to SELINUX=disabled

3. Save the file

4. Reboot the server

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 /var/www nfs soft,nfsvers=4 0 0

Here 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 ( open the browser and type the IP address of the load balancer

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

touch /docroot/cgi-bin/script.cgi
chmod o+x /docroot/cgi-bin/script.cgi
echo -e "Content-Type: text/plain\n"

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

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 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

ProxySet lblmethod=method-name


ProxySet lblmethod=bytraffic

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

x  Powerful Protection for WordPress, from Shield Security
This Site Is Protected By
Shield Security