HAProxy is a powerful, open-source load balancer that can be used to distribute incoming traffic across multiple servers. It’s a popular choice for web and application servers, as well as for database clusters and other network-based services. In this blog post, we’ll take a look at how to install latest HAProxy on Linux server.

What is HAProxy?

HAProxy is a software load balancer that is designed to distribute incoming network traffic across multiple servers. It can be used to balance traffic between web servers, application servers, and database servers, among others. It is often used in environments where high availability and scalability are important.

HAProxy can be configured to use several different load balancing algorithms, such as round-robin, least connections, and IP hash. These algorithms determine how incoming traffic is distributed among the servers. For example, the round-robin algorithm sends incoming traffic to the next server in a list, while the least connections algorithm sends traffic to the server with the fewest active connections.

Benefits of HAProxy

HAProxy provides several benefits for your infrastructure. Here are a few of the most notable:

  • High availability: HAProxy can be configured to automatically detect when a server goes down and redirect traffic to another server.
  • Scalability: HAProxy can be used to distribute incoming traffic across multiple servers, allowing you to scale your infrastructure as needed.
  • Flexibility: HAProxy can be configured to use a variety of different load balancing algorithms, allowing you to fine-tune the way traffic is distributed among your servers.
  • Performance: HAProxy is a lightweight software load balancer that has a low overhead. This means that it can handle a large amount of traffic without significantly impacting the performance of your servers.

HAProxy versions, Release Dates and EOL:

VersionsReleased DatesEnd of life
2.8 (LTS)2023-05-312028-Q2
2.7 (stable)2022-12-012024-Q1
2.6 (LTS)2022-05-312027-Q2
2.5 (stable)2021-11-232023-Q1
2.4 (LTS)2021-05-142026-Q2
2.2 (LTS)2020-07-072025-Q2
HAProxy Released Dates and EOL

Steps to install latest HAProxy on Linux server

HAProxy 2.8 is now released and available for download, 2.8 provides traffic shaping, QUIC now prod ready, Lua-based mailers, OCSP auto updates, LetsEncrypt, wolfSSL support and improves user experience with everything related to troubleshooting and issue reporting.

Note: We need to compile latest HAProxy 2.8 on some Linux systems.

latest haproxy installation on linux
latest haproxy installation on linux

Prerequisites:

  • sudo privileges.
  • Stable internet connection.
  • Enable required ports in firewall

First check server OS version then start installation as per OS version:

# egrep '^(VERSION|NAME)=' /etc/os-release

HAProxy installation on Debian and Ubuntu:

Steps to install latest stable or LTS version of HAProxy on Debian and Ubuntu server.

Install the prerequisites:

# apt install --no-install-recommends software-properties-common -y

Ubuntu:

Set up the apt repository as per your Ubuntu version:

Latest stable version 2.7 for Ubuntu 22.04 (Jammy), 20.04 (Focal) and 18.04 (Bionic) only
# add-apt-repository ppa:vbernat/haproxy-2.7 -y
Latest LTS version 2.8 for Ubuntu 22.04 (Jammy) and 20.04 (Focal) only
# add-apt-repository ppa:vbernat/haproxy-2.8 -y

Debian:

Import the HAProxy public GPG Key on Debian server

# curl https://haproxy.debian.net/bernat.debian.org.gpg | gpg --dearmor > /usr/share/keyrings/haproxy.debian.net.gpg

Set up the apt repository as per your Debian version:

Latest stable version 2.7 for Debian 11 (bullseye)
# echo deb "[signed-by=/usr/share/keyrings/haproxy.debian.net.gpg]" https://haproxy.debian.net bullseye-backports-2.7 main > /etc/apt/sources.list.d/haproxy.list

Latest stable version 2.7 for Debian 10 (Buster)
# echo deb "[signed-by=/usr/share/keyrings/haproxy.debian.net.gpg]" https://haproxy.debian.net buster-backports-2.7 main > /etc/apt/sources.list.d/haproxy.list
Latest LTS version 2.8 for Debian 11 (bullseye)
# echo deb "[signed-by=/usr/share/keyrings/haproxy.debian.net.gpg]" https://haproxy.debian.net bullseye-backports-2.8 main > /etc/apt/sources.list.d/haproxy.list

Latest LTS version 2.8 for Debian 12 (Bookworm)
# echo deb "[signed-by=/usr/share/keyrings/haproxy.debian.net.gpg]" https://haproxy.debian.net bookworm-backports-2.8 main > /etc/apt/sources.list.d/haproxy.list

Refresh the apt cache

# apt update

And finally install HAProxy

# apt install haproxy -y

HAProxy installation on CentOS and related distros:

Steps to install Latest HAProxy 2.8 on CentOS, Red Hat Enterprise Linux, Oracle Linux and Rocky Linux.

In order to install HAProxy 2.8 from source, we need some prerequisite packages are installed on system.

# yum -y install wget yum-utils gcc perl pcre-devel openssl-devel zlib-devel readline-devel systemd-devel tar lua lua-devel make

OR
# dnf -y install wget yum-utils gcc perl pcre-devel openssl-devel zlib-devel readline-devel systemd-devel tar lua lua-devel make

Now  Download the HAProxy version using terminal:

For latest stable version
# wget -O /tmp/haproxy.tgz https://www.haproxy.org/download/2.7/src/haproxy-2.7.10.tar.gz
OR
For latest LTS version
# wget -O /tmp/haproxy.tgz https://www.haproxy.org/download/2.8/src/haproxy-2.8.3.tar.gz

Next step is unzip downloaded zip file and go to unzip directory:

# tar -xzvf /tmp/haproxy.tgz -C /tmp && cd /tmp/haproxy-*

Compile downloaded HAProxy source code:

# make USE_NS=1 USE_TFO=1 USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 USE_SYSTEMD=1 USE_LIBCRYPT=1 USE_THREAD=1 TARGET=linux-glibc

# make TARGET=linux-glibc install-bin install-man

Prepare directory for HAProxy configuration

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

Now Add sample HAProxy Configuration File:

cat <<EOT | sudo tee /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    maxconn     4000
    daemon

    stats socket /var/lib/haproxy/stats mode 600 level admin
    stats timeout 2m

    tune.ssl.default-dh-param 2048
    ssl-default-bind-ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS
    ssl-default-bind-options ssl-min-ver TLSv1.2 no-sslv3 no-tls-tickets
    ssl-default-server-ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS
    ssl-default-server-options ssl-min-ver TLSv1.2 no-sslv3 no-tls-tickets

defaults
    mode http
    log global
    option httplog
    option dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    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

listen stats
    bind *:9201
    mode http
    stats enable
    stats hide-version
    stats realm Haproxy\ Statistics
    stats auth admin:password
    stats uri  /haproxy?stats
    stats refresh 5s
    stats show-node
EOT

Then create HAProxy service:

cat <<'EOT' | sudo tee /etc/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/haproxy
ExecStart=/usr/local/sbin/haproxy -f $CONFIG_FILE -p $PID_FILE $CLI_OPTIONS
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -USR1 $MAINPID

[Install]
WantedBy=multi-user.target
EOT

To set environment variables:

cat <<EOT | sudo tee /etc/sysconfig/haproxy
# Command line options to pass to HAProxy at startup
# The default is:
CLI_OPTIONS="-Ws"

# Specify an alternate configuration file. The default is:
CONFIG_FILE=/etc/haproxy/haproxy.cfg

# File used to track process IDs. The default is:
PID_FILE=/var/run/haproxy.pid
EOT

To load Systemd configuration changes:

# systemctl daemon-reload

Managing HAProxy service

let’s review some basic management commands.

Verify installed version of HAProxy:

# haproxy -v

Before starting the HAProxy service please validate haproxy.cfg

# /usr/local/sbin/haproxy -c -V -f /etc/haproxy/haproxy.cfg

Start the HAProxy service:

# systemctl start haproxy.service

Check status of the HAProxy service:

# systemctl status haproxy.service

Restart the HAProxy service:

# systemctl restart haproxy.service

If you want to reload HAProxy service to apply conf:

# systemctl reload haproxy.service

If you want to stop HAProxy service:

# systemctl stop haproxy.service

By default, HAProxy service is disabled to start automatically when the server boots. If you want to enable it at startup, run:

# systemctl enable haproxy.service

Re-disable the service to start up at boot:

# systemctl disable haproxy.service

Accessing the HAProxy stats:

URL: http://server_ip_address:9201/haproxy?stats
Login user: admin
Login password: password

Conclusion

Congratulations! We’ve installed Latest HAProxy 2.8 on Linux system like CentOS, RHEL, Ubuntu and Debian. We hope this 2 minutes stuff helped you and thank you for visiting our website.
Cheers!!!