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:
- AWS Account with IAM Role/Permissions
- 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.
C. Launch a New Instance
On the EC2 Dashboard, click the Launch Instance button to start the process.
D. Give your instance a name unique to the project you are currently working on
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.
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.
G. Configuring the key pair, you can choose from the previous ones or create a new one.
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
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"
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
-
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 ofapt update
to/dev/null
, effectively suppressing the output. The installation logs are redirected to a file namedoutput.txt
.
2. Installing Unzip Utility
apt install unzip -y
- Installs the unzip utility, allowing the script to extract compressed files.
3. Clearing the Web Root Directory
rm -rf /var/www/html/*
- 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
-
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
- 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"
- 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.
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
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
Step 3. Verify the Web Page
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)