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 set File Upload Size limit in Apache?

As we know Apache is an open-source and free web server. Here, we will configure apache to set...

How to install ISPConfig on CentOS 7.x?

This article will help you on how to install ISPConfig on CentOS 7.xISP config is an open-source...

How to place an order for Linux VPS hosting?

Step-1: First, select your desired Linux VPS Hosting plan with your desired billing cycle...

How to add IP address in Ubuntu using command?

Sometimes you may require to add the IP Address manually in ubuntu network Interface. Following...

Do You Provide TUN/TAP On VPS Hosting?

Typically, TUN/TAP is used when you want to utilize VPN services from your VPS. Windows VPS...