LAMP Tutorials (5/6): Add a Front-End Apache Cloud Load Balancer

This post is the fifth chapter of a LAMP Tutorial series in which we will show you how to set up a fully redundant, high-availability LAMP stack in the cloud with ElasticHosts. Find the other parts here:

Add a Front-End Apache Cloud Load Balancer

The next step in our tutorial is adding load balancing. This means setting up a new server in front of our two web servers. This load balancing server will monitor both web servers, and switch traffic between them intelligently.

Load balancing setup on ElasticHosts has special requirements because our system does not allow floating Mac addresses. This makes using VRRP (Virtual Router Redundancy Protocol), which is the basis for many load balancers, problematic. We use the Apache module mod_proxy_balancer. This turns Apache into a load balancer, retrieving requested pages from two or more backend web servers and delivering them to the user’s computer. If required, mod_proxy_balancer can keep track of sessions, so a single user always deals with the same backend web server.

The steps are as follows:

  • Step 1: Set up a new server
  • Step 2: Install Apache and mod_proxy_balancer
  • Step 3: Configure mod_proxy_balancer

Step 1: Set up a new server

First we create a fifth Ubuntu 10.04 server, which will be our load balancer. We’ll call this new server herring. Then set herring up to share a private VLAN with our web and database servers, as described in our How To Set Up a VLAN tutorial.

In this tutorial, we’ve given herring the IP address In addition, we will need an external static IP: add this as described in our Add a Static IP Address tutorial. In this tutorial, we’ve given herring the static IP address

Note: In the next tutorial, you’ll be setting up a second load balancer with another static IP address. These should be within the same class C range, so you may find it helpful to set up two static IP addresses now, and check that they are within the same A.B.C. range.

Just to recap, we currently have five servers in total:

  • puffin: Web server 1, private IP address
  • albatross: Web server 2, private IP address
  • beagle: Database server 1, private IP address
  • crocodile: Database server 2, private IP address
  • herring: Load balancer 1, private IP address and static IP address

In a real system, at this point, you might want to configure your DNS servers to give each of your servers with a subdomain (e.g.,, etc) in order to check their status easily.

Step 2: Install Apache and mod_proxy_balancer

First, on our new load balancing server, we need to install Apache and enable the proxy modules:

$ apt-get update
$ apt-get install apache2
$ a2enmod proxy
$ a2enmod proxy_balancer
$ a2enmod proxy_http
$ /etc/init.d/apache2 force-reload

Step 3: Configure mod_proxy_balancer

Now, we need to configure our cluster, in a new file in conf.d:

$ vi /etc/apache2/conf.d/proxy-balancer


ProxyPass / balancer://mycluster

Then we need to configure our proxy to allow access from all hosts:

$ vi /etc/apache2/mods-enabled/proxy.conf

Change the line “Deny from all” in this file to “Allow from all”.

Then restart Apache:

$ /etc/init.d/apache2 restart

Now, when you visit in a web browser, you should see our site. You should also be able to stop either one of the web servers or one of the database servers without the site stopping.

You can find more information on configuring mod_proxy_balancer – including balancing methods, sticky sessions and dynamic updates to load balancing at the Apache documentation.

You're finished

In the final tutorial, we will [Add a Second HA Cloud Load Balancer](/blog/add-a-second-ha-cloud-load-balancer/) to our servers.