How to create an AWS EC2 instance & install Apache to host a website

Aaron Slowinski
4 min readSep 10, 2022

--

This tutorial with providing instructions on how to create a t2.micro EC2 instance with an Apache. A script will be used to update software packages and install and enable the Apache server.

Process:

· Launch an EC2 Amazon Linux t2.micro (free tier) in a public subnet.

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

Prerequisites:

AWS console access

IAM User account

VIM

Step 1: Log in to the AWS console and search for EC2, click on the EC2 services

Now click on the launch instance

We need to give your EC2 instance a name. For this project, I have just named my LUIT Week 5 Project

We were asked to use t2.micro free tier, so that is what we have selected

Step2: Next we will have to create a key pair. A key pair allows us to access our instances without having to use a password.

Let's give your key pair a name. For this name I used LUITW5

Then click on create key pair.

Note once you click create key pair it will download to the machine you are working on. Please note the download directory as we will need to know this path later

Step 3: Next we need to create a security group and connect to your EC2 instance

Click create security group, and review network settings. You can allow HTTS from the internet and allow SSH from only your pc

Once done with the network settings click on launch instance

Review instance is up by looking at the instance state

Select the instance and then click on connect

Next, go to the SSH tab

Copy the following line

Now head back over to our pc and open a terminal session and move the download directory to where Pair Key was downloaded.

For me, it was in the downloads directory

I used the following command to change to the downloads directory: CD downloads

Then paste the following command from your AWS SSH client that we copied and hit enter

You are now connected to the EC2 instance via SSH

Step 4: Next we need to create a BASH script

Back on our terminal use the following command to create a script

command VIM <filename.sh> to create a script. For this part, I just used the following name LUITWS.sh

For this project, we entered the following commands to install Apache and create custom webpage output.

Use I to insert or use the INS on the keyboard.

Once you are done hit the ESC key and then shift: and type in wq to write and quit

We also need to change the permission on the script by running the following commands

Step 5: testing — Let's test our script to see if it worked.

Grab the IP address for our instance

Bring up a web browser page and type in the instance IP address. If you see your comments on the web page you have implemented the script successfully.

--

--