How to Configure Load Balancer in Apache

To configure Load Balancer in Apache Webserver

Load Balancer manages the high network traffic in web services by distributing the workload. It is especially used to utilize the resources, avoid overload and maximize throughput. The Load Balancer can be configured in Apache web-server itself. This article explains the configuration of Load Balancer in Apache.

 

 

Testing Environment

Load Balancer:

IP Address – 192.168.5.157
Hostname - lb.example.com

WebServer 1:

IP Address – 192.168.5.158
Hostname - ws1.example.com

WebServer 2:

IP Address – 192.168.5.159
Hostname – ws2.example.com

Create some entry in the file /etc/hosts in all the three machines for hostname resolution.

Entry:

192.168.5.157 lb.example.com lb
192.168.5.158 ws1.example.com ws1
192.168.5.159 ws2.example.com ws2

Note: The Host file entry is not required, if you have DNS Server.


To configure Load Balancer in Apache

Here we have one load balancer and two web servers. First you need to configure the load balancer and then web servers. Then run the following steps on both the webservers.


To install Apache

Utilize the below command to install Apache package on both web servers.

[root@ws1 ~]# yum install httpd -y
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:01
updates | 3.4 kB 00:00:00
(1/2): extras/7/x86_64/primary_db | 166 kB 00:00:01
(2/2): updates/7/x86_64/primary_db | 9.1 MB 00:00:27
Determining fastest mirrors
* base: centos.excellmedia.net
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
.
.
.
httpd.x86_64 0:2.4.6-40.el7.centos.4

Dependency Installed:
apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 mailcap.noarch 0:2.1.41-2.el7

Complete!

Once the Apache is installed, start the service and configure firewall.

[root@ws1 ~]# systemctl start httpd
[root@ws1 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@ws1 ~]# firewall-cmd --permanent --add-service=http
success
[root@ws1 ~]# firewall-cmd –reload
success

Now place the default file 'index.html' for the web servers and enter any content to that file.

[root@ws1 ~]# vim /var/www/html/index.html


To configure Load Balancer

Run the following command to install the Apache package on load balancer machine.

[root@lb ~]# yum install httpd -y
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): extras/7/x86_64/primary_db | 166 kB 00:00:03
(2/2): updates/7/x86_64/primary_db | 9.1 MB 00:00:30
Determining fastest mirrors
* base: centos.excellmedia.net
.
.
.
Installed:
httpd.x86_64 0:2.4.6-40.el7.centos.4

Dependency Installed:
apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-40.el7.centos.4 mailcap.noarch 0:2.1.41-2.el7

Complete!

Once the Apache is installed in Load balancer, start the service and configure the firewall.

[root@lb ~]# systemctl start httpd
[root@lb ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@lb ~]# firewall-cmd --permanent --add-service=http
success
[root@lb ~]# firewall-cmd --reload
Success

Now its time to create new Apache configuration file in the 'conf.d' directory.

[root@lb ~]# cd /etc/httpd/conf.d
[root@lb conf.d]# vim lb.conf

Entry:

ProxyRequests off
BalancerMember http://192.168.5.158:80
BalancerMember http://192.168.5.159:80
ProxySet lbmethod=byrequests
SetHandler balancer-manager

ProxyPass /balancer-manager !
ProxyPass / balancer://linuxhelp/

Replace the content with your webserver's IP address and Load balancer name  as linuxhelp.

Restart the apache service with the following command.

[root@lb ~]# systemctl restart httpd

Finally open the browser and point to load balancer IP address. It displays the content from any of your configured web server.

Load-Balancer-open

 


Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website www.linuxhelp.com
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Apache Load balancing Web Server Load balancer
user image Author :  Cameron