Published January 20, 2018 by

Create a Simple Load Balancer in Apache Web Server

This article described how to create load balancer in Apache for your website or application server.

Prerequisites

Hardware

  • 1 Main load balancer server
  • 2 Web application server

Software

  • Apache Web Server on All 3 Server

Install Apache

Install Apache on all 3 servers using the following command:

# sudo yum update -y
# sudo yum install httpd -y
# sudo systemctl start httpd.service
# sudo systemctl enable httpd.service

After installing apache you can run an application on your both web server.

Server Configuration

Now create a load balancer we edit main load balancer server's apache virtual host file /etc/httpd/conf/httpd.conf

# vim /etc/httpd/conf/httpd.conf

Insert following code into your httpd.conf file. And replace highlighted data:

<VirtualHost *:80>
        ProxyRequests off
        ServerName serverkaka.com

        <Proxy balancer://mycluster>
                # WebServer1
                BalancerMember http://54.208.62.105:80 loadfactor=1
                # WebServer2
                BalancerMember http://52.290.87.147:80 loadfactor=2

                # In this example all requests are allowed.
                Require all granted
                
                # Load Balancer Settings
                ProxySet lbmethod=byrequests
        </Proxy>

        # balancer-manager
        <Location /balancer-manager>
                SetHandler balancer-manager
                Require host example.org
        </Location>

        # Point of Balance
        ProxyPass /balancer-manager !
        ProxyPass / balancer://mycluster/

</VirtualHost>

Restart Apache Server

# sudo systemctl restart httpd.service

Now test your load balancer using the main server_IP or domain name.