DEV Community

Alex Abiola
Alex Abiola

Posted on

Deploying a Website Using EC2 User Data with Bash Scripting

Image description

Hello there, Alex here again!

Deploying a website using Amazon EC2 User Data with Bash scripting is an efficient way to automate the setup and configuration of your server during launch. By embedding a Bash script in the EC2 instance's User Data field, you can install necessary packages, configure software, and deploy your website seamlessly without manual intervention. This approach saves time, ensures consistency, and makes scaling infrastructure easier, especially in dynamic environments.

To deploy a website using Amazon EC2 User Data with Bash scripting, you’ll need the following tools:

  1. AWS Account with IAM Role/Permissions
  2. Text Editor/IDE: To write your Bash script (e.g., Visual Studio Code, Sublime Text, or any editor of your choice).

Step 1: Launching an EC2 Instance

A. Go to the AWS Management Console.

B. Navigate to the EC2 Dashboard: Search for "EC2" in the services search bar and select it.

Image description

C. Launch a New Instance
On the EC2 Dashboard, click the Launch Instance button to start the process.

Image description

D. Give your instance a name unique to the project you are currently working on

Image description

E. Choose an Amazon Machine Image (AMI)

  • Select an Amazon Machine Image (AMI). AMIs are pre-configured templates for your instance. Common options include Amazon Linux 2, Ubuntu, or Windows.

  • Click on the Select button for the AMI of your choice.

Image description

Image description

F. Choose an Instance Type

  • Select the instance type based on the required CPU, memory, storage, and network capacity. For most basic use cases, the t2.micro instance type is often sufficient (and is eligible for the free tier).

  • Click Next: Configure Instance Details.

Image description

G. Configuring the key pair, you can choose from the previous ones or create a new one.

Image description

H. Configure Security Group

Create a new security group or select an existing one. A security group acts as a firewall for your instance, controlling inbound and outbound traffic.

For example, you can add a rule to allow SSH access (for Linux).
I added a rule for both HTTP and HTTPS to allow inbound connncetion from anywhere

Image description

Image description

I. Configure user data for automation

#!/bin/bash
echo "Updating and installing nginx..."
apt update -y &> /dev/null
apt install nginx -y &> output.txt
apt install unzip -y
rm -rf /var/www/html/*
wget https://github.com/startbootstrap/startbootstrap-agency/archive/gh-pages.zip
unzip gh-pages.zip &>/dev/null
mv startbootstrap-agency-gh-pages/* /var/www/html
sudo chmod +x /var/www/html/index.html
echo "setup completed"
sleep 3
echo "check your ip on the browser to view result"

Enter fullscreen mode Exit fullscreen mode

Image description

This Bash script automates the process of setting up a basic web server using Nginx and deploying a static website. Here's a breakdown of what each line does:

1. Updating and Installing Nginx

   echo "Updating and installing nginx..."
   apt update -y &> /dev/null
   apt install nginx -y &> output.txt
Enter fullscreen mode Exit fullscreen mode
  • apt update -y updates the list of available packages.
  • apt install nginx -y installs the Nginx web server. The -y flag automatically confirms the installation.
  • The &> /dev/null redirects the output of apt update to /dev/null, effectively suppressing the output. The installation logs are redirected to a file named output.txt.

2. Installing Unzip Utility

   apt install unzip -y
Enter fullscreen mode Exit fullscreen mode
  • Installs the unzip utility, allowing the script to extract compressed files.

3. Clearing the Web Root Directory

   rm -rf /var/www/html/*
Enter fullscreen mode Exit fullscreen mode
  • Deletes all existing files in the /var/www/html directory, which is the default directory for web content in Nginx.

4. Downloading and Extracting Website Template

   wget https://github.com/startbootstrap/startbootstrap-agency/archive/gh-pages.zip
   unzip gh-pages.zip &>/dev/null
   mv startbootstrap-agency-gh-pages/* /var/www/html
Enter fullscreen mode Exit fullscreen mode
  • wget downloads the Start Bootstrap Agency template as a zip file from GitHub.
  • unzip extracts the downloaded zip file.
  • mv moves the extracted contents into the /var/www/html directory, replacing the files deleted earlier.

5. Changing Permissions

   sudo chmod +x /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode
  • Grants execute permissions to the index.html file in the /var/www/html directory.

6. Completion and Final Message

   echo "setup completed"
   sleep 3
   echo "check your ip on the browser to view result"
Enter fullscreen mode Exit fullscreen mode
  • Prints "setup completed" when the script finishes.
  • sleep 3 pauses the script for 3 seconds.
  • The final echo prompts the user to open their server's IP address in a browser to see the deployed website.

Purpose:

This script sets up a basic web server with Nginx, installs the necessary dependencies, and deploys a Bootstrap-based template to the web server’s root directory for easy testing or a quick project setup.

J. Review and Launch
Review all your settings. Once satisfied, click the Launch button.

Image description

K. Find the Public IP of Your Instance
Go to the EC2 Dashboard, select your instance, and note the Public IPv4 address.
It should look something like 3.93.189.84

Image description

Step 2. Access the Web Server in Your Browser
Open a web browser and enter the public IP of your EC2 instance:

http://3.93.189.84

Enter fullscreen mode Exit fullscreen mode

Step 3. Verify the Web Page

Image description

This guide has walked you through the process of setting up an automated web server on an AWS EC2 instance using a Bash script. By following these steps, you successfully updated and installed essential packages, deployed a pre-designed website template, and configured your instance for public access.

With your web server up and running, you can now explore further customizations, such as enhancing the security, adding SSL for HTTPS, or deploying more complex applications. AWS EC2 and Nginx provide a robust foundation for web hosting, whether for learning, prototyping, or deploying live applications.

Remember to regularly monitor your instance, optimize configurations for performance, and securely manage your key pairs and access credentials. This setup forms a solid starting point for building scalable and reliable web solutions.

Feel free to expand on this foundation and experiment with more advanced web server configurations. The possibilities are endless!

I am your guy, until next time, we meet here again. Stay tune!

Top comments (0)