Configure Apache Load Balancer Easily with 1 Code Block | InMotion Hosting (2024)

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
  • Enable Proxy Server Modules
  • Configure Apache Load Balancing
  • Configure Apache Load Balancer Manager

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.

Configure Apache Load Balancer Easily with 1 Code Block | InMotion Hosting (1)

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/htpasswdRequire valid-user

Debian:

AuthType "basic"AuthName "lb-manager"AuthUserFile /etc/apache2/htpasswdRequire 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.

Experience full control over your server environment and deploy the best operating and management systems that fit your needs with our reliable Cloud VPS Hosting!

Configure Apache Load Balancer Easily with 1 Code Block | InMotion Hosting (2024)

FAQs

How to configure Apache as load balancer? ›

How to Configure Apache Load Balancer
  1. Install Required Apache modules. We require 4 Apache modules to configure Apache load balancer – mod_proxy, mod_proxy_http, mod_proxy_balancer, mod_lbmethod_byrequests. ...
  2. Restart Apache Server. ...
  3. Setup backend servers. ...
  4. Configure Apache Load Balancer. ...
  5. Restart Apache Server.
Jun 29, 2020

What are the possible ways of configuring a load balancer? ›

Create and configure a load balancer
  • Initiate load balancer creation. ...
  • Set the region. ...
  • Set computing configuration. ...
  • Configure network. ...
  • Configure listeners. ...
  • Enter the name. ...
  • (Optional) Enable Logging. ...
  • (Optional) Add tags.

How to configure the load balancing in Apache Tomcat? ›

By default, Apache will attempt to use port 80, the same default m-Power port for production. You may need to change either Apache or your production Tomcat to use a different port. Once you have verified that Apache can start, you will need to download and install Apache's load balancing module. You can do so here.

How to configure software load balancer? ›

Select General > Load balancer. Specifies the Web application load balancer URL. If a value is not specified, then API Gateway uses the default hostname and port number during publish of an API Gateway asset from CentraSite to API Gateway. For example, http://myHostname:9072.

Can we use Apache as a load balancer? ›

To create your Apache load balancer, you will need to have the Apache module mod proxy and the mod proxy balancer which are readily available for download. The Apache Software Foundation provides support for its user community along with other open source applications.

What is the command to create a load balancer in AWS? ›

To create a load balancer

Use the create-load-balancer command to create a dualstack load balancer. Use the create-target-group command to create a target group, specifying the same VPC that you used for your EC2 instances. You can create IPv4 and IPv6 target groups to associate with dualstack load balancers.

What are the three types of load balancers? ›

Traffic type
Load balancer typeTraffic type
Application Load BalancersHTTP or HTTPS
Passthrough Network Load BalancersTCP or UDP These load balancers also support other IP protocol traffic such as ESP, GRE, ICMP, and ICMPv6.
Proxy Network Load BalancersTCP with optional SSL offload

How to configure load balancing on server? ›

In the case of a NLB, you'll want to visit the Network Load Balancing Manager Panel where you'll create a virtual server cluster on an NLB node using these configuration steps: Click “Tools” Click “Network Load Balancing Manager” Select and right click “Network Load Balancing Clusters”

How to configure Apache and Tomcat? ›

Enable Apache auto-configure
  1. In the server. xml for Tomcat (<tomcat-install-folder>\conf\server. xml), enable Apache auto-configure. ...
  2. Stop the Apache HTTP Web server if it is running.
  3. Restart Tomcat. The <tomcat-install-folder>\conf\auto\mod_jk. conf file is generated.

How to load Apache server? ›

Step by step Apache install on Windows

Extract the contents of the Apache Web Server 2.4 zip to the file system. Locate the extracted Apache24 folder and copy this folder to the root of C:\ Open the C:\Apache24\bin folder and run the httpd.exe command.

What is the difference between clustering and load balancing in Tomcat? ›

Through clustering, multiple Tomcat servers can be connected to act as one, providing redundancy and high availability. Load balancing, on the other hand, evenly distributes network traffic across a cluster of servers to ensure no single server bears too much load.

How to configure software load balancer in Linux? ›

Configuration
  1. Check Apache HTTP Server with httpd and mod_ssl packages installed. These packages might not be installed on a default Ubuntu distribution.
  2. Copy the certificate files into the /etc/apache2/ssl/ directory. ...
  3. Add the SSL and Load Balancer configuration to the Apache HTTP Server.
  4. Restart the Apache HTTP Server.
Mar 26, 2024

How to configure SSL in load balancer? ›

Associate an ACM SSL certificate with a Classic Load Balancer
  1. Open the Amazon EC2 console.
  2. In the navigation pane, choose Load Balancers. ...
  3. Choose the Listeners tab, and then choose Edit.
  4. For Load Balancer Protocol, choose HTTPS.
  5. For SSL Certificate, choose Change.
  6. Select Choose a certificate from ACM.

How to check if network load balancing is working? ›

To test network load balancing, connect a browser to the cluster IP address, for example: http://192.168.10.10. Refresh the screen multiple times. If the cluster is operating successfully, web pages from different machines in the cluster appear after each refresh.

How do I configure Apache to use SSL? ›

II. Apache: Installing & Configuring Your SSL Certificate
  1. Copy the certificate files to your server. ...
  2. Find the Apache configuration file (httpd. ...
  3. Identify the SSL <VirtualHost> block you need to configure. ...
  4. Configure the <VirtualHost> block for the SSL-enabled site. ...
  5. Test your Apache configuration file before restarting.

How to configure Apache to run PHP? ›

  1. Why PHP on Apache?
  2. Step 1: Visit the official website of PHP. ...
  3. Step 2: Extract the files and rename the extracted file to PHP8. ...
  4. Note: Make sure that both Apache and PHP extracted files should be in the same folder.
  5. Step 3: To check whether the PHP directory is added to the path of system variables or not.
Dec 16, 2022

Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 6011

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.