Configuring Apache as a Loadbalancer
A new EC2 instance running Ubuntu Server 20.04 was created for this Load Balancer.  Below is the snapshot of the available instance showing the Webservers, the NFS Server and the Database server:
Port 80 was opened on the EC2 Instance security group as shown below:
Next is the installation of Apache Load Balancer.
sudo apt update
sudo apt install apache2 -y
sudo apt-get install libxml2-devAfter the installation, the following commands were run to enable some modules:
sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod headers
sudo a2enmod lbmethod_byrequestsApache2 service was restarted and confirmed running:
sudo systemctl restart apache2
sudo systemctl status apache2Next is the Configuration of the load balancer:
sudo vi /etc/apache2/sites-available/000-default.confThe config entries is edited as shown below:
<Proxy "balancer://mycluster">
               BalancerMember http://172.31.33.120:80 loadfactor=5 timeout=1
               BalancerMember http://172.31.45.154:80 loadfactor=5 timeout=1
               ProxySet lbmethod=byrequests
        </Proxy>
        ProxyPreserveHost On
        ProxyPass / balancer://mycluster/
        ProxyPassReverse / balancer://mycluster/Then the apache2 service is restarted:
sudo systemctl restart apache2Before testing, we needed to be able to log each webserver access in its own log, so we unmounted the log directory from the NFS as confirmed below:
sudo umount  172.31.43.227:/mnt/logs /var/log/httpdTo test our load balancer, we open the public IP from the web browser multiple times as follow:
http://ec2-3-137-204-64.us-east-2.compute.amazonaws.com/index.php