Apache Load Balancing: How to Use Apache for Load Balancing

Apache Load Balancing: How to Use Apache for Load Balancing. In this post, we introduce load balancing, its features then show you how to use Apache for load balancing.

Apache is one of the most popular web server over the internet. Comes with many features and you may add additional features using modules. Additionally, also use Apache as a reverse proxy to setup a load balancing server for two or more web servers.

A load balancer’s ability to be integrated into application delivery controllers (ADCs) for improved performance and application security is another advantage. Additionally, if a load balancer fails for any reason, it promptly switches the entire workload to a backup server. Let’s discuss some of its additional features and benefits.

What is Load Balancing?

Load Balancing is a process under which all the workload is distributed evenly among different systems, servers as well as computational resources. This technique basically helps distribute the traffic among several servers uniformly to improve the performance as well as the reliability of each application. This further lessens the load on each server and increases server efficiency, which speeds up performance and lowers latency. 

The majority of Internet applications cannot run successfully without load balancing. Additionally, by sending client requests to computer nodes that are idle, it lessens the overloading of computational resources. 

Load balancers are tools that act as a middleman between client devices and backend servers, allowing them to receive and direct client requests to various servers. Because it guarantees that the apps are active and available for users, businesses generally choose it for the load balancing strategy. 

On a daily basis, hundreds and thousands of requests are received by businesses each minute. And if it is a peak season, you may notice a huge rise in traffic. This in turn affects the server performance. Hence, in order to reduce server pressure and maintain high quality media, most businesses started using load balancers.

These devices basically help in tracking every single incoming request and divert the traffic to respective servers depending on various factors. This way load balancing prevents overworking a single server.

Advantages of Using Apache Load Balancing

  • Scalable and lessens troubleshooting.
  • Lessened Bandwidth Usage.
  • Offers quick and easy access to application insights.
  • Session Persistence.
  • Saves time and quickly deploys with software load balancers.
  • Enables the original server to reject suspicious packets before they arrive.
  • Routes client requests while maximizing network efficiency.
  • Permits request to be sent to the same server from a specific client.
  • Improves user experience and focuses on reducing error responses

Apache Load Balancing: How to Use Apache for Load Balancing

Prerequisites

  • Three servers running an Ubuntu operating system.
  • A root user or a user with sudo privileges.

To demonstrate in this article, we use the following domains.

  • web1.example.com
  • web2.example.com
  • balancer.example.com

Update System Packages

First, it is always a good idea to update and upgrade all system packages to the latest version. Update all of them by running the following command on all servers.

				
					apt update -y
apt upgrade -y
				
			

Once all the packages are updated, restart all servers to apply the changes.

				
					reboot
				
			

Install Apache Web Server

Before you start, install Apache web server package on all three servers. Do it by running the following command.

				
					apt install apache2 -y
				
			

After installing the Apache server package, you need to start and enable the Apache service on all servers.

				
					systemctl start apache2
systemctl enable apache2

				
			

Then verify the status of Apache service using the following command.

				
					systemctl status apache2
				
			

You see the Apache status on the following screen.

At this point, Apache web server package is installed on all servers. Now proceed to configure the backend servers.

Configure First Apache Backend Server

Next, you need to create a sample HTML page and Apache virtual host configuration file on the first Apache server.

First, create a sample HTML file with the following command.

				
					nano /var/www/html/web1.html
				
			

Add the following code.

				
					<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <title>Apache Web Server1</title>

<h2>This is Apache Web Server 1 Page!</h2>

</html>

				
			

Save and close the file after you are done.

Next, create an Apache virtual host configuration file.

				
					nano /etc/apache2/sites-enabled/web1.conf
				
			

Add the following configurations.

				
					<VirtualHost *:80>
        ServerName web1.example.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        DirectoryIndex web1.html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

				
			

Save and close the file then restart the Apache service to apply the changes.

				
					systemctl restart apache2
				
			

Configure Second Apache Backend Server

Now, go to the second server and create a sample HTML file using the following command.

				
					nano /var/www/html/web2.html
				
			

Add the following HTML code.

				
					<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <title>Apache Web Server2</title>

<h2>This is Apache Web Server 2 Page!</h2>

</html>
				
			

Save and close the file when you are finished. Next, create an Apache virtual host configuration file.

				
					nano /etc/apache2/sites-enabled/web2.conf
				
			

Add the following configurations.

				
					<VirtualHost *:80>
        ServerName web2.example.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        DirectoryIndex web2.html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

				
			

Save and close the file then restart the Apache service to implement the changes.

				
					systemctl restart apache2
				
			

Create an Apache Load Balancer

At this point, both Apache backend server is configured to server sample HTML page. Now, you need to configure the third server as a load balancing server to forward all traffic to both backend web servers.

Firstly, enable proxy modules to the load balancing server. You enable all of them with the following command.

				
					a2enmod proxy
a2enmod proxy_http
a2enmod proxy_balancer
a2enmod lbmethod_byrequests
				
			

Next, restart the Apache service to apply the changes.

				
					systemctl restart apache2
				
			

Now verify all proxy modules using the following command.

				
					apachectl -M | grep proxy
				
			

You see all modules in the following screen.

Next, create an Apache configuration file for load balancing.

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

				
			

Add the following configurations.

				
					<VirtualHost *:80>
      ServerName balancer.example.com
     <Proxy balancer://webserver>
       BalancerMember http://web1.example.com
       BalancerMember http://web2.example.com
       ProxySet stickysession=ROUTEID

      </Proxy>
      ProxyPreserveHost On
      ProxyPass / balancer://webserver/
      ProxyPassReverse / balancer://webserver/
</VirtualHost>

				
			

Save and close the file then restart the Apache service to apply the configuration.

				
					systemctl restart apache2
				
			

Verify Apache Load Balancing

At this point, your Apache load balancing server is installed and configured to forward all traffic to both backend servers. Now, its time to verify the load balancer.

Open your web browser and access the load balancer using the URL http://balancer.example.com. You should see your first backend server page.

Now wait for some time and refresh the page. This time, you should see the sample HTML page of your second backend server.

Thank you for reading Apache Load Balancing: How to Use Apache for Load Balancing. We shall conclude. 

Apache Load Balancing: How to Use Apache for Load Balancing Conclusion

In this load balancing guide, we showed you what is load balancing, and we have created two backend web servers. We have then setup a load balancing using the Apache web server to forward traffic to both backend web servers. Load Balancers (hardware and software-based) are mostly used to evenly distribute the workload across different servers. The three different kinds of load balancers are DNS Round Robin, L3/L4 (suited for the IP and TCP layers), as well as L7 Load Balancer. These help maximize server capacity, improve user experience, and save time.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x