How to set up load balancing on CentOS 7 / Redhat 7 Linux using Apache.
Enjoying this content? Subscribe to the Channel!
Master Active/Active Load Balancing on CentOS 7 using Apache (The Ultimate Guide)
Welcome back to Darren’s Tech Tutorials! Today, we’re diving into a crucial topic for anyone running a serious web application: Active/Active Load Balancing.
If you’re hosting mission-critical applications, relying on a single web server is risky. What happens if it fails? Load balancing is the answer, ensuring high availability and distributing traffic efficiently.
In this comprehensive tutorial, we will show you exactly how to set up an Active/Active load balancing solution on CentOS 7 using the robust capabilities of Apache. By the end of this guide, you will have two working web servers being actively balanced by a central Apache proxy, ensuring maximum uptime and performance!
Note: This configuration works flawlessly on both CentOS 7 and Red Hat Enterprise Linux 7 (RHEL 7).
Why Active/Active Load Balancing?
Active/Active load balancing means that all your backend servers are actively serving user requests simultaneously. If Server A fails, Server B is already live and immediately takes over 100% of the traffic, resulting in zero downtime for the end user. This configuration maximizes resource utilization and resilience.
Prerequisites and Setup Environment
To follow along with this tutorial, you will need a minimum of three virtual machines (VMs):
- Load Balancer Server: The central Apache machine that routes traffic. (CentOS 7)
- Web Server 1 (Backend): The first target web server. (CentOS 7)
- Web Server 2 (Backend): The second target web server. (CentOS 7)
Essential Prerequisites:
Before starting the load balancing configuration, please ensure the following software is installed on your machines:
- CentOS 7 Installed: If you need help installing the operating system, check out our tutorial here: CentOS 7 Installation Guide
- Apache Installed: You must have Apache installed and configured on all three machines (specifically, the Load Balancer Server needs the proxy modules). A full Apache installation tutorial can be found here: Apache Installation Guide
All the specific commands referenced in this guide can also be found on the accompanying resource page: Load Balancing Commands
## Step 1: Configuring the Backend Web Servers
The first step is to set up our two backend servers (Web Server 1 and Web Server 2) so that we can easily verify the load balancer is working correctly. We need to create a unique identifier on each server.
On Web Server 1, edit the default Apache index file:
sudo vi /var/www/html/index.html
Replace the content with something descriptive:
<html>
<head>
<title>Server 1</title>
</head>
<body>
<h1>Welcome to Web Server 1! Traffic handled by Server 1.</h1>
</body>
</html>
On Web Server 2, edit the default Apache index file:
sudo vi /var/www/html/index.html
Replace the content with the unique identifier for Server 2:
<html>
<head>
<title>Server 2</title>
</head>
<body>
<h1>Welcome to Web Server 2! Traffic handled by Server 2.</h1>
</body>
</html>
Restart Apache on both backend servers to ensure the new files are active:
sudo systemctl restart httpd
## Step 2: Enabling Apache Proxy Modules on the Load Balancer
On the Load Balancer Server, we need to ensure the necessary Apache proxy modules are loaded. These modules are responsible for handling reverse proxying and load balancing decisions.
While the specific method varies, on CentOS 7, you often enable the modules by ensuring they are referenced in the main httpd.conf or an included configuration file (like a virtual host file).
The key modules we need are:
mod_proxymod_proxy_http(to proxy HTTP traffic)mod_proxy_balancer(to handle load balancing algorithms)
If you installed Apache via standard packages, these are usually available.
## Step 3: Configuring the Active/Active Load Balancer
The main configuration takes place on the Load Balancer Server. We will define a balancer-manager (which is useful for monitoring) and then set up a virtual host that acts as our proxy gateway.
Open the main Apache configuration file (or create a dedicated virtual host file):
sudo vi /etc/httpd/conf/httpd.conf
Add the following configuration block, which defines our load balancing cluster:
# -----------------------------------------------
# Load Balancer Configuration for CentOS 7 / RHEL 7
# -----------------------------------------------
<Proxy balancer://mycluster>
# Define the members of the cluster. Use the IP addresses of your Web Servers.
# We use load factor 1 for Active/Active (all servers handle traffic equally)
BalancerMember http://[IP_of_Web_Server_1]:80 loadfactor=1 route=server1
BalancerMember http://[IP_of_Web_Server_2]:80 loadfactor=1 route=server2
# Set the load balancing method: 'byrequests' sends traffic to the server
# with the fewest requests until failure.
ProxySet lbmethod=byrequests
</Proxy>
# Now configure the main site to use the cluster
<VirtualHost *:80>
ServerName balancer.mydomain.com
# Send all requests (/) through our defined balancer
ProxyPass / balancer://mycluster/
# Crucial for Active/Active setup: tell the proxy to keep track of sessions
ProxyPassReverse / balancer://mycluster/
# Optional: Enable the balancer-manager for monitoring (good for production)
# <Location /balancer-manager>
# SetHandler balancer-manager
# Order deny,allow
# Deny from all
# Allow from [Your Management IP Range]
# </Location>
</VirtualHost>
Replace [IP_of_Web_Server_1] and [IP_of_Web_Server_2] with the actual IP addresses of your backend servers.
## Step 4: Restart and Verify
Once the configuration is complete, save the file and check the Apache configuration for syntax errors:
sudo apachectl configtest
If the syntax is OK, restart the Apache service on the Load Balancer Server:
sudo systemctl restart httpd
Testing the Active/Active Setup
Now for the fun part! You can test the configuration by navigating to the IP address of your Load Balancer Server in a web browser.
- First Refresh: The page should display: “Welcome to Web Server 1! Traffic handled by Server 1.”
- Second Refresh: The page should automatically switch and display: “Welcome to Web Server 2! Traffic handled by Server 2.”
- Subsequent Refreshes: The requests should continue to alternate between Server 1 and Server 2.
If the pages are alternating, congratulations! Your Active/Active load balancing setup is fully operational and is successfully distributing traffic across your backend fleet.
Testing Failover (Active/Active Resilience)
To confirm your failover capability, try the following:
- Stop the Apache service on Web Server 1:
sudo systemctl stop httpd - Refresh the browser accessing the Load Balancer Server several times.
- The page should now only display: “Welcome to Web Server 2! Traffic handled by Server 2.”
- The load balancer automatically detected the failure and directed all traffic to the remaining active server.
- Start Web Server 1 back up, and the balancing will automatically resume!
Conclusion: You’re Now Highly Available!
You have successfully configured a robust, Active/Active load balancing environment on CentOS 7 using Apache. This setup dramatically increases the reliability and performance of your applications by distributing load and providing instantaneous failover protection.
This is a massive step towards building production-ready infrastructure!
If you found this tutorial helpful and managed to set up your load balancer successfully, please let us know in the comments below!
Don’t forget to hit that like button and subscribe to Darren’s Tech Tutorials for more practical guides and expert technology breakdowns.