The following steps are taken to automate a Load Balancer configuration to distribute traffic between 2 Web Servers with shell scripting:
Use the following parameters when configuring the EC2 Instance:
- Name of Instance: Web Server 1
- AMI: Ubuntu Server 22.04 LTS (HVM), SSD Volume Type
- New Key Pair Name: web11
- Key Pair Type: RSA
- Private Key File Format: .pem
- New Security Group: Apache Web Server Security Group
- Inbound Rules: Allow Traffic From Anywhere On Port 8000 and Port 22.
Instance Summary for Web Server 1
- Open the terminal on your computer.
- Run the following command to go to the directory (i.e. Downloads) where the
.pemkey pair file was downloaded.
cd Downloads- Paste the following command to give read permissions to the
.pemkey pair file:
sudo chmod 400 <private-key-pair-name>.pem- SSH into the Web Server 1 Instance using the command shown below:
ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>- Create and open a file
install.shusing the command shown below:
sudo vi install.sh- Paste the script shown below into the file then save and exit the file:
#!/bin/bash
####################################################################################################################
##### This automates the installation and configuring of apache webserver to listen on port 8000
##### Usage: Call the script and pass in the Public_IP of your EC2 instance as the first argument as shown below:
######## ./install_configure_apache.sh 18.234.230.94
####################################################################################################################
set -x # debug mode
set -e # exit the script if there is an error
set -o pipefail # exit the script when there is a pipe failure
PUBLIC_IP=18.234.230.94
[ -z "${PUBLIC_IP}" ] && echo "Please pass the public IP of your EC2 instance as an argument to the script" && exit 1
sudo apt update -y && sudo apt install apache2 -y
sudo systemctl status apache2
if [[ $? -eq 0 ]]; then
sudo chmod 777 /etc/apache2/ports.conf
echo "Listen 8000" >> /etc/apache2/ports.conf
sudo chmod 777 -R /etc/apache2/
sudo sed -i 's/<VirtualHost \*:80>/<VirtualHost *:8000>/' /etc/apache2/sites-available/000-default.conf
fi
sudo chmod 777 -R /var/www/
echo "<!DOCTYPE html>
<html>
<head>
<title>My EC2 Instance</title>
</head>
<body>
<h1>Welcome to my 1st Web Server EC2 instance</h1>
<p>Public IP: "${PUBLIC_IP}"</p>
</body>
</html>" > /var/www/html/index.html
sudo systemctl restart apache2- Assign executable permissions on the file using the command shown below:
sudo chmod +x install.sh- Run the shell script using the command shown below:
./install.sh- Go to your web browser and paste the following URL to verify the setup:
http://public_ip_address_of_web_server_1:8000Use the following parameters when configuring the EC2 Instance:
- Name of Instance: Web Server 2
- AMI: Ubuntu Server 22.04 LTS (HVM), SSD Volume Type
- Key Pair Name: web11
- Security Group: Apache Server Security Group
Instance Summary for Web Server 2
- Open the terminal on your computer.
- Run the following command to go to the directory (i.e. Downloads) where the
.pemkey pair file was downloaded.
cd Downloads- SSH into the Web Server 2 Instance using the command shown below:
ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>- Create and open a file
install.shusing the command shown below:
sudo vi install.sh- Paste the script shown below into the file then save and exit the file:
#!/bin/bash
####################################################################################################################
##### This automates the installation and configuring of apache webserver to listen on port 8000
##### Usage: Call the script and pass in the Public_IP of your EC2 instance as the first argument as shown below:
######## ./install_configure_apache.sh 18.234.230.94
####################################################################################################################
set -x # debug mode
set -e # exit the script if there is an error
set -o pipefail # exit the script when there is a pipe failure
PUBLIC_IP=54.175.35.61
[ -z "${PUBLIC_IP}" ] && echo "Please pass the public IP of your EC2 instance as an argument to the script" && exit 1
sudo apt update -y && sudo apt install apache2 -y
sudo systemctl status apache2
if [[ $? -eq 0 ]]; then
sudo chmod 777 /etc/apache2/ports.conf
echo "Listen 8000" >> /etc/apache2/ports.conf
sudo chmod 777 -R /etc/apache2/
sudo sed -i 's/<VirtualHost \*:80>/<VirtualHost *:8000>/' /etc/apache2/sites-available/000-default.conf
fi
sudo chmod 777 -R /var/www/
echo "<!DOCTYPE html>
<html>
<head>
<title>My EC2 Instance</title>
</head>
<body>
<h1>Welcome to my 2nd Web Server EC2 instance</h1>
<p>Public IP: "${PUBLIC_IP}"</p>
</body>
</html>" > /var/www/html/index.html
sudo systemctl restart apache2- Assign executable permissions on the file using the command shown below:
sudo chmod +x install.sh- Run the shell script using the command shown below:
./install.sh- Go to your web browser and paste the following URL to verify the setup:
http://public_ip_address_of_web_server_2:8000Use the following parameters when configuring the EC2 Instance:
- Name of Instance: Load Balancer
- AMI: Ubuntu Server 22.04 LTS (HVM), SSD Volume Type
- New Key Pair Name: web11
- New Security Group With Inbound Rules: Allow Traffic From Anywhere On Port 80 and Port 22.
Instance Summary for Load Balancer
- Open the terminal on your computer.
- Run the following command to go to the directory (i.e. Downloads) where the
.pemkey pair file was downloaded.
cd Downloads- SSH into the Load Balancer Instance using the command shown below:
ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>- Create and open a file
nginx.shusing the command shown below:
sudo vi nginx.sh- Paste the script shown below into the file then save and exit the file:
#!/bin/bash
######################################################################################################################
##### This automates the configuration of Nginx to act as a load balancer
##### Usage: The script is called with 3 command line arguments. The public IP of the EC2 instance where Nginx is installed
##### the webserver urls for which the load balancer distributes traffic. An example of how to call the script is shown below:
##### ./configure_nginx_loadbalancer.sh PUBLIC_IP Webserver-1 Webserver-2
##### ./configure_nginx_loadbalancer.sh 54.90.83.129 18.234.230.94:8000 54.175.35.61:8000
#############################################################################################################
PUBLIC_IP=54.90.83.129
firstWebserver=18.234.230.94:8000
secondWebserver=54.175.35.61:8000
[ -z "${PUBLIC_IP}" ] && echo "Please pass the Public IP of your EC2 instance as the argument to the script" && exit 1
[ -z "${firstWebserver}" ] && echo "Please pass the Public IP together with its port number in this format: 127.0.0.1:8000 as the second argument to the script" && exit 1
[ -z "${secondWebserver}" ] && echo "Please pass the Public IP together with its port number in this format: 127.0.0.1:8000 as the third argument to the script" && exit 1
set -x # debug mode
set -e # exit the script if there is an error
set -o pipefail # exit the script when there is a pipe failure
sudo apt update -y && sudo apt install nginx -y
sudo systemctl status nginx
if [[ $? -eq 0 ]]; then
sudo touch /etc/nginx/conf.d/loadbalancer.conf
sudo chmod 777 /etc/nginx/conf.d/loadbalancer.conf
sudo chmod 777 -R /etc/nginx/
echo " upstream backend_servers {
# your are to replace the public IP and Port to that of your webservers
server "${firstWebserver}"; # public IP and port for webserver 1
server "${secondWebserver}"; # public IP and port for webserver 2
}
server {
listen 80;
server_name "${PUBLIC_IP}";
location / {
proxy_pass http://backend_servers;
}
} " > /etc/nginx/conf.d/loadbalancer.conf
fi
sudo nginx -t
sudo systemctl restart nginx- Assign executable permissions on the file using the command shown below:
sudo chmod +x nginx.sh- Run the shell script using the command shown below:
./nginx.sh- Go to your web browser and paste the following URL to verify the setup:
http://public_ip_address_of_load_balancer:80Each time you refresh the page, you will see the web page of Web Server 1 and Web Server 2 as shown above.


















