Load Balancing with Apache


Load-balanced can be done either by a hardware or a software based load balancer. In this post, I will be using Apache httpd as the software load balancer to distribute load on different PHP nodes.

Set Up:
1. Apache httpd should be installed on all the servers with the support of mod_proxy, mod_proxy_http and mod_proxy_balancer modules.

2. Open httpd-vhosts.conf and add the following configurations inside the VirtualHost block. Follow the complete settings below:

<VirtualHost localhost:1000>
DocumentRoot C:/wamp/www/loadtest
ServerName loadtest
ProxyRequests Off

<Proxy balancer://web-nodes>
BalancerMember http://localhost:1001 route=load1 min=3 max=5 loadfactor=2
BalancerMember http://localhost:1002 route=load2
ProxySet stickysession=NODE
</Proxy>

ProxyPass /balancer-manager !
ProxyPass / balancer://web-nodes

<Location /balancer-manager>
SetHandler balancer-manager
Order deny,allow
Allow from all
</Location>
</VirtualHost>

Save the httpd-vhosts.conf after making the above settings and restart the server. That will complete our task of creating Apache httpd as reverse proxy load balancer server.

Testing PHP Cluster

Now, to test your PHP load-balance setup, create a file index.php in your application nodes load1 and load2. The source code for the file would be very simple and as below:

For PHP application load1

<?php
echo 'Hi, I am from http://localhost:1001';
?>

For PHP application load2

<?php
echo 'Hi, I am from http://localhost:1002';
?>

After creating these files, try to load these files from your load balancer url i.e. http://localhost:1000/ for multiple times. If your server setup is correct, you can view the output as shown in below screenshots:

Data coming from http://localhost:1001

Data coming from http://localhost:1002

To see, how your load balancer is performing and if you have set it up as explained earlier, you can visit the url http://localhost:1000/balancer-manager and see the output as shown below:

Thats all, to add more application nodes to the load balancer, you only have to configure BalancerMember as configured above with its url.

Advertisements

One thought on “Load Balancing with Apache

  1. Hello

    I have used Wamp Server 2.5
    Apache Version : 2.4.9

    When i enabled mod_proxy_balancer , my wamp server’s color is go to Orange. I have waited some time but it was not active means green.

    Could you please assist here ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s