How to Install HAProxy on Ubuntu

Enhance your Ubuntu setup with HAProxy for superior load balancing & scalability. Our guide provides practical insights to optimize performance & manage traffic.

Introduction

In today’s digital world, web traffic management is crucial for any business to keep things running smoothly. High Availability Proxy (HAProxy) is the most powerful tool that helps manage web traffic to ensure sites stay fast and reliable. This open-source software acts as a load balancer and proxy server, making it easier to manage incoming requests. It balances the load across multiple servers and directs user requests efficiently.

Benefits of HAProxy: 

  • High Availability: Keeps operations running by balancing traffic across servers and offering failover. 
  • Load Balancing: Efficiently manages incoming traffic to prevent server overload and boost performance. 
  • Scalability: Easily scales to handle more traffic and supports additional servers. 
  • Advanced Traffic Management: Includes features like SSL termination, content switching, and HTTP/2 support. 
  • Real-Time Statistics: Provides metrics and monitoring through a statistics page for performance management. 
  • Flexibility: Configurable for TCP and HTTP load balancing with support for custom rules. 
  • Security: Enhances security with access control, IP whitelisting, and SSL/TLS encryption. 
  • Cost-Effective: Open-source and free, offering robust capabilities like commercial solutions. 

Applications of HAProxy: 

  • Web Traffic Load Balancing: Distributes HTTP/HTTPS traffic across servers. 
  • Application Delivery: Balances load for application servers. 
  • SSL Termination: Offloads SSL decryption from backend servers. 
  • TCP Load Balancing: Manages TCP traffic for numerous services. 
  • High Availability: Provides failover and redundancy. 
  • Content Switching: Routes traffic based on content or URL patterns. 
  • Real-Time Monitoring: Offers metrics and health checks. 
  • Security: Implements access control and DDoS protection.  

Step-by-Step Guide

1. Login to the Ubuntu terminal with username and password.
Run the command below to update the latest packages.

Command: sudo apt update
Ubuntu terminal login with username and password, followed by the Command: sudo apt update for updating packages
Ubuntu terminal login with username and password, followed by the Command: sudo apt update for updating packages

2. Now, install HAProxy by running the below command.

Command: sudo apt install haproxy
Command: sudo apt install haproxy for installing HAProxy
Command: sudo apt install haproxy for installing HAProxy

3. You can verify the version of HAProxy by running the below command.

Command: haproxy -v
Command: haproxy -v to verify HAProxy version
Command: haproxy -v to verify HAProxy version

4. As one Apache server is already installed, create another Apache server. First, update the latest packages by running the below command.

Command: sudo apt update
Command: sudo apt update to update packages before installing a second Apache server.
Command: sudo apt update to update packages before installing a second Apache server.

5.  Run the command below to install the second Apache server.

Command: sudo apt-get install apache2 -y
Command: sudo apt-get install apache2 -y to install a second Apache server.
Command: sudo apt-get install apache2 -y to install a second Apache server.

6. Write a message to check if the server is working or not.

Command: echo “<h1>Success!  this is my second apache server</h1>” |tee /var/www/index.html

If you see the message “Success! This is my second apache server”, then the server is running.

7. Open the new browser and enter http://your_ip_address:80, then you will see the message: “Success! This is my second Apache server.”

Browser displaying the message 'Success! This is my second Apache server'.
Browser displaying the message ‘Success! This is my second Apache server’.

8. Now, edit the configuration file by running the command below.

Command: sudo nano /etc/haproxy/haproxy.cfg
Command: sudo nano /etc/haproxy/haproxy.cfg to edit the HAProxy configuration file
Command: sudo nano /etc/haproxy/haproxy.cfg to edit the HAProxy configuration file

9. Edit the configuration file.

Enter the front end and back end, and listen to the stats.

Command:
frontend apache_front
                    bind *:8404
                    default_backend apache_backend_servers
                    option forwardfor
backend apache_backend_servers
                    balance roundrobin
                    server backend01 xxx.xxx.6.147:80 check
                    server backend02 xxx.xxx.6.148:80 check
listen stats
                    bind *:8404
                    stats enable
                    stats uri /stats
                    stats admin:password

Note:

  • bind *:8404 will listen to requests on port 8404.
  • While signing in, enter username and password as admin and password, respectively.
  • Save the file by Pressing Ctrl+o (saves content to the current file)
  • When prompted, press Enter and
  • To exit GNU nano editor, press Ctrl+x
Editing HAProxy configuration file to include frontend, backend, and stats settings
Editing HAProxy configuration file to include frontend, backend, and stats settings

10.  Run the command below to see if the configuration is valid or not.

Command: sudo haproxy -c -f /etc/haproxy/haproxy.cfg

You will get the message as ‘configuration file is valid.’

Command: sudo haproxy -c -f /etc/haproxy/haproxy.cfg to check HAProxy configuration validity
Command: sudo haproxy -c -f /etc/haproxy/haproxy.cfg to check HAProxy configuration validity

11.  After editing the file, restart haproxy

Command: sudo systemctl restart haproxy
Command: sudo systemctl restart haproxy to restart the HAProxy service after configuration changes
Command: sudo systemctl restart haproxy to restart the HAProxy service after configuration changes

12.  Once the system is restarted, enable haproxy by running the below command.

Command: sudo systemctl enable haproxy
Command: sudo systemctl enable haproxy to enable HAProxy to start on system boot
Command: sudo systemctl enable haproxy to enable HAProxy to start on system boot

13.  Check the status of haproxy

Command: sudo systemctl status haproxy
Command: sudo systemctl status haproxy to check the HAProxy service status
Command: sudo systemctl status haproxy to check the HAProxy service status

14.  To know about HAProxy statistics,

Open the web browser and type http://your_ip_address:8404/stats.

You will be asked to enter your username and password and click Sign in.

Note: For username and password, go to step 9.

Web browser displaying the HAProxy statistics page with a prompt for username and password
Web browser displaying the HAProxy statistics page with a prompt for username and password

15.  You can see the HAProxy statistics report. The HAProxy statistics page gives a real-time overview of the performance and status of your HAProxy load balancer. It helps you monitor and manage your load-balancing setup effectively.

HAProxy statistics page showing real-time performance and status
HAProxy statistics page showing real-time performance and status

16.  By installing HAProxy, you can enhance the availability of servers and manage traffic efficiently.

Conclusion 

In summary, HAProxy is a robust tool for enhancing network performance and reliability. It excels in load balancing, SSL termination, and high availability, ensuring efficient traffic management and continuous service. Its flexibility and real-time monitoring make it ideal for various applications, while its open-source nature provides powerful capabilities at a low cost. 

FAQs 

Q1. What are the usages of HAProxy? 

Answer: HAProxy is free and open-source software that provides a high-availability load balancer and proxy (forward proxy, reverse proxy) for TCP and HTTP-based applications that spread requests across multiple servers. 

Q2. Where is HAProxy used? 

Answer: HAProxy, or High Availability Proxy is used by RightScale for load balancing in the cloud. HAProxy is installed with RightScale load balancer server templates. Load-balancer servers are also known as front-end servers. Generally, their purpose is to direct users to available application servers. 

Q3. What is the front end in HAProxy? 

Answer: A frontend section defines the IP addresses and ports that clients can connect to. You may add as many frontend sections as needed to expose various websites or applications to the internet. 

Q4. Where is the HAProxy default location? 

Answer: /etc/haproxy/haproxy 

The following is an example of the default configuration file for HAProxy on the Overcloud’s Controller nodes. This file is located at /etc/haproxy/haproxy. 

Q5. Can HAProxy be used as a web server? 

Answer: HAProxy is not a web server, because “web server” implies an HTTP endpoint that can serve static content from files and/or dynamic content generated from code. 

At The Scribe, we can help you optimize your HAProxy setup with clear, expert documentation. Contact us today to improve your network performance and ensure efficient load balancing.

Komal

Komal

Table of Contents

Read More

Scroll to Top