Creating an EC2 instance in AWS to host a website

Babatunde Oyedeji
4 min readJul 11, 2022

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.

Objectives

  1. Launch an EC2 Amazon Linux t2.micro (free tier) in a public subnet.
  2. 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.
  3. 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)

Step 1: Log in to the AWS Management Console as an IAM user and navigate to the EC2 page. Choose the region closest to you. The closer the region, the lower the latency. You can use this link.

Step 2: Create and launch instance

Add a name tag to your instance for easy reference

Choose your Amazon Machine Image (AMI). I selected the Amazon Linux 2 AMI free tier. Click on browse more AMIs to view the complete list.

Click on create key pair and keep it safe

Note: I changed the Key pair name from “My project” to “My_Project”

Change the firewall settings by creating a new security group or use an existing security group. I will be creating a security group.

Allow HTTPS and HTTP traffic from the internet as requested and SSH traffic from your IP address only. This is to prevent unauthorized access to your instance

Verify your settings and launch the instance

Connect to your instance using the SSH client

I used the command below to connect using the SSH client. This reflected the name change of my key pair.

$ ssh -i "My_Project.pem" ec2-user@ec2-34-210-109-79.us-west-2.compute.amazonaws.com

Mac users can just follow through with steps 3 and 4 and are connected to your instance. However, because I am on Windows OS, I need to run some commands before I gain access.

Kindly follow along

Note: You need to change to the directory where the keypair before you continue on your terminal

Connection to instance successful

Step 3: Install apache with a custom webpage using the BASH script

$ vim project

Press “i” to enable insert mode

save by script by pressing escape and “:wq” to save and exit

$ chmod +x

makes the file executable

$ ./project

runs the script

I am denied permission, why? I need to run the command with sudo

$ sudo ./project

Try out the webpage using the public IP address on the instance

Note:

I can also update all packages, install and start Apache service by writing a script in the user-data field when creating an instance.

In this case, when the instance is created, Apache also starts

Thanks for reading

--

--