Installing HAProxy 2.5.5 from source in RedHat

5 months ago SETHA THAY 359
Installing HAProxy 2.5.5 from source in RedHat

In this blog post, we will show step by step on how to install HAPrxoy version 2.5.5 from sources downloaded from the HAProxy official website in the Redhat server. As you might already know, HAProxy is free and open-source software that provides high availability load balancers for both TCP and HTTP-based applications by spreading incoming requests across multiple servers. HAProxy is commonly used to bootstrap the performance of a server environment by distributing the workload across servers not only for web or applications but also databases.

In our scenario of conducting this experiment, we are actually using HAProxy as load balancers standing in front of our database cluster for routing transactions to read-only replicas. Follow the steps below to get the work done.

1. Download Source Code of HAProxy 2.5.5

You can click here to download or visit the HAProxy official website to download the version you want. In our case, we experiment with the 2.5.5 version. Next, using tar command to extract source code.

tar vxf haproxy-2.5.5.tar.gz

2. Prerequisite

In order to install HAProxy 2.5.5 and start the service properly, we have to have these packages (gcc, pcre-devel, systemd-devel, zlib-devel) pre-installed in RedHat. You can use the following command to check.

rpm -qa | egrep 'gcc|pcre|systemd|zlib'

If there is a missing package, you have to manually download it with the compatible version ex. systemd-libs-219-67.el7_7.1.x86_64 , we need to download systemd-devel-219-67.el7_7.1.x86_64.rpm and then install using the below command

rpm -ivh systemd-devel-219-67.el7_7.1.x86_64.rpm

3. Compile

Navigate to the source code then extracted folder of HAProxy and then use the below command to compile

make -j $(nproc) TARGET=linux-glibc USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1

4. Installation

Then, using the below command to install HAProxy

make install

Prepare directory for HAProxy configuration and statistic collector.

cp /usr/local/sbin/haproxy /usr/sbin/haproxy
mkdir -p /var/lib/haproxy/
mkdir -p /etc/haproxy

5. HAProxy Configuration File

Next step, we will provide a sample HAProxy configuration file just for the sake of testing. However, you might have your own existing configuration file which can work as well.

- Create a new HAProxy config file using the below command

touch /etc/haproxy/haproxy.cfg

- Sample HAProxy config file

# Example configuration for a possible web application.  See the
# full configuration options online.
# Global settings
    # 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     /run/
    maxconn     4000
    # 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                    tcp
    log                     global
    option                  tcplog
    option                  dontlognull
    option http-server-close
    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 *:5000
#    acl url_static       path_beg       -i /static /images /javascript /stylesheets
#    acl url_static       path_end       -i .jpg .gif .png .css .js
#   use_backend static          if url_static
#    default_backend             app
# static backend for serving up images, stylesheets and such
#backend static
#    balance     roundrobin
#    server      static check
# round robin balancing between the various backends
#backend app
#    balance     roundrobin
#    server  app1 check
#    server  app2 check
#    server  app3 check
#    server  app4 check
listen stats
    bind *:9201
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats uri /
    stats refresh 5s
    stats show-node 

- Then, we can create an empty file for the stats collector of HAProxy. This stats collector will record traffic of connection accessing through HAProxy and provide us an insight into what's going on in HAProxy.

touch /var/lib/haproxy/stats

5. Create HAProxy Service

Since we install HAProxy from the source, we have to create a system service in Redhat with creatable privilege users manually. Using the following command to create the service called haproxy

vi /etc/systemd/system/haproxy.service

Description=HAProxy Load Balancer
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/" "EXTRAOPTS=-S /run/haproxy-master.sock"
ExecStartPre=/usr/local/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecStart=/usr/local/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $EXTRAOPTS
ExecReload=/usr/local/sbin/haproxy -f $CONFIG -c -q $EXTRAOPTS
ExecReload=/bin/kill -USR2 $MAINPID

Next, we can now enable, start, and check the service of installed HAProxy v2.5.5 using the below command

systemctl enable haproxy
systemctl start haproxy
systemctl status haproxy

Finally, now let's open the web browser and check the statistic page with port 9201 as we've config in haproxy.cfg. We should see the screen below


THANK YOU, Find Us @

About author

Author Profile


Software Engineer & Project Manager. I am willing to share IT knowledge, technical experiences and investment to financial freedom. Feel free to ask and contact me.

DigitalOcean Referral Badge

Scroll to Top