DEV Community

Victor Okonkwo
Victor Okonkwo

Posted on

Mastering Automation: An Introduction to Server Setup Scripting

Overview

In this guide, we’ll explore how to automate server provisioning using a simple Bash script. Automating server setups helps streamline the configuration of cloud instances, saving time and reducing the risk of manual errors. This script installs and configures essential tools such as the Apache web server, Docker for container management, and Git for version control.

Script Purpose

The script automates the following tasks:

  • Updating the server: Ensures all installed packages are up to date.
  • Installing Apache: Deploys a web server to handle HTTP requests.
  • Installing Docker: Sets up Docker for containerized applications.
  • Installing Git: Installs Git to manage code repositories.

This script is perfect for cloud engineers, DevOps professionals, or anyone looking to demonstrate proficiency in automating infrastructure setups.


The Script

#!/bin/bash

# Ensure the script is run as root
if [ "$EUID" -ne 0 ]; then
  echo "Please run as root"
  exit
fi

# Update server
echo "Updating server..."
apt update && apt upgrade -y

# Install Apache
echo "Installing Apache web server..."
apt install apache2 -y

# Install Docker
echo "Installing Docker..."
apt install docker.io -y
systemctl start docker
systemctl enable docker

# Install Git
echo "Installing Git..."
apt install git -y

# Output message
echo "Server setup complete. Apache, Docker, and Git are installed."
Enter fullscreen mode Exit fullscreen mode

Explanation of Each Section

  1. Shebang (Interpreter Directive):
   #!/bin/bash
Enter fullscreen mode Exit fullscreen mode

This specifies that the script should be executed in the Bash shell. It tells the system to use /bin/bash as the interpreter for the script.

  1. Ensuring Root Privileges:
   if [ "$EUID" -ne 0 ]; then
     echo "Please run as root"
     exit
   fi
Enter fullscreen mode Exit fullscreen mode

This block checks if the script is being run as the root user. Installing system packages requires elevated permissions, so the script exits with an error message if the user is not root.

  1. Updating the Server:
   apt update && apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

This command refreshes the package list (apt update) and upgrades installed packages (apt upgrade -y). The -y flag answers "yes" to any prompts, allowing the update to run without user interaction.

  1. Installing Apache Web Server:
   apt install apache2 -y
Enter fullscreen mode Exit fullscreen mode

This installs the Apache web server. Once installed, Apache will be able to handle HTTP requests on the server. The -y flag automatically accepts the installation.

  1. Installing Docker:
   apt install docker.io -y
   systemctl start docker
   systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

These commands install Docker from the package repository and ensure Docker starts immediately (systemctl start docker) and after every server reboot (systemctl enable docker).

  1. Installing Git:
   apt install git -y
Enter fullscreen mode Exit fullscreen mode

This installs Git, a popular tool for version control, allowing you to manage code changes and collaborate with others.

  1. Final Confirmation Message:
   echo "Server setup complete. Apache, Docker, and Git are installed."
Enter fullscreen mode Exit fullscreen mode

A friendly message to inform the user that the setup has completed successfully.


How to Run the Script

Follow these steps to execute the script and automate your server setup:

1. Save the Script

Create a new file named setup_server.sh and copy the above script into the file.

2. Make the Script Executable

Before you can run the script, you need to give it executable permissions. Run the following command:

chmod +x setup_server.sh
Enter fullscreen mode Exit fullscreen mode

3. Run the Script

To execute the script, use the following command with sudo (to ensure root privileges):

sudo ./setup_server.sh
Enter fullscreen mode Exit fullscreen mode

4. Observe the Output

As the script runs, it will display messages at each step—updating the server, installing Apache, Docker, and Git—and will conclude with:

Server setup complete. Apache, Docker, and Git are installed.
Enter fullscreen mode Exit fullscreen mode

automated


Why Automate Server Setup?

Automating server setups improves efficiency, consistency, and security by eliminating human error and ensuring that every server is configured exactly the same way. With this script, anyone can spin up a new cloud server and have it ready with essential services in a matter of minutes.


Conclusion

This script demonstrates the power of automation in managing cloud infrastructure. By following this guide, you’ll master the basics of automating server provisioning, a crucial skill for DevOps and Cloud engineers.

Top comments (0)