Web Server Load-Balancing on Ubuntu with HAProxy

by September 1, 2015 0 comments

HAProxy is an open-source load-balancer which can load balance any TCP service. HAProxy is a free, very fast and reliable solution that offers load-balancing, high-availability, and proxying for TCP and HTTP-based applications

STEP 1: Here, we are installing HAProxy (High Availability Proxy) in Ubuntu by using the below commands:
apt-get install haproxy
haproxy -v
STEP 2: Start HAProxy by using the init script /etc/default/haproxy. Set ENABLED option to 1 as:
STEP 3: HAProxy is now installed. Now install 2(two) Apache Web Server in other machines. Here, we are using three systems virtually through Virtual Box.
Virtual machine 1 – Load Balancer
Hostname: haproxy
OS: Ubuntu
Private IP:
Virtual machine 2 – Web Server 1
Hostname: webser01
OS: Ubuntu with LAMP
Private IP:
Virtual machine 3- Web Server 2
Hostname: webserver02
OS: Ubuntu with LAMP
Private IP:
STEP 4: Before configuring HAProxy, take a backup of the original file by renaming it:
mv /etc/haproxy/haproxy.cfg{,.original}
Here, we have created haproxy.cfg file. Create nano /etc/haproxy/haproxy.cfg file as:
log /dev/log local0
log local1 notice
maxconn 4096
user haproxy
group haproxy
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webfarm
mode http
stats enable
stats uri /haproxy?stats
balance roundrobin
option httpclose
option forwardfor
server webserver01 check
server webserver02 check
STEP 5: Start the HAProxy service using below command:
sudo service haproxy start
STEP 6: Testing Load-Balancing and Fail-over:
Edit the server name in both the default index.html files located by default at /var/www/index.html on both web servers.

On the Web Server 1 (webserver01 with IP-, append below line as:
sudo sh -c “echo \<h1\>Hostname: webserver01 \(\)\<\/h1\> >> /var/www/index.html”
On the Web Server 2 (webserver02 with IP-, append below line as:
sudo sh -c “echo \<h1\>Hostname: webserver02 \(\)\<\/h1\> >> /var/www/index.html”
STEP 7: Now, open your browser on your local machine and type haproxy installed machine IP i.e. Every time when you refresh the tab, the load is being distributed to each web server.


When we refresh the page for the second time, we get the following interface.


No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.