Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

AWS Tutorial: Automating an Apache Web Server Deployment on AWS Using CLI and EC2 Instance Creation

We are back with another exciting project, this time in Amazon Web Services (AWS)!

This week we will create an EC2 instance in AWS, then SSH into our EC2 instance and install Apache with a custom webpage using a BASH script.

Scenario: Let’s say we are working for a Bank!
Your company wants to start shifting from using on-premises servers to using servers in the cloud. Rather than purchasing all the infrastructure in a data center, they ask you to create an EC2 Instance in AWS to host their new website.

Benefits of Using AWS for Banking Institutions:

Scalability: AWS provides on-demand resources to help banks scale their IT infrastructure quickly and efficiently.

Security and Compliance: AWS offers a range of security and compliance features that can help banks protect their data and meet regulatory requirements.

Cost Savings: AWS provides a range of pricing models that can help banks optimize their cloud spending and reduce IT infrastructure costs.

Innovation: AWS offers various services and tools to help banks experiment with new technologies, such as machine learning and artificial intelligence

Key Words Defined:

Apache- A free and open-source cross-platform web server software.

AWS- Amazon Web Services, the world’s largest cloud computing platform.

Bash Script- a series of commands written in a file. These are read and executed by the bash program. The program executes line by line.

CLI- Command Line Interface, a text-based user interface (UI) used to run programs, manage computer files, and interact with the computer.

EC2 Instance- a virtual server in Amazon’s Elastic Compute Cloud (EC2) for running applications on the Amazon Web Services (AWS) infrastructure.

SSH- Secure Shell, a network protocol that gives users a secure way to access a computer over an unsecured network.

(●'◡'●) Now let us go over the Essentials and Objectives for this project!

Essentials:
I:
AWS Account (Free Tier)
II: Command Line Terminal
III: Sudo capabilities

Objectives:
I:
Launch an EC2 Amazon Linux t2.micro (free tier) in a public subnet of your Default VPC.
II: Create a security group that allows inbound traffic on HTTP for 0.0.0.0/0 and allows inbound traffic on SSH from your IP address.
III: SSH into your EC2 instance and install Apache with a custom webpage using a BASH script.

Put Your Game Face On and Let’s Roll!!

I: Launch an EC2 Amazon Linux t2.micro (free tier) in a public subnet of your Default VPC.

First, let’s log in to our (free) AWS account, then use the search bar located at the top, and type “EC2” to bring you to the screen below.

From here we will click on the orange button “Launch Instance”. This will bring you to the instance configuration screen. An example is located below.

Configure your instance exactly as the example is shown above.

  • Name your project
  • Use the “Amazon Linux 2023 AMI” application.
  • Make sure the “Virtual Server Type” is “t2.micro”.
  • Leave all other configurations up to this point as their default settings.

Next, we must create a key pair!

Click on “Create new key pair”. The screen below will auto-populate.

Configure your key pair exactly as in the image above.

  • Name your key pair
  • The key pair type will be “RSA”
  • The private key file format is “.pem”

Once configured click the “Create Key Pair” button and save the key pair in the “Downloads” file on your computer.

We will refer to this key pair later when we use the command line.

II: Create a security group that allows inbound traffic on HTTP for 0.0.0.0/0 and allows inbound traffic on SSH from your IP address.

Next, scroll to the “Network Settings” section. Under the “Firewall (security groups)” section follows the configuration listed below.

  • Click the “Create security group” option.
  • Check the “Allow SSH traffic from” box and on the drop-down button to the right choose the option “My IP”.
  • Check the “Allow HTTPS traffic from the Internet” box.
  • Check the “Allow HTTP traffic from the Internet” box.

Your configuration settings should mirror the image shown below.

Verify the “Summary” Section is as follows below and click the “Launch Instance” button.

Congratulations! You have just launched an “EC2” instance. Give it a few minutes to configure and start running!

Use the search bar to go to your EC2 Dashboard. You will see the screen shown below. Notice in the Resources section that we have one instance currently running. Click on “Instances (running)”.

Next, click on the checkbox next to your running instance, and at the top of the page click on the “Connect” button.

This will direct you to the following screen!

From here click on the “SSH Client” tab.

We will come back to this screen but now it is time to work in the command line to access this EC2 instance securely.

Open your local computer Terminal or PowerShell. Follow the instructions below to get access to your EC2 instance.

cd Downloads
From your home directory type in the following command to change into your Downloads directory where your “key” is located.

Next, jump back to your AWS window and copy and paste the highlighted command located under the “Example” section. **Note that your command will differ from mine in the image below**

Copy and paste the highlighted code directly into your command line!

Press Enter once the command has been pasted, and enter “Y” when asked if you would like to continue then🪄Wah-Lah you have access to your Amazon Linux Instance. The example is shown below.

III: SSH into your EC2 instance and install Apache with a custom webpage using a BASH script.

We have entered our EC2 instance securely now let’s install our Apache Web Server with a Bash script.

To create our file and access it we will be using the “vim” command shown below.

vim Project

Press “I” to be able to insert text in your Vim text editor.
Enter the below command as followed.

#!/bin/bash

#Update all yum packages
yum update -y

#Install Apache Web Server
yum install -y httpd

#Start Then Enable Apache Web Server
systemctl start httpd
systemctl enable httpd

#Adds our custom webpage html code to "index.html" file.
echo "

Congratulations Project One Complete!

" > /var/www/html/index.html

This command when run will update the required packages for your web server, install the Apache web server, Start and Enable the Apache web server, and add text to your Apache web page.

To exit the Vim text editor press the “Esc” key and type “:wq” to save and quit Vim.

Before we can run the command we must first change the permissions on the file “execute”. Run the below command to edit the permissions.

chmod 744 Project

Check your permissions using the following command.

ls -l

Your results will reflect the image below.

Next, use the following command to run your bash script.

sudo ./Project

In order to view your results on the webpage you will need your IP Address. To view in your command line Enter the below command.

curl ifconfig.me

Use your listed public IP address and post it in a new window to search for it on the internet.

🍾🙌🎉Congratulations!!
We have just completed the Project.

If your feeling like you can conquer the world then try your hand at the advanced portion of this project.

Objective:
I:
SSH into your EC2 instance and create a “scripts” directory.
II: Move your Bash script created earlier from the current location to the new scripts directory.
III: Create a new repository in your GitHub for your Bash Scripts
IV: Use Git commands to add your Bash scripts to the newly created GitHub repo.

Essentials:
I:
GitHub account (Free)
II: Sudo Capabilities
III: Command Line Terminal (Used in portion 1 of this project)

OK! Let's get started!

I: SSH into your EC2 instance and create a “scripts” directory.

First, we will make a new “Scripts” directory!
Use the below command to create the directory, then use the 2nd command to verify the placement of the directory.

mkdir Scripts
ls
Results should be as shown above.

II: Move your Bash script created earlier from the current location to the new scripts directory.

Next, we will use the “move” command to transfer the “project” file into the “Scripts” directory.

mv Project Scripts

To verify we will change to the ”Scripts” directory and use the “ls” command to view the change.

cd Scripts
ls

III: Create a new repository in your GitHub for your Bash Scripts

Log in to your GitHub account and from the homepage select the green button titled “New” to create a new repository.

In the “Repository Name” section type in the name “Bash Scripts”. Leave all other settings as default and click the green “create repository” button at the bottom.

Jump back to your command line and let’s install “git”. Type the command below.

sudo yum install git

In order to use “git” we must configure it. Which means we will create a “username” and “email”. Follow the below commands to configure. Be sure to switch to your “Scripts” directory if not already in it.

cd Scripts
git config --global user.name "Use your GitHub profile name here"
git config --global user.email "Use your own email address here"

IV: Use Git commands to add your Bash scripts to the newly created GitHub repo.

From here switch back to your GitHub account, click into your “Bash-Scripts” repository and we will copy the HTTPS code.

Once the code has been copied switch back to your command line and we will use “git clone” to put the repo in git.

git clone {paste your code here}

Next, use the mv command again to place the “project” file into the “Bash-Scripts” repo.

mv Project Bash-Scripts

Next, use the “git add” command to add “project” to the repo.

git add Project

Use “git status” to verify the movement.

git status

ALMOST AT THE FINISH LINE🏁🏁🏁🏁

Now that we have the file added, we can use the “git commit” command to save the changes to your repo.

git commit -m "Project 3 Advanced"

Before we move into the last step of the project we must switch back to GitHub to retrieve our personal access token. We will use the personal access token when prompted to use our password in the command line.

Click in your GitHub settings
Click on Developer settings
Click on “Tokens (classic)”
** Click “Generate new token (classic)**
Name your token “Project 3”

Set your scopes as displayed below.

**Very Important** — once you click “generate token”, your token code will populate, save this token as this will be the only time that it is displayed.

Lastly, let's use the “git push” command to transfer our file to GitHub.

git push

Type in the personal username (should be the same username as your GitHub account) that you created previously.

Paste the personal access token when you are prompted for your password.

Switch back to your GitHub profile and take a look at what you have completed.

Switch to the “Bash-Scripts” repo

Click on “Project 3 Advanced” and you should see your bash script!

Advanced Project Complete!

Congratulations on sticking with it and learning something new!

YOU DID IT!!

Come back here for more DevOps fun!

Feel free to connect and chat using the linked information below!

LinkedIn:https://www.linkedin.com/in/brandon-mccullum-4504b7161/


AWS Tutorial: Automating an Apache Web Server Deployment on AWS Using CLI and EC2 Instance Creation was originally published in TechManyu on Medium, where people are continuing the conversation by highlighting and responding to this story.



This post first appeared on TechManyu, please read the originial post: here

Share the post

AWS Tutorial: Automating an Apache Web Server Deployment on AWS Using CLI and EC2 Instance Creation

×

Subscribe to Techmanyu

Get updates delivered right to your inbox!

Thank you for your subscription

×