Configuring load balancing between multiple web servers may seem challenging, but it doesn't have to be. HAProxy is a great lightweight tool that is easy to setup and has a large number of rich features for managing and serving content.
Lets look at a simple scenario using two application or web servers n2 and n3, running on 10.0.0.12 and 10.0.0.13 respectively. Each web server is running apache2, serving a simple html page, but it could be any form of static or rich content.
We use a third server with a minimal install of Debian 6.0 (Squeeze) 64bit, listening on 10.0.0.11 and a publicly accessible IP that we will install HAProxy onto.
root@n1:~# apt-get install haproxy Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: haproxy 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 521 kB of archives. After this operation, 1,118 kB of additional disk space will be used. Get:1 http://ftp.us.debian.org/debian/ squeeze/main haproxy amd64 1.4.8-1 [521 kB] Fetched 521 kB in 0s (541 kB/s) Selecting previously deselected package haproxy. (Reading database ... 21199 files and directories currently installed.) Unpacking haproxy (from .../haproxy_1.4.8-1_amd64.deb) ... Processing triggers for man-db ... Setting up haproxy (1.4.8-1) ... Adding group `haproxy' (GID 105) ... Done. Adding system user `haproxy' (UID 102) ... Adding new user `haproxy' (UID 102) with group `haproxy' ... Not creating home directory `/home/haproxy'. root@n1:~#
You can verify the installed version of HAProxy easily...
root@n1:~# haproxy -v HA-Proxy version 1.4.8 2010/06/16 Copyright 2000-2010 Willy Tarreau
Next lets enable that to start, edit /etc/default/haproxy and change the ENABLED value from 0 to 1. Or use the following snippet.
sed -i 's/ENABLED=0/ENABLED=1/' /etc/default/haproxy
Debian comes with a helpful example configuration. By tweaking that a little the following will work for us...
root@n1:~# cat /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log /dev/log local0
log 127.0.0.dd1 local1 notice
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen web-farm 0.0.0.0:80
cookie SERVERID rewrite
balance roundrobin
server app1 10.0.0.12:80 cookie app1inst1 check inter 2000 rise 2 fall 5
server app2 10.0.0.13:80 cookie app1inst2 check inter 2000 rise 2 fall 5
And now we start that up...
service haproxy start
You should now be able to point your browser at the 'n1' server and see the content from one of the two backend servers.
What else can you do now? Lots of goodness can be gleaned from the main documentation, some ideas include...
- take one or both web servers offline to test what happens when you access HAProxy
- configure HAProxy to serve a custom maintenance page
- configure the web interface so you can visually monitor HAProxy statistics
- change the scheduler to something other than round-robin
- configure prioritization/weights for particular servers
- tweak caching to optimize site load time
- configure cookie pass-through so customers get served predictable content
- setup internal monitoring of HAProxy and HAProxy logs using Monit.


Thanks for this writeup. I did these steps on an Ubuntu 11.04 Server and it worked great.
Thanks for the feedback, glad it was useful for you
Hi excelente tutorial!!
Now, I have some questions...
How recommendable is for small and medium enterprises?
How secure is it?
Can it be used with other software? For example Zentyal
A lot of thanks!
Greetings.
PS.
Sorry for my bad English