Steps to Install and Configure HAProxy on CentOS

In this tutorial, we’ll explain Steps to Install and Configure HAProxy on CentOS or Amazon Linux. HAProxy is a very popular open-source software as it is free and it provides HA (high availability) load balancer and it provides a proxy server for TCP and HTTP-based applications that send/forward requests to multiple servers (backend machine). Steps to Install and Configure HAProxy on Centos HAProxy is used by many websites such as GitHub, GoDaddy, Bitbucket, Stack Overflow and etc because it is a very fast and reliable solution for (HA) high availability, it supports TCP and HTTP-based applications and load balancing. the main feature of using HAProxy is to get 99.999% uptime for websites and it is not possible with a single server/machine.

In this article, you’ll learn the simple steps to install and configure HAProxy on Centos Linux.

Steps to Install and Configure HAProxy on CentOS

By Default HAProxy package is available in the yum repository for CentOS or Amazon Linux systems.

$ yum install haproxy

For specific HAProxy version goto download page.

Steps to Configure HAProxy.cfg

Locate your HAProxy configuration file  – /etc/haproxy/haproxy.cfg

HAProxy.cfg sample file

$ vim /etc/haproxy/haproxy.cfg

global

        log 127.0.0.1   local0

        log 127.0.0.1   local1 debug

        maxconn   45000 # Total Max Connections - ((change as per required))

        daemon

        nbproc      1 # Number of processing cores.

defaults

        timeout server 86400000

        timeout connect 86400000

        timeout client 86400000

        timeout queue   1000s



# [sysadmin HTTP Site Configuration]

listen  http_web 192.168.5.25:80

        mode http

        balance roundrobin  # Load Balancing algorithm - ((change as per required))

        option httpchk

        option forwardfor

        server server1 192.168.5.200:80 weight 1 maxconn 512 check

        server server2 192.168.5.201:80 weight 1 maxconn 512 check



# [sysadmin HTTP Site Configuration]

listen  https_web 192.168.5.25:443

        mode tcp

        balance source# Load Balancing algorithm

        reqadd X-Forwarded-Proto: http

        server server1 192.168.5.200:443 weight 1 maxconn 512 check

        server server2 192.168.5.201:443 weight 1 maxconn 512 check

Change the IP and hostname with your server details. Whenever there is any request on IP 192.168.5.25 on port 80, HAProxy will be redirected to port 80 on the backend server “192.168.5.200” and “192.168.5.201”. Similarly, when there is a request on HAProxy on port 443 it will be sent to the port of “192.168.5.200” and “192.168.5.201” servers.

Command to start HAProxy Service on CentOS

$ service haproxy start

Add HAProxy service to auto-start on boot

$ chkconfig haproxy on

HAProxy Command Reference/Tips:

Command to verify HAProxy.cfg file:

Whenever you do any changes to the HAPRoxy.cfg file on CentOS or Amazon Linux check HAProxy file is valid/error/warring. Once you check the HAProxy file is valid then reload the file.

$ service haproxy check
Command to use the updated HAProxy file without restarting HAProxy service.
$ service haproxy reload
Command to Verify HAProxy service running or not
$ service haproxy status
Command to restart HAProxy service
$ service haproxy restart

HAProxy SysAdmin Tips:

  • Before editing HAProxy file take backup and then proceed with changes.
  • Whenever you do any changes to HAProxy.cfg file, Check is there any errors/warring command is mention above.
  • Avoid restarting HAProxy service on minor changes in the HAProxy config file try with reload command first.

End of the article, you learned the steps to Install and Configure HAProxy on Centos or Amazon Linux.


thanks for reading this article, you’ll also like to read below articles.

How to run HAProxy Service as a non-root user in Linux

What is Linux Software yum and rpm

Getting Started with Unix

Install EPEL Repository on CentOS or RHEL or Amazon Linux

Step By Step Method for installing Nagios in Amazon Linux