How to Set up HAProxy on CentOS

HAProxy is a service that can help with load balancing of web servers, or just proxying traffic. We recommend installing through the Epel repository to keep the packages updated.

Install Epel:

Run the following commands:

yum -y install epel-release

If yum does not find the epel-release package, please see How to enable epel with RPM for installing it via RPM.

Install HAProxy

Run the following command:

yum -y install haproxy

Once it has been installed, you can edit the config file by running the following:

nano /etc/haproxy/haproxy.cfg

Sample configuration for Web Server Proxy

    # Where to save the logs, 
    log local2 info
    chroot      /var/lib/haproxy
    pidfile     /var/run/
    # Max connections per process
    maxconn     256
    # User and group process runs under
    user        haproxy
    group       haproxy

    # Mode the server is running in
    mode               http
    log                global
    option             httplog
    timeout connect    10s
    timeout client     30s
    timeout server     30s

# Define Frontend (set any name for http-in section)
frontend http-in
    # Set server to listen on port 80
    bind *:80
    # Set default backend to forward traffic to
    default_backend    backend_servers
    option             forwardfor

# Define Backend server with roundrobin mode
backend backend_servers
    # Set balance type as roundrobin
    balance            roundrobin
    # Define backend servers
    server             <hostname> check
    server             <hostname> check

Auto-start for CentOS 7

To get HAproxy to run automatically after configuration run the following commands:

systemctl start haproxy
systemctl enable haproxy