Bobcares

Deploy HAProxy on Ubuntu 22.04 in 5 Steps

PDF Header PDF Footer

Learn how to deploy HAProxy on Ubuntu 22.04. Our HAProxy Support team is here to answer queries and concerns.

Deploy HAProxy on Ubuntu 22.04 in 5 Steps

Deploy HAProxy on Ubuntu 22.04 in 5 StepsHAProxy is designed for distributing traffic efficiently across multiple servers. It acts as both a reverse and forward proxy for TCP and HTTP-based applications, enhancing performance, reducing server load, and ensuring high availability.

Today, we will walk through the process of setting up HAProxy on Ubuntu to load-balance traffic between two Apache web servers.

Step 1. Prepare Backend Apache Servers

Before configuring HAProxy, we need at least two backend servers running Apache.

Log in to each backend server and run:

apt-get install apache2 -yCopy Code

For easier identification during testing, create a unique index page on each server:

On the first server:

echo "<h1>This is my first Apache Server</h1>" | tee /var/www/html/index.htmlCopy Code

On the second server:

echo "<h1>This is my second Apache Server</h1>" | tee /var/www/html/index.htmlCopy Code

Step 2. Install HAProxy

On the server where HAProxy will run, execute this command:

apt-get install haproxy -yCopy Code

Then, verify the installation:

haproxy -vCopy Code

We will see output similar to:

HAProxy version 2.4.24-0ubuntu0.22.04.1 2023/10/31 – https://haproxy.org/Copy Code

Now, start and enable the HAProxy service:


systemctl start haproxy
systemctl enable haproxy
Copy Code

Step 3. Configure HAProxy for Load Balancing

Edit the HAProxy configuration file:

nano /etc/haproxy/haproxy.cfgCopy Code

Add the following configuration:


frontend haproxy-main
bind *:80
option forwardfor
default_backend apache_webservers
backend apache_webservers
balance roundrobin
server websvr1 192.168.1.13:80 check
server websvr2 192.168.1.17:80 check
Copy Code

Replace `192.168.1.13` and `192.168.1.17` with the actual IP addresses of the Apache backend servers.

Frontend handles incoming connections on port 80. The backend defines the pool of servers to which traffic will be forwarded, using the round-robin method.

To further control routing based on IP ranges, you can implement HAProxy ACLs using CIDR to manage access policies more securely.

Step 4. Enable HAProxy Stats Page with Authentication

To monitor traffic and server health, enable HAProxy’s built-in statistics page.

We need to open the configuration file again:

nano /etc/haproxy/haproxy.cfgCopy Code

Then, add this block:


listen stats
bind :8800
stats enable
stats uri /
stats hide-version
stats auth admin:password
default_backend apache_webservers
Copy Code

Save and close the file, then restart HAProxy to apply changes:

systemctl restart haproxyCopy Code

Now, check the service status:

systemctl status haproxyCopy Code

We will see a confirmation like this:


● haproxy.service - HAProxy Load Balancer
Active: active (running)
Copy Code

If you’re working with HTTPS traffic, consider using HAProxy ACLs with ssl_fc to make decisions based on whether SSL is in use—ideal for secure applications.

Step 5. Test the HAProxy Setup

Open a browser and go to:

http://<your-haproxy-ip>Copy Code

We will be able to see alternating content from our two Apache servers, confirming that HAProxy is successfully balancing traffic.

If the page fails to load or displays unexpected errors, reviewing your configuration and ensuring there are no syntax issues is key. This guide on fixing “unable to parse headers” in HAProxy error files can help in diagnosing those specific errors.

How to Access the Stats Page

Head to the http://<your-haproxy-ip>:8800/ and log in using the configured credentials to view detailed load balancing stats and backend health checks.

For setups using TCP load balancing instead of HTTP, it’s essential to set up HAProxy health checks tailored for TCP traffic to ensure consistent performance.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In brief, our Support Experts demonstrated how to deploy HAProxy on Ubuntu 22.04.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get featured on the Bobcares blog and share your expertise with a global tech audience.

WRITE FOR US
server management

Spend time on your business, not on your servers.

TALK TO US

Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!