How to Configure Apache Load Balancer

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.

cPanel load balancing isn’t an option as of February 2021. However, there are many open discussions regarding future implementation.

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

Log into SSH. Afterwards, install Apache web server on the load balancer server from your Linux distribution (distro) repositories (e.g. CentOS, Ubuntu, etc.).

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
  • proxy_http
  • Proxy_balancer (for Balancer Manager)
  • lbmethod_bytraffic (for round robin load balancing)

Ensure the necessary modules are enabled.

CentOS: 

httpd -M | grep 'proxy\|lbmethod'

Debian:

apache2ctl -M | grep 'proxy\|lbmethod'

If any module listed above is not installed or enabled, enable it.

CentOS: 

yum install [module]

Debian: 

a2enmod [module]

Restart Apache if you had to enable a module.

CentOS:

systemctl restart httpd

Debian:

Systemctl restart apache2

Configure Apache Load Balancing

Create and edit a new load balancer file using Nano, Vim, Emacs, etc.

CentOS:

nano /etc/httpd/conf.d/loadbalancer.conf

Debian:

Nano /etc/apache2/conf-enabled/loadbalancer.conf

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:

  • serverpool with 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
  • sitePass with the URI which will redirect to the server pool members
<proxy balancer://serverpool>
        BalancerMember http://1.2.3.4:80
        BalancerMember http://5.6.7.8:80
        ProxySet lbmethod=bytraffic
</proxy>
ProxyPass "/site" "balancer://serverpool/"
ProxyPassReverse "/site" "balancer://serverpool/"

If you plan to add domains with a valid SSL certificate, you may need to install the mod_ssl Apache module as well.

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 .bash_history file.

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. 

Edit Apache Load Balancer settings
Apache Load Balancer Manager settings

To get started, create and edit a new Apache configuration file.

CentOS:

nano /etc/httpd/conf.d/loadbalancer-manager.conf

Debian:

nano /etc/apache2/conf-enabled/loadbalancer-manager.conf

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.

CentOS:

AuthType "basic"
AuthName "lb-manager"
AuthUserFile /etc/httpd/htpasswd
Require valid-user

Debian:

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.

CentOS:

htpasswd -c /etc/httpd/htpasswd admin

Debian:

htpasswd -c /etc/apache2/htpasswd admin

You’ll be prompted for a password. Enter the password twice. Save it in a password manager like KeePass.

Add the following to the bottom of your load balancer configuration file, replacing “lb-manager” to match your balancer manager location:

ProxyPass "/lb-manager" "!"

Restart Apache.

Open the URL in your web browser to access your balancer-manager:
http://[serverhostname]/[lb-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)
  • Timeout
  • 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.

BalancerPersist On

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.

Have full control of your environment and deploy the best operating and management systems that fit your needs with our reliable Cloud VPS solutions!
J
Jacqueem Content Writer I

Technical writer focused on cybersecurity and musicianship.

More Articles by Jacqueem

Was this article helpful? Let us know!