{"id":19327841,"url":"https://github.com/ekedonald/automating-load-balancer-configuration-with-shell-scripting","last_synced_at":"2026-06-17T19:31:34.706Z","repository":{"id":204118658,"uuid":"710100806","full_name":"ekedonald/Automating-Load-Balancer-Configuration-with-Shell-Scripting","owner":"ekedonald","description":"A comprehensive guide on how to Automate a Load Balancer Configuration to distribute traffic between 2 Web Servers with Shell Scripting.","archived":false,"fork":false,"pushed_at":"2023-10-30T09:30:47.000Z","size":8964,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-24T06:33:37.907Z","etag":null,"topics":["apache2","ec2-instance","load-balancer","nginx","shell-scripting"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ekedonald.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-26T02:49:38.000Z","updated_at":"2023-10-30T10:20:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"2cdc97c4-dd1e-4b46-949a-add21df3e841","html_url":"https://github.com/ekedonald/Automating-Load-Balancer-Configuration-with-Shell-Scripting","commit_stats":null,"previous_names":["ekedonald/automating-load-balancer-configuration-with-shell-scripting"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ekedonald/Automating-Load-Balancer-Configuration-with-Shell-Scripting","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FAutomating-Load-Balancer-Configuration-with-Shell-Scripting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FAutomating-Load-Balancer-Configuration-with-Shell-Scripting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FAutomating-Load-Balancer-Configuration-with-Shell-Scripting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FAutomating-Load-Balancer-Configuration-with-Shell-Scripting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ekedonald","download_url":"https://codeload.github.com/ekedonald/Automating-Load-Balancer-Configuration-with-Shell-Scripting/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekedonald%2FAutomating-Load-Balancer-Configuration-with-Shell-Scripting/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34463551,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-17T02:00:05.408Z","response_time":127,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apache2","ec2-instance","load-balancer","nginx","shell-scripting"],"created_at":"2024-11-10T02:19:07.663Z","updated_at":"2026-06-17T19:31:34.689Z","avatar_url":"https://github.com/ekedonald.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Automating Load Balancer Configuration With Shell Scripting\nThe following steps are taken to automate a Load Balancer configuration to distribute traffic between 2 Web Servers with shell scripting:\n\n### Step 1: Provision an EC2 Instance for the 1st Web Server\nUse the following parameters when configuring the EC2 Instance:\n* Name of Instance: Web Server 1\n* AMI: Ubuntu Server 22.04 LTS (HVM), SSD Volume Type\n* New Key Pair Name: web11\n* Key Pair Type: RSA\n* Private Key File Format: .pem\n* New Security Group: Apache Web Server Security Group\n* Inbound Rules: Allow Traffic From Anywhere On Port 8000 and Port 22.\n\n![web server 1](./images/1.%20webserver%201.png)\n_Instance Summary for Web Server 1_\n\n### Step 2: Connect to the 1st Web Server via the terminal using SSH\n* Open the terminal on your computer.\n* Run the following command to go to the directory (i.e. Downloads) where the `.pem` key pair file was downloaded.\n\n```sh\ncd Downloads\n```\n\n* Paste the following command to give read permissions to the `.pem` key pair file:\n\n```sh\nsudo chmod 400 \u003cprivate-key-pair-name\u003e.pem\n```\n\n![chmod key pair](./images/1.%20chmod%20key%20pair.png)\n\n* SSH into the **Web Server 1 Instance** using the command shown below:\n\n```sh\nssh -i \u003cprivate-key-name\u003e.pem ubuntu@\u003cPublic-IP-address\u003e\n```\n\n![ssh web server 1](./images/1.%20ssh%20webserver%201.png)\n\n### Step 3: Deployment of the 1st Web Server\n* Create and open a file `install.sh` using the command shown below:\n\n```sh\nsudo vi install.sh\n```\n\n* Paste the script shown below into the file then save and exit the file:\n\n```sh\n#!/bin/bash\n\n####################################################################################################################\n##### This automates the installation and configuring of apache webserver to listen on port 8000\n##### Usage: Call the script and pass in the Public_IP of your EC2 instance as the first argument as shown below:\n######## ./install_configure_apache.sh 18.234.230.94\n####################################################################################################################\n\nset -x # debug mode\nset -e # exit the script if there is an error\nset -o pipefail # exit the script when there is a pipe failure\n\nPUBLIC_IP=18.234.230.94\n\n[ -z \"${PUBLIC_IP}\" ] \u0026\u0026 echo \"Please pass the public IP of your EC2 instance as an argument to the script\" \u0026\u0026 exit 1\n\nsudo apt update -y \u0026\u0026  sudo apt install apache2 -y\n\nsudo systemctl status apache2\n\nif [[ $? -eq 0 ]]; then\n    sudo chmod 777 /etc/apache2/ports.conf\n    echo \"Listen 8000\" \u003e\u003e /etc/apache2/ports.conf\n    sudo chmod 777 -R /etc/apache2/\n\n    sudo sed -i 's/\u003cVirtualHost \\*:80\u003e/\u003cVirtualHost *:8000\u003e/' /etc/apache2/sites-available/000-default.conf\n\nfi\nsudo chmod 777 -R /var/www/\necho \"\u003c!DOCTYPE html\u003e\n        \u003chtml\u003e\n        \u003chead\u003e\n            \u003ctitle\u003eMy EC2 Instance\u003c/title\u003e\n        \u003c/head\u003e\n        \u003cbody\u003e\n            \u003ch1\u003eWelcome to my 1st Web Server EC2 instance\u003c/h1\u003e\n            \u003cp\u003ePublic IP: \"${PUBLIC_IP}\"\u003c/p\u003e\n        \u003c/body\u003e\n        \u003c/html\u003e\" \u003e /var/www/html/index.html\n\nsudo systemctl restart apache2\n```\n\n![web server 1 script](./images/1.%20webserver_1_script.png)\n\n* Assign executable permissions on the file using the command shown below:\n\n```sh\nsudo chmod +x install.sh\n```\n\n![sudo chmod install.sh](./images/1.%20chmod%20install_sh.png)\n\n* Run the shell script using the command shown below:\n\n```sh\n./install.sh\n```\n\n![run web server 1 script](./images/1.%20run%20webserver_1%20script.png)\n\n* Go to your web browser and paste the following URL to verify the setup:\n\n```sh\nhttp://public_ip_address_of_web_server_1:8000\n```\n\n![http public ip web server 1](./images/1.%20http_ip_webserver_1.png)\n\n### Step 4: Provision an EC2 Instance for the 2nd Web Server\nUse the following parameters when configuring the EC2 Instance:\n* Name of Instance: Web Server 2\n* AMI: Ubuntu Server 22.04 LTS (HVM), SSD Volume Type\n* Key Pair Name: web11\n* Security Group: Apache Server Security Group\n\n![web server 2](./images/2.%20webserver%202.png)\n\n_Instance Summary for Web Server 2_\n\n### Step 5: Connect to the 2nd Web Server via the terminal using SSH\n* Open the terminal on your computer.\n* Run the following command to go to the directory (i.e. Downloads) where the `.pem` key pair file was downloaded.\n\n```sh\ncd Downloads\n```\n\n* SSH into the **Web Server 2 Instance** using the command shown below:\n\n```sh\nssh -i \u003cprivate-key-name\u003e.pem ubuntu@\u003cPublic-IP-address\u003e\n```\n\n![ssh web server 2](./images/2.%20ssh%20webserver%202.png)\n\n### Step 6: Deployment of the 2nd Web Server\n* Create and open a file `install.sh` using the command shown below:\n\n```sh\nsudo vi install.sh\n```\n* Paste the script shown below into the file then save and exit the file:\n\n```sh\n#!/bin/bash\n\n####################################################################################################################\n##### This automates the installation and configuring of apache webserver to listen on port 8000\n##### Usage: Call the script and pass in the Public_IP of your EC2 instance as the first argument as shown below:\n######## ./install_configure_apache.sh 18.234.230.94\n####################################################################################################################\n\nset -x # debug mode\nset -e # exit the script if there is an error\nset -o pipefail # exit the script when there is a pipe failure\n\nPUBLIC_IP=54.175.35.61\n\n[ -z \"${PUBLIC_IP}\" ] \u0026\u0026 echo \"Please pass the public IP of your EC2 instance as an argument to the script\" \u0026\u0026 exit 1\n\nsudo apt update -y \u0026\u0026  sudo apt install apache2 -y\n\nsudo systemctl status apache2\n\nif [[ $? -eq 0 ]]; then\n    sudo chmod 777 /etc/apache2/ports.conf\n    echo \"Listen 8000\" \u003e\u003e /etc/apache2/ports.conf\n    sudo chmod 777 -R /etc/apache2/\n\n    sudo sed -i 's/\u003cVirtualHost \\*:80\u003e/\u003cVirtualHost *:8000\u003e/' /etc/apache2/sites-available/000-default.conf\n\nfi\nsudo chmod 777 -R /var/www/\necho \"\u003c!DOCTYPE html\u003e\n        \u003chtml\u003e\n        \u003chead\u003e\n            \u003ctitle\u003eMy EC2 Instance\u003c/title\u003e\n        \u003c/head\u003e\n        \u003cbody\u003e\n            \u003ch1\u003eWelcome to my 2nd Web Server EC2 instance\u003c/h1\u003e\n            \u003cp\u003ePublic IP: \"${PUBLIC_IP}\"\u003c/p\u003e\n        \u003c/body\u003e\n        \u003c/html\u003e\" \u003e /var/www/html/index.html\n\nsudo systemctl restart apache2\n```\n\n![web server 2 script](./images/2.%20webserver_2_script.png)\n\n* Assign executable permissions on the file using the command shown below:\n\n```sh\nsudo chmod +x install.sh\n```\n\n![chmod install.sh](./images/2.%20chmod%20install_sh.png)\n\n* Run the shell script using the command shown below:\n\n```sh\n./install.sh\n```\n\n![run web server 2 script](./images/2.%20run%20webserver_2%20script.png)\n\n* Go to your web browser and paste the following URL to verify the setup:\n\n```sh\nhttp://public_ip_address_of_web_server_2:8000\n```\n\n![http web server 2](./images/2.%20http_ip_webserver_2.png)\n\n### Step 8: Provision an EC2 Instance for the Load Balancer\nUse the following parameters when configuring the EC2 Instance:\n* Name of Instance: Load Balancer\n* AMI: Ubuntu Server 22.04 LTS (HVM), SSD Volume Type\n* New Key Pair Name: web11\n* New Security Group With Inbound Rules: Allow Traffic From Anywhere On Port 80 and Port 22.\n\n![load balancer](./images/3.%20load%20balancer.png)\n\n_Instance Summary for Load Balancer_\n\n### Step 9: Connect to the Load Balancer via the terminal using SSH\n* Open the terminal on your computer.\n* Run the following command to go to the directory (i.e. Downloads) where the `.pem` key pair file was downloaded.\n\n```sh\ncd Downloads\n```\n\n* SSH into the **Load Balancer Instance** using the command shown below:\n\n```sh\nssh -i \u003cprivate-key-name\u003e.pem ubuntu@\u003cPublic-IP-address\u003e\n```\n\n![ssh load balancer](./images/3.%20ssh%20loadbalancer.png)\n\n### Step 10: Deploying and Configuring Nginx Load Balancer\n* Create and open a file `nginx.sh` using the command shown below:\n\n```sh\nsudo vi nginx.sh\n```\n\n* Paste the script shown below into the file then save and exit the file:\n\n```sh\n#!/bin/bash\n\n######################################################################################################################\n##### This automates the configuration of Nginx to act as a load balancer\n##### Usage: The script is called with 3 command line arguments. The public IP of the EC2 instance where Nginx is installed\n##### the webserver urls for which the load balancer distributes traffic. An example of how to call the script is shown below:\n##### ./configure_nginx_loadbalancer.sh PUBLIC_IP Webserver-1 Webserver-2\n#####  ./configure_nginx_loadbalancer.sh 54.90.83.129 18.234.230.94:8000 54.175.35.61:8000\n############################################################################################################# \n\nPUBLIC_IP=54.90.83.129\nfirstWebserver=18.234.230.94:8000\nsecondWebserver=54.175.35.61:8000\n\n[ -z \"${PUBLIC_IP}\" ] \u0026\u0026 echo \"Please pass the Public IP of your EC2 instance as the argument to the script\" \u0026\u0026 exit 1\n\n[ -z \"${firstWebserver}\" ] \u0026\u0026 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\" \u0026\u0026 exit 1\n\n[ -z \"${secondWebserver}\" ] \u0026\u0026 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\" \u0026\u0026 exit 1\n\nset -x # debug mode\nset -e # exit the script if there is an error\nset -o pipefail # exit the script when there is a pipe failure\n\n\nsudo apt update -y \u0026\u0026 sudo apt install nginx -y\nsudo systemctl status nginx\n\nif [[ $? -eq 0 ]]; then\n    sudo touch /etc/nginx/conf.d/loadbalancer.conf\n\n    sudo chmod 777 /etc/nginx/conf.d/loadbalancer.conf\n    sudo chmod 777 -R /etc/nginx/\n\n    \n    echo \" upstream backend_servers {\n\n            # your are to replace the public IP and Port to that of your webservers\n            server  \"${firstWebserver}\"; # public IP and port for webserver 1\n            server \"${secondWebserver}\"; # public IP and port for webserver 2\n\n            }\n\n           server {\n            listen 80;\n            server_name \"${PUBLIC_IP}\";\n\n            location / {\n                proxy_pass http://backend_servers;   \n            }\n    } \" \u003e /etc/nginx/conf.d/loadbalancer.conf\nfi\n\nsudo nginx -t\n\nsudo systemctl restart nginx\n```\n\n![load balancer script](./images/3.%20loadbalancer_script.png)\n\n* Assign executable permissions on the file using the command shown below:\n\n```sh\nsudo chmod +x nginx.sh\n```\n\n![chmod nginx.sh](./images/3.%20chmod%20nginx_sh.png)\n\n* Run the shell script using the command shown below:\n\n```sh\n./nginx.sh\n```\n\n![run load balncer script](./images/3.%20run%20loadbalancer_script.png)\n\n* Go to your web browser and paste the following URL to verify the setup:\n\n```sh\nhttp://public_ip_address_of_load_balancer:80\n```\n\n![http ip load balancer1](./images/3.%20http%20ip1.png)\n\n![http ip load balancer2](./images/3.%20http%20ip2.png)\n\n_Each time you refresh the page, you will see the web page of **Web Server 1** and **Web Server 2** as shown above._","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekedonald%2Fautomating-load-balancer-configuration-with-shell-scripting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fekedonald%2Fautomating-load-balancer-configuration-with-shell-scripting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekedonald%2Fautomating-load-balancer-configuration-with-shell-scripting/lists"}