Load balancing is the process of distributing traffic across multiple servers for high-availability (HA) and elastic scalability. Many system administrators opt for dedicated software such as HAProxy to incorporate a proxy server. But with the mod_proxy_balancer module, you can easily use an unmanaged Linux cloud server as an Apache load balancer for domains or specified URIs. Then, you can add other unmanaged or cPanel servers to your server pool configuration with an additional line of code. Such server environments usually include a software or hardware RAID solution.
There are multiple load balancing methods. We’ll cover round robin below but the Apache load balancer module can also distribute traffic requests by server load, readiness, or authorized traffic capacity.
Continue reading to learn how to:
- Install Apache on the Load Balancer Server
- Enable Proxy Server Modules
- Configure Apache Load Balancing
- Configure Apache Load Balancer Manager
Install Apache on the Load Balancer Server
For better server security, enable the ModSecurity Apache module as well.
Enable Proxy Server Modules
You’ll need the following Apache modules for load balancing:
- Proxy_balancer (for Balancer Manager)
- lbmethod_bytraffic (for round robin load balancing)
Ensure the necessary modules are enabled.
httpd -M | grep 'proxy\|lbmethod'
apache2ctl -M | grep 'proxy\|lbmethod'
If any module listed above is not installed or enabled, enable it.
yum install [module]
Restart Apache if you had to enable a module.
systemctl restart httpd
Systemctl restart apache2
Configure Apache Load Balancing
The name of the file doesn’t matter. What matters is that the file is titled for you to easily remember what it is, is in the correct directory for your distro, and ends with the .conf file extension.
Add the code block below, but replace the following:
serverpoolwith your preferred name for the balancer function
- IP addresses with the IP, domain, or server hostname of your web servers along with the port number
sitePasswith the URI which will redirect to the server pool members
<proxy balancer://serverpool> BalancerMember http://220.127.116.11:80 BalancerMember http://18.104.22.168:80 ProxySet lbmethod=bytraffic </proxy> ProxyPass "/site" "balancer://serverpool/" ProxyPassReverse "/site" "balancer://serverpool/"
Save changes and restart Apache:
systemctl restart httpd or
systemctl restart apache2
Terminal tip: Use your up arrow key to quickly reuse commands from your
Open the URL to your load balancer function in a web browser. It should display the website from one of the balancer members. When you refresh the page, it should show the next balancer member. If not, try a private browsing session.
If your load balancer doesn’t work, start troubleshooting with your Apache error_log.
Configure Apache Load Balancer Manager
The Apache Balancer Manager allows you to monitor and manage your load balancing configuration. This is optional but a light-weight monitoring solution. For more advanced options, look into security information and event management (SIEM) solutions such as ELK Stack or Splunk. Ensure you have
proxy_balancer_module enabled before continuing below.
To get started, create and edit a new Apache configuration file.
Again, the name of the file doesn’t matter, only the directory and .conf file extension.
Add the following code block to the file, replacing “lb-manager” with your preferred URI for accessing the page.
<location "/lb-manager"> SetHandler balancer-manager allow from all </location>
By default, the page is publicly accessible. To configure Apache Load Balancer Manager securely, add the next code block above the
</location> line. Then, use
htpasswd to create user credentials to restrict the page to authorized users only. Change “AuthName” to match your location URI and “AuthUserFile” with the appropriate file path per your distro.
AuthType "basic" AuthName "lb-manager" AuthUserFile /etc/httpd/htpasswd Require valid-user
AuthType "basic" AuthName "lb-manager" AuthUserFile /etc/apache2/htpasswd Require valid-user
Create a htpasswd file. Replace “admin” at the end with an obscure username.
htpasswd -c /etc/httpd/htpasswd admin
htpasswd -c /etc/apache2/htpasswd admin
You’ll be prompted for a password. Enter the password twice. Save it in a password manager.
Add the following to the bottom of your load balancer configuration file, replacing “lb-manager” to match your balancer manager location:
ProxyPass "/lb-manager" "!"
Open the URL in your web browser to access your balancer-manager:
If you used
htpasswd, you’ll be prompted for an username and password before you can continue.
Load Balancer Manager will display the following notable information:
- Web server version
- Whether Load Balancer changes are retained upon system reboot
- LoadBalancer members, or work URLs, along with server load statistics
You can make changes by clicking the balancer://[serverpoolname] link. Options to edit balancer settings include:
- LBMethod – bytraffic, heartbeat (ready capacity over time but not fully idle), bybusyness, byrequests (you’ll need to have the appropriate modules enabled for the respective method)
- Failover Attempts
- Disable Failover
- Sticky Session to ensure users are bound to a specific server
By default, changes here don’t persist on restart. To change this, add the line below to the bottom of your load balancer configuration file.
Restart Apache. Upon refreshing Apache Load Balancer Manager, you’ll see: “Balancer changes will be persisted on restart.”
Learn more about server administration from our Cloud Server Hosting Product Guide.