Logo

dev-resources.site

for different kinds of informations.

Final Bash Script Series Mastering Remote Server Management and Web App Deployment

Published at
1/4/2025
Categories
bash
automation
shell
linux
Author
jamiu_cloud
Categories
4 categories in total
bash
open
automation
open
shell
open
linux
open
Author
11 person written this
jamiu_cloud
open
Final Bash Script Series Mastering Remote Server Management and Web App Deployment

In the world of cloud engineering, managing remote servers and deploying web applications are essential skills. Today, I’m sharing the thrilling final day of the 7-Day Bash Scripting Challenge, where we tackle a real-world scenario: securely managing servers and deploying a web app using Docker and Nginx. Here's a simplified breakdown of the process.


The Story

Imagine you’re part of a tech team at CodeCrafters Inc., managing three remote servers: Alpha, Beta, and Gamma. These servers need to communicate securely, and you must deploy a web app across two of them. The challenge? Automate it all using Bash scripts, Docker, and Nginx.


The Mission

  1. Create a Secure Server Network

    First, we generate SSH keys to set up passwordless authentication between the servers. This allows the main server (Alpha) to control Beta and Gamma securely.

  2. Remote Management with Bash Scripts

    Using Bash scripting, we automate tasks like running commands on remote servers and securely transferring files.

  3. Web App Deployment with Docker and Nginx

    Finally, we containerize a simple web app using Docker and deploy it on the Beta and Gamma servers, using Nginx to serve the application.


Step-by-Step Guide

Step 1: Setup Secure Server Communication

We start by creating passwordless SSH connections between the servers. This removes the need to repeatedly type passwords when running remote commands or transferring files.

  • Generate SSH keys on the main server:
  ssh-keygen -t rsa
Enter fullscreen mode Exit fullscreen mode
  • Share the key with the other servers:
  ssh-copy-id [email protected]
  ssh-copy-id [email protected]
Enter fullscreen mode Exit fullscreen mode

Step 2: Automate Remote Commands

Next, we write a script to remotely execute commands on Beta and Gamma from the Alpha server:

#!/bin/bash
clients=("192.168.1.101" "192.168.1.102")
for client in "${clients[@]}"; do
    ssh user@$client "$@"
done
Enter fullscreen mode Exit fullscreen mode

This lets us automate tasks like checking system uptime or installing software.

Step 3: Secure File Transfers

We also need to share files between servers. Another script makes this seamless:

#!/bin/bash
file_to_transfer=$1
clients=("192.168.1.101" "192.168.1.102")
for client in "${clients[@]}"; do
    scp $file_to_transfer user@$client:/home/user/
done
Enter fullscreen mode Exit fullscreen mode

You can now transfer your web app files or Docker images with a single command!

Step 4: Build and Containerize the Web App

We create a simple web app and use Docker to package it:

  • Write an HTML file:
  <h1>Welcome to Bash Blaze Day 7 Challenge!</h1>
Enter fullscreen mode Exit fullscreen mode
  • Create a Dockerfile:
  FROM nginx:alpine
  COPY ./app /usr/share/nginx/html
Enter fullscreen mode Exit fullscreen mode
  • Build and save the Docker image:
  docker build -t webapp:v1 .
  docker save webapp:v1 > webapp.tar
Enter fullscreen mode Exit fullscreen mode

Step 5: Deploy the Web App

Transfer the Docker image to Beta and Gamma and run it using this script:

#!/bin/bash
docker load < /home/user/webapp.tar
docker run -d --name webapp -p 80:80 webapp:v1
Enter fullscreen mode Exit fullscreen mode

Once deployed, the web app is live on both servers!

Step 6: Validate

Visit the servers in your browser:

  • http://192.168.1.101
  • http://192.168.1.102 You should see your web app up and running!

Why This Matters

This challenge demonstrates the power of Bash scripting in automating routine cloud engineering tasks. From managing secure server communication to deploying scalable applications, you’ve seen how simple scripts can drive efficiency and reliability in remote server management.


πŸ’‘ Stay tuned for more updates!

References

Let’s connect and share ideas!

shell Article's
30 articles in total
Favicon
Poor man's parallel in Bash
Favicon
Ergonomic Pyhon Text Piping Solution for Linux Shell with pypyp and uv
Favicon
Become a Bash Scripting Pro in 10 Minutes: A Quick Guide for Beginners
Favicon
Final Bash Script Series Mastering Remote Server Management and Web App Deployment
Favicon
kkTerminal β€”β€” A terminal for Web SSH connection
Favicon
The Complete Guide to Bash Commands
Favicon
Navigating TC39 Proposals: From Error Handling to Iterator.range
Favicon
Introducing TheShell: A Game-Changer in LivinGrimoire
Favicon
Pick Files from a List for Git Add and Stash Directly in Your Terminal
Favicon
Start Shell Programming: A Beginner's Guide βš™ [Part-I]
Favicon
Pytest Fish shell autocompletion
Favicon
Discover File Splitter & Merger: A Revolutionary Tool for Managing Large Files
Favicon
πŸš€ RazzShell v1.0.1 is Here: Plugin Support, Enhanced Job Management, and More! 🌟
Favicon
ps, kill -9 PID
Favicon
\\wsl$
Favicon
Escape quotes correctly when using psql via docker in bash
Favicon
Bash vs. Shell: The Ultimate Comparison
Favicon
Search and Sync Your Shell History With Atuin
Favicon
Building a (somewhat) intelligent agent
Favicon
Environment Management in Bash: Unlocking the Secrets of the Shell
Favicon
3 Must-Know File Permissions and Ownership Commands
Favicon
Ask Git to Show a Method
Favicon
UNIX
Favicon
DEV OPS JOURNEY
Favicon
Unlock the Secrets of Your Command Line with the History Command
Favicon
Mastering Text Processing with Grep, Sed, Awk, Cut, and Sort
Favicon
Shell Special Variables and Execution Environment
Favicon
Spice up Your Terminal With a Todo Reminder Using Starship Prompt and iZiDo Bash Script
Favicon
Introducing RazzShell: A Customizable Unix Shell for Modern CLI Users
Favicon
File Management in Bash : Commands and Examples

Featured ones: