How To Install HAProxy on Ubuntu 22.04 LTS

Table of Contents

HAProxy is an open-source load balancer proxy used to run several web applications with high performance and increase the speed by dividing the load across multiple servers. In addition, if a server dies, HAProxy detects it and reroutes its web traffic to other servers, ensuring that clients suffer no interruptions. This guide demonstrates how to install HAProxy on Ubuntu 22.04 LTS.

I will demonstrate how to install HAProxy on Ubuntu 22.04 step by step. This tutorial assumes you have at least a fundamental understanding of Linux, are familiar with the shell, and, most importantly, host your website on a VPS. If you are not using the root account, you may need to append “sudo” to the instructions to obtain root access.

Prerequisites

  • A server runs Ubuntu 22.04, 20.04, or any other Debian-based distribution, such as Linux Mint.
  • You should install the operating system from scratch to avoid any potential complications.
  • SSH access to the server, or if you’re on a desktop, launch Terminal.
  • A non-root user with sudo or root access. However, we advocate acting as a non-root sudo user, as carelessness when working as root can destroy your system.

Install HAProxy on Ubuntu 22.04 LTS

Step 1. First, perform the following apt commands in the terminal to ensure that your system packages are up-to-date.

sudo apt update
sudo apt upgrade
sudo apt install wget apt-transport-https gnupg2 software-properties-common

Step 2. Installing Apache HTTP Server on Ubuntu 22.04.

Execute the following command to install the most recent version of Apache on your Ubuntu system:

sudo apt install apache2

Enable Apache (so it starts automatically at system boot), run it, and validate its status using the procedures listed below.

sudo systemctl enable apache2
sudo systemctl start apache2
sudo systemctl status apache2

You can verify the Apache2 version with the command below:

apache2 -v

Step 3. Setup Apache Web Servers as Backend Server.

Now we configure two Apache web servers as the tutorial’s backend server.

  • Install the Apache package on the backend server and generate a sample Apache index page using the below command.
    echo "<H1>Apache Server One</H1>" | tee /var/www/html/index.html
  • Apache package should be installed on the second backend server. Create an example Apache index page with the below command.
    echo "<H1>Apache Server Two</H1>" | tee /var/www/html/index.html

Step 4. Installing HAProxy on Ubuntu 22.04.

HAProxy is accessible by default on the Ubuntu 22.04 basic repository. Now use the command below to install the most recent stable version of HAProxy on your Ubuntu system:

sudo apt install haproxy

Enable HAProxy (so it starts automatically upon system boot), start it, and verify its status using the commands below.

sudo systemctl enable haproxy
sudo systemctl start haproxy
sudo systemctl status haproxy

Step 5. Configure HAProxy. Confirm the installation and verify the version of HAProxy that was installed:

haproxy -v

Now, using your preferred text editor, modify the HAProxy default configuration file and define the backend web servers:

vim /etc/haproxy/haproxy.cfg

Add your two Apache backend 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
  
backend apache_backend_servers                                                                                                                     
        # Use roundrobin to balance traffic
        balance            roundrobin
        # Define the backend servers
        server             HA01 192.168.1.100:80 check
        server             HA02 192.168.1.101:80 check

Save and close the file, then use the systemctl command to restart the HAProxy service:

sudo systemctl restart haproxy

Step 6. Configure Firewall.

Now we configure a Simple Firewall (UFW) with Apache to permit public access on the default web ports for HTTP and HTTPS:

sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable

Step 7. Test HAProxy.

Once completely configured and installed, launch your web browser and enter http://your-haproxy-ip-address in the address bar. After each refresh, HAProxy delivers requests to backend servers individually.

Share
Do You Enjoyed This Article?
Join our community of 3 million people and get updated every week We have a lot more just for you! Lets join us now

Leave a Reply

Your email address will not be published.

Recent Post

Contact us to request a no-charge business process assessment!

Posts You Might

View Our Other Brands and Websites

Pegas

Technology Solutions

Learn about our cloud and open source consulting products and services. 

Pegas

Content Hub

Want to do some if it yourself? We have hundreds of follow along videos. 

Pegas

Chat Inbox (Coming Soon)

We have built a platform that allows you to you have one live chat inbox.

Pegas

Social (Coming Soon)

We provide our clients with a free social media manager so you don’t have to pay extra.