Creating an EC2 instance in AWS to host a new website

Shreya Kale
6 min readJan 23, 2023

--

Scenario

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

***************************FOUNDATIONAL:********************************

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

- 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.

- SSH into your EC2 instance and install Apache with a custom webpage using a BASH script. (The webpage can be a simple Hello World or something more complex. We are not testing your HTML skills)

1.) After logging into your AWS account, search for “EC2” service and click on the instances

2.) Click “Launch Instance” to start the AWS wizard to build an EC2 instance

We’re going to use the “Amazon Linux” instance in our example. Give your server a friendly name.

NB: Make sure you have “t2.micro” selected for the instance type as it is a part of the free tier.

3.) Next step, we need to create a key pair in order to access the server we created. This can be done in the same setup wizard. Click “Create new key pair”

Give your key pair a familiar name; I used “AWSLAB”. Ensure “RSA” is selected for the key pair type and the private key file format is set to “.pem” then click the “create key pair” button

Once the key is created, it will download on your computer as seen below.

I suggest moving this file into a safe folder or location on your computer. You will need this to later to ssh into the server.

4.) Scroll down to the “Network settings” section and click “Edit”

We’re going to give our security group a friendly name, in my case “Public_Subnet”. This helps to organize your network so you can properly group resources together.

We’re also going to add three security rules to our “Public_Subnet” security group:

  • ssh
  • HTTP
  • HTTPS

Since this is a test, we’re going to allow those ports to be accessed from anywhere.

Note: in a production environment, its best to allow ONLY what needs access to the resources instead of anywhere.

5.) Once you’re done, confirm that you’re able to get through all the steps and then click “Launch Instance”.

You will now see your instance if you navigate back to EC2 — → instance. We will now connect to our EC2 instance by selecting our server and clicking “Connect”

Connecting our EC2 instance

Navigate to the “SSH client” tab. We’re going to copy the ssh command in the green circle in the picture below and paste it into the terminal on your computer to connect to the EC2 instance.

Note: “AWSLAB.pem” in the red box, is the key pair I created earlier. The command is using the key pair as an authentication to log into the instance.

I pasted the command into the terminal and got a “Permission denied” message. This is expected because we need to make a few changes.

1st — we need be in the same directory as our key pair that we created; in my case, I place my key pair in a folder which I will navigate to within my terminal

cd ../Documents/Keys

2nd — change the permission on the file so that the terminal can read the key pair file.

sudo chmod 400 AWSLAB.pem

Try to connect to the EC2 instance again after making those changes.

Creating a Bash script that installs Apache and creates a basic web page.

vim ApacheScript.sh

Bash Script

#!/bin/bash
# updaing all the packages on the server 
yum update -y
# installing the Apache web server
yum install httpd -y
# Start the Apache services
systemctl start httpd
# Enable to Apache services to start automatically whenever server restart or boot
systemctl enable httpd
# Create a same HTML file called index.html and placed into in the /var/www/html location.
echo "<html><body>Hello World - My Name is Courtney Campbell and this is a Custom Webpage using Apache<body><html>" >> /var/www/html/index.html

Running script — make sure to change the permission on the script to give “root” execute rights.

sudo chmod u+x ApacheScript.sh

Run script — two ways to run your script

sudo bash ApacheScript.sh or sudo ./ApacheScript.sh

******************************ADVANCED: **********************************

- SSH into your EC2 instance and create a scripts directory.

- Move your BASH script created earlier from it’s current location to the new scripts directory.

- Create a new repository in your GitHub for your BASH Scripts. (you can use this repo in the future for any new scripts you create or if you need to use one for a future project)

- Use Git commands to add your BASH scripts to the newly created GitHub repo.

Create a new directory called “scripts” on the newly created server

mkdir scripts 
mv ApacheScript.sh scripts/ApacheScript.sh

Log into your GitHub account and create a GitHub repo as seen below.

Once the repo has been successfully created we will need to clone it locally onto our EC2 instance which can be done by using the following command in your terminal.

git clone (Repo URL Address)

If successful, you’ll see the “Cloning into (Name_of Repo)” Once downloaded, check and verify if the repo is locally on the server as seen below

ls -l

Move the “scripts” folder into the “BASH-Scripts” repo with these commands

mv scripts BASH-Scripts/

Pushing the file back to the origin

Add the “ApacheScript.sh” script to the git staging area with the following command.

git add ApacheScript.sh

Then commit the changes to the local repo.

git commit -m "Pushing ApacheScript.sh to GitHub"

Once committed, push the changes back to your remote repo.

You will be prompted to enter the user name for your Github account and password (In the password field, enter the access token)

git push orgin -u

Ec 2 Instance

AWS

Apache

Github

--

--

Shreya Kale

AWS Cloud DevOps Engineer AWS Cloud, Azure Cloud, GCP Cloud, Alibaba Cloud