In this guide, we will discuss how to install HAProxy and how to load balancing between two web servers.

We will use three instances. First one will be load balancing instance and others will be web servers. We will launch three instances from SkyAtlas panel.


Instance 1 – load balancing

Public IP address : x.x.x.x

Private IP address :


Instance 2 – Web server

Private IP address :


Instance 3 – Web server

Private IP address :

First we connect to our load balancer server via ssh and install haproxy packages.

haproxyinstance:~$ sudo apt-get install haproxy

After installation is completed, we will connect our web instances via ssh and we will install Apache2 packages.

webinstance1:~$ sudo apt-get install apache2
webinstance2:~$ sudo apt-get install apache2

After web servers' installation is completed, we will re-connect our load balancing instance for doing load balancing settings. HAProxy configuration file will we located in :


We will edit this file using file editor, i will use vim editor, and i will add a few lines end of the configuration files.

HAProxy configuration file looks like :

 log /dev/log local0
 log /dev/log local1 notice
 chroot /var/lib/haproxy
 stats socket /run/haproxy/admin.sock mode 660 level admin
 stats timeout 30s
 user haproxy
 group haproxy
# Default SSL material locations
 ca-base /etc/ssl/certs
 crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
 # For more information, see ciphers(1SSL). This list is from:
 ssl-default-bind-options no-sslv3
 log global
 mode http
 option httplog
 option dontlognull
 timeout connect 5000
 timeout client 50000
 timeout server 50000
 errorfile 400 /etc/haproxy/errors/400.http
 errorfile 403 /etc/haproxy/errors/403.http
 errorfile 408 /etc/haproxy/errors/408.http
 errorfile 500 /etc/haproxy/errors/500.http
 errorfile 502 /etc/haproxy/errors/502.http
 errorfile 503 /etc/haproxy/errors/503.http
 errorfile 504 /etc/haproxy/errors/504.http


Here we need to specify on which servers we want to use load balancer. For this we need to add following lines to the end of configurations file.

listen firstbalance
 bind *:80
 balance roundrobin
 option forwardfor
 option httpchk
 server webinstance1 check
 server webinstance2 check

We have stated that we will do balancing with the roundrobin algorithm. Since Apache2 web server runs on port 80, we listen port 80 and we distribute incoming requests that arrives to this port. At the bottom, we specify the private IP addresses of our servers to load balancer.After this, we need to restart the haproxy service.

haproxyinstance:~$ sudo service haproxy restart

We need to change the Default Apache page on our web servers to know which server we are redirected to. We need to connect with ssh to our web servers. After connection, we delete the /var/www/htlm/index.html file and we create a new index.html file.

webinstance1~$: cd /var/www/html
webinstance1~$: sudo rm index.html
webinstance1~$: vim index.html

 We write a simple html code in to this file and we save it. After that we will restart the apache2 sevice.


<header><title>Web Instance 1</title></header>
<body>Web instance 1</body>
webinstance1~$: sudo service apache2 restart

We do the same on other web server.

webinstance2~$: cd /var/www/html
webinstance2~$: sudo rm index.html
webinstance2~$: vim index.html
<header><title>Web Instance 2</title></header>
<body>Web instance 2</body>
webinstance2~$: sudo service apache2 restart 

After these processes, we will write our load balancers IP address to URL bar of web browser to access the web servers.We will see that we have access to two servers by refreshing page. We can test it with the curl command through the terminal.

<header><title>This is web instance2</title></header>
Web Instance 2

<header><title>This is web instance1</title></header>
Web Instance 1

As you can see, the load balancing instance distribute our requests to web servers.