How To Install and configure Haproxy Load Balancer On Linux

How To Install and Configure Haproxy Load Balancer On Linux

I will show you the basics of how to install and configure haproxy load balancer for the apache web server. we can also check the config using the command available in haproxy. we will have two apache web server and one load balancer server.

Load Balancer IP: 
Web Server 1:
Web Server 2:

Install the package using below command

yum install haproxy -y
haproxy.x86_64 0:1.5.18-9.el7

Dependency Updated:
openssl.x86_64 1:1.0.2k-19.el7 openssl-libs.x86_64 1:1.0.2k-19.el7

Check the version

[root@haproxy-master ~]# haproxy -version
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <>

Make a copy of the default haproxy file.

[root@haproxy-master ~]# cd /etc/haproxy/
[root@haproxy-master haproxy]# ls
[root@haproxy-master haproxy]# cp haproxy.cfg haproxy.cfg-back

Start and enable the service using the following commands.

[root@haproxy-master ~]# systemctl start haproxy
[root@haproxy-master ~]# systemctl enable haproxy
ln -s '/usr/lib/systemd/system/haproxy.service' '/etc/systemd/system/'
[root@haproxy-master ~]# systemctl is-active haproxy

Haproxy doesn’t create a log file. We will have to create it. You can follow this article to configure the log file.

Check Out: Configure HaProxy Server Log In Rsyslog On CentOS 7

Go to the vim /etc/rsyslog.conf and uncomment the below parameters.

# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514


# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514

Restart the service and create a haproxy.log file in /var/log/

Once you check the status of service and found the below error.

[root@haproxy-master log]# systemctl status haproxy 
haproxy.service - HAProxy Load Balancer
   Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled)
   Active: active (running) since Wed 2020-04-01 12:02:17 PDT; 2min 44s ago
 Main PID: 17588 (haproxy-systemd)
   CGroup: /system.slice/haproxy.service
           ├─17588 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/
           ├─17589 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/ -Ds
           └─17590 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/ -Ds

Apr 01 12:04:42 haproxy-master python[17592]: SELinux is preventing /usr/sbin/haproxy from name_connect access on the tcp_socket port 5002.

you have to set an SELinux boolean and if you don’t want it, you can disable SELinux. 

[root@haproxy-master log]# setsebool -P haproxy_connect_any 1
[root@haproxy-master log]# systemctl restart haproxy

Check the status again, the error won’t be showing.

[root@haproxy-master log]# systemctl status haproxy 
haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled)
Active: active (running) since Wed 2020-04-01 12:08:14 PDT; 9s ago
Main PID: 18660 (haproxy-systemd)
CGroup: /system.slice/haproxy.service
├─18660 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/
├─18661 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/ -Ds
└─18662 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/ -Ds

Apr 01 12:08:14 haproxy-master systemd[1]: Started HAProxy Load Balancer.
Apr 01 12:08:14 haproxy-master haproxy-systemd-wrapper[18660]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/ -Ds

Next step, we will configure two apache web server. I’ve set up a web server.

Check Out:  Copy Database To Another Database Using SQLYog On Ubuntu

webserver 1 output:

[root@memcached ~]# curl
testing first website

webserver 2 output:

[root@redhat html]# curl
testing website second

Now I will create a separate file for haproxy web server. 

[root@haproxy-master haproxy]# vim ha_web.cfg

Verify the configuration settings before restarting the haproxy service.

[root@haproxy-master haproxy]# haproxy -c -f /etc/haproxy/haproxy.cfg
Configuration file is valid

Now configure the haproxy for a webserver.

[root@haproxy-master haproxy]# vim haproxy.cfg
# to have these messages end up in /var/log/haproxy.log you will
# need to:
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
# local2.* /var/log/haproxy.log
log local2

chroot /var/lib/haproxy
pidfile /var/run/
maxconn 4000
user haproxy
group haproxy

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

# main frontend which proxys to the backends
frontend main 
## acl url_static path_beg -i /static /images /javascript /stylesheets
## acl url_static path_end -i .jpg .gif .png .css .js
bind *:80

## use_backend static if url_static
default_backend web_server

# static backend for serving up images, stylesheets and such
backend static
balance roundrobin
server static check

# round robin balancing between the various backends
backend web_server
balance roundrobin
mode http
server check
server memcached check

Check Out:  How To Install and Configure MongoDB On Ubuntu

we will hit the load balancer IP and it should go to the webserver 1 and 2 to the server the website with load balancing using round-robin. 

[root@haproxy-master ~]# curl
testing first website
[root@haproxy-master ~]# curl
testing website second

We can change the timeout values in the configuration file. if we don’t define the bind *:80 and you try to access the website using the load balancer IP. It won’t work. 

You’re done haproxy configure for apache install check config linux

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment