How to Install and Configure the HAProxy on CentOS?

HAProxy is a very fast and decent tool to achieve high availability, load balancing. It can work with TCP and HTTP-based applications. Most of the websites require very high uptime for their websites, which can't be possible with a single server setup. It is required to set up high availability environment which can be managed with single server failure.

Following are the steps to install HAProxy on Centos. Load Balancer will transfer the request to the webserver and distribute the traffic to balance the load.

  1. Install HAProxy using the below command.

    # sudo yum install haproxy
  2. Configure HAProxy Configuration file as per the requirement. It is located at /etc/haproxy. You can use your favorite editor to modify the haproxy.cfg

    # nano /etc/haproxy/haproxy.cfg
    global
            log 127.0.0.1   local0
            log 127.0.0.1   local1 debug
            maxconn   45000 # Total Max Connections.
            daemon
            nbproc      1 # Number of processing cores.
    defaults
            timeout server 86400000
            timeout connect 86400000
            timeout client 86400000
            timeout queue   1000s
    
    # [HTTP Site Configuration]
    listen  http_web 192.168.1.10:80
            mode http
            balance roundrobin  # Load Balancing algorithm
            option httpchk
            option forwardfor
            server server1 192.168.1.10:80 weight 1 maxconn 512 check
            server server2 192.168.1.10:80 weight 1 maxconn 512 check
    
    # [HTTPS Site Configuration]
    listen  https_web 192.168.1.10:443
            mode tcp
            balance source# Load Balancing algorithm
            reqadd X-Forwarded-Proto: http
            server server1 192.168.1.101:443 weight 1 maxconn 512 check
            server server2 192.168.1.102:443 weight 1 maxconn 512 check
    
    
    

You can modify the IP Address in the configuration file as per your network setup. Inside HTTP Site Configuration, if any request received on port 80 of IP Address 192.168.1.10, it will redirect the request to Port 80 of 192.168.1.101 or 192.168.10.102 to balance the server load. Same thing will work for the https with port 443. If any request comes on port 443 of IP Address 192.168.1.10, it will redirect the request to the Port 443 of 192.168.1.101 or 192.168.10.102 to balance the server load.

You can also modify the configuration file further as per your requirement.

  • nbproc (value) # Number of processing cores.
  • mode (value) # 'http' for http website and ‘tcp’ for https website
  • balance (value) # Type of load balancing like ‘source’, ’roundrobin’ etc

  1. Hit the below command to start and enable the haproxy.

    # systemctl start haproxy
    # systemctl enable haproxy

This is it. You have installed and configured HAProxy service.

  • 0 Users Found This Useful

Was this answer helpful?

Related Articles

How to Install phpMyAdmin on CentOS 7?

phpMyAdmin is a web-based GUI tool to manage your MySQL and MariaDB Databases. It is an...

How to install Joomla in CentOS 7?

Joomla is a powerful open source content management system for publishing web content. Jumla uses...

How do I connect to Linux VPS through SSH from Linux and Windows OS?

This article will guide you on how to login to Linux VPS through SSH (Secure Shell) from Linux...

How to transfer cPanel accounts from Shared Hosting to VPS/Dedicated Server manually?

cPanel facilitates you to restore full cPanel account along with websites, email accounts, MySQL...

How to Install Java on Ubuntu 18.04 ?

Java is a programming language developed by Sun Microsystems. There are lots of applications and...