HAProxy also stands for High Availability Proxy is a free, open-source Load Balancer and proxying solution designed to simplify and secure modern application architectures. It allows a web server to distribute incoming requests across multiple endpoints. It is very useful where too many concurrent connections over-saturate the capability of a single server. It is gaining popularity due to its efficiency, reliability, and low memory and CPU footprint.

In this post, we will explain how to install HAProxy on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04 for HAProxy.
  • Two servers running Ubuntu 22.04 for Apache Backend server.
  • A root password is configured on all servers.

Setup Apache Web Servers as Backend Server

First, you will need to set up two Apache web servers as a backend server to perform the tutorial.

On the first backend server, install the Apache package with the following command:

apt-get install apache2 -y

After the installation, create a sample Apache index page using the following command:

echo "

Success! This is my first Apache Server

" | tee /var/www/html/index.html

On the second backend server, install the Apache package with the following command:

apt-get install apache2 -y

Next, create a sample Apache index page using the following command:

echo "

Success! This is my second Apache Server

" | tee /var/www/html/index.html

Once you are done, you can proceed to install the HAProxy server.

Install HAProxy

By default, HAProxy is included in the Ubuntu 22.04 default repository. You can install it by running the following command:

apt-get install haproxy -y

Once the HAProxy is installed, start the HAProxy service and enable it to start at system reboot:

systemctl start haproxy

systemctl enable haproxy

Once you are finished, you can proceed to configure the HAProxy.

Configure HAProxy

At this point, the HAProxy is installed and running. Now, you will need to edit the HAProxy default configuration file and define the backend web servers.

nano /etc/haproxy/haproxy.cfg

Add your both backend Apache servers:

frontend apache_front
        # Frontend listen port - 80
        bind *:80
        # Set the default backend
        default_backend    apache_backend_servers
        # Enable send X-Forwarded-For header
        option             forwardfor
  
# Define backend
backend apache_backend_servers                                                                                                                     
        # Use roundrobin to balance traffic
        balance            roundrobin
        # Define the backend servers
        server             backend01 172.16.0.200:80 check
        server             backend02 172.16.0.201:80 check

Save and close the file when you are finished.

Where: 172.16.0.200 is the IP address of the first Apache backend server and 172.16.0.201 is the IP address of the second Apache backend server.

Next, restart the HAProxy service to apply the changes:

systemctl restart haproxy

You can now check the status of the HAProxy with the following command:

systemctl status haproxy

You will get the following output:

? haproxy.service - HAProxy Load Balancer
     Loaded: loaded (/lib/systemd/system/haproxy.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-07-11 09:03:45 UTC; 45s ago
       Docs: man:haproxy(1)
             file:/usr/share/doc/haproxy/configuration.txt.gz
   Main PID: 48217 (haproxy)
      Tasks: 2 (limit: 2242)
     Memory: 71.3M
        CPU: 143ms
     CGroup: /system.slice/haproxy.service
             ??48217 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
             ??48219 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock

Jul 11 09:03:45 ubuntu2204 systemd[1]: Starting HAProxy Load Balancer...
Jul 11 09:03:45 ubuntu2204 haproxy[48217]: [NOTICE]   (48217) : New worker #1 (48219) forked
Jul 11 09:03:45 ubuntu2204 systemd[1]: Started HAProxy Load Balancer.

Test HAProxy

At this point, HAProxy is configured and running. Now, it’s time to verify whether the HAProxy is functioning or not.

Open your web browser and type the URL http://your-haproxy-ip. You will see that HAProxy is sending requests to backend servers one by one after each refresh.

<img alt="HAProxy first server" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/p1.png62cea3a91dc3e.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="297" loading="lazy" src="data:image/svg xml,” width=”750″>

<img alt="HAProxy second server" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/p2.png62cea3a944ee0.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="309" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

In this post, we showed you how to install and configure the HAProxy server to distribute load across two Apache web servers. on Ubuntu 22.04. You can now implement HAProxy in the production environment to increase your web application performance and availability.

<img alt="Hitesh Jethva" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/hitesh-80.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="76" src="data:image/svg xml,” width=”80″>

About Hitesh Jethva

Over 8 years of experience as a Linux system administrator. My skills include a depth knowledge of Redhat/Centos, Ubuntu Nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, Squid proxy, NFS, FTP, DNS, Samba, LDAP, OpenVPN, Haproxy, Amazon web services, WHMCS, OpenStack Cloud, Postfix Mail Server, Security etc.