Create An AWS EC2 Instance To Host A Website Using Apache Web Server

William
10 min readFeb 13, 2023

Why use an AWS EC2 instance to host your site?

With EC2, you can shift from in house data centers to scalable cloud computing with very little hassle. Whether you’re just starting out with a small website or managing an enterprise application, Amazon EC2’s pay-as-you go model ensures that your cloud spending only grows as much as it needs to!

Scenario

Today we have a scenario where our 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 us to create an EC2 instance in AWS to host their new website. We will:

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

This will be done in a VPC that has already been created. If your AWS account does not have a VPC, you can quickly create one using this AWS Administration guide.

The first few steps will entail configuring our routing and subnets. This is to ensure that HTTP and SSH traffic are able to travel to and from our EC2 instance.

Subnets

We will begin in the AWS console. In the search box, search for VPC and select the first result. This will take us to the VPC dashboard.

From the dashboard, select subnets.

Any existing subnets will appear here, click “create subnet” on the top right to continue.

Associate your subnet with your previously created VPC using the dropdown menu. Write down the CIDR block range under your VPC name in the dropdown menu and click “Create Subnet”.

VPC Name in RED. CIDR Block in ORANGE

Next we will modify our subnet settings. Give your subnet a name and select an availability zone.

Next we select an IPv4 CIDR block for the subnet. Earlier we noted our CIDR block range for the VPC. Under the IPv4 CIDR block option, type the IP address without the slash. It will recommend multiple blocks with different numbers after the slash.

Choose a block with a higher number after the slash than the VPC’s number. For example, the VPC in this demonstration had a block of 172.31.0.0/16. For the subnet we will chose 172.31.0.0/24.

Classless Inter-Domain Routing (CIDR) notation is a way of representing an IP address and its network mask. For more information on please see AWS documentation on VPC IP addressing.

Keep the default tag or modify it if desired, then click “create subnet”.

Success! You should now be back at the subnet screen with our new subnet in the list.

For our subnet to be public, it needs to be able to route traffic to the internet. For this we will need to configure an internet gateway.

Internet Gateway

Highlight your subnet, click the actions menu and select “Edit subnet settings”

At the “Edit subnet settings” menu, select “Enable auto-assign public IPv4 address.

Click “Save”

Back on the VPC dashboard, select Internet gateways from the menu on the left.

On the internet gateways dashboard select create internet gateway.

Next name the gateway, modify the tag if needed, then select “create Internet gateway” to save.

Now, with the gateway created, we need to enable communications to the internet. For this the gateway has to be attached to our VPC.

Click the actions menu and select “Attach to VPC”.

Select the VPC from the dropdown menu and click “attach Internet gateway”

Routing Tables

Now we configure our routing tables. This tells traffic in our subnet how to get to the Internet. In the VCP Dashboard menu, select “Route tables”, then “Create route table” from the dashboard.

Give your route table a name, select your VPC from the dropdown menu, and select “create route table” to save.

We now need to create a route for traffic to get to the public internet. Select “Edit routes” at the bottom of the page, then “add route” on the following menu.

Input 0.0.0.0/0 for Destination (route to public internet). Select internet gateway for the target.

Click “save changes”

Finally we will associate our route table with our public subnet. At the route table menu, click the“subnet associations” tab, then “edit subnet associations”

Select our previously created subnet, then click “save associations”.

Now our subnet will allow traffic within it to access the public internet.

EC2

With routing finalized we can now get to the EC2 instance. Let's begin by typing EC2 into the search bar and select the first option.

At the EC2 dashboard select “launch instance”.

Give you instance a name.

Choose the desired machine image, architecture and instance type. For this lesson the image we are selecting is Amazon Linux 2 AMI (free tier). The architecture is “64-bit (x86)”. The instance type is “t2.micro”

SSH / Key Pair

In order to SSH to your ec2 instance, you will need to create a new key pair or select one that has already been created. To create a new one, select “create new key pair”

Give you key pair a name and leave other settings default. Click “create key pair”. The key pair will download to your machine, note the location as we will come back to this later.

Continue at the “launch an instance” menu. Under network settings, you will see “create security group” highlighted. We are going to allow ssh traffic from our IP address only. Make this change by selecting the dropdown menu that says “anywhere” and selecting “My IP”.

Check the box to “Allow HTTP traffic from the internet” then click “launch instance” in the bottom right corner

With that, the instance has been created. Click View all instances to see machine.

After EC2 creation, it can take a few minutes before you’re able to connect to the machine. Ensure the “Instance state” shows running and under “status check” all checks have passed.

Now we will connect to our newly launced machine using SSH. The following instructions are for connecting from a Mac or Linux machine. To connect to the EC2 instance using Windows Powershell, please refer to this AWS documentation.

Begin by opening Terminal or any other program you may use for SSH connections.

Earlier we downloaded our key pair to our personal computer. Using the cd command, change directory to the folder containing that file.

Before you can connect, you need to ensure your key is not publicly viewable. Run the following command to make the file unreadable for any other users on your machine.

chmod 400 your-key-name.pem

You will also need to know the public DNS address of your EC2 instance before connecting. This can be found by clicking on the instance id in the EC2 dashboard.

This takes you to the instance summary where you can find the address.

With this info you are now able to establish an SSH connection with your EC2 instance. The command syntax is:

ssh -i key-pair-name ec2-user@PublicDNS

In my case that would be:

ssh -i test-key-pair.pem ec2-user@ec2-34-202-164-113.compute-1.amazonaws.com

After entering the command in Terminal, you will receive an authenticity warning, type “yes” at the prompt.

Run “sudo yum update -y” at the prompt to install all security updates needded on our new machine.

sudo yum update -y

Installing Apache Web Server

With all updates complete we are now able to install Apache Web Server on our machine. We will accomplish this using a bash script. This script will install the package as well as customize our test page on the website with the message “Project Complete!”

Lets first use the touch command to create an empty file named “install-apache.sh”. We will write our script in this file.

touch install-apache.sh

By default the file won't have the necessary permissions to be executed as a script, let's change that with the “change mode” command.

chmod u+x install-apache.sh

In this command, chmod is giving the user (u) permissions to execute (x) the file install-apache.sh.

To verify the file is now executable, use the ls -l command to list permissions.

ls -l

In the output, the x in the beginning (rwx) shows that the script can be executed by you (the user).

Now we will modify the file’s text using Vim text editor.

vim install-apache.sh

Once Vim opens, type “i” to enter insert mode and type the following. The script will:

  • Install Apache.
  • Change the ownership of the html directory to so we can customize our test page.
  • Add our formatted text “Project Complete” to the test page.
#!/bin/bash

#Install Apache
sudo yum install httpd -y
sudo systemctl start httpd

#Change ownership of html directory to customize test page
cd /var/www/html
sudo chown -R $USER /var/www/html

#Add our text to custom test page
sudo echo "<html><h1>Project Complete!</h1></html>" > index.html

Hit the escape key when finished typing. Next type “:wq” and press enter to return to the terminal.

Our bash script is ready, run the file by running the following from the terminal.

./install-apache.sh

Complete! Apache has been installed and our test page should now have our “Project Complete” message. To verify, we will need to access our website using our public IP address. Use the following command to get the address.

curl ifconfig.me

The commands output has given me the public IP address of 34.202.164.113.

Access the address you receive through your browser. We have not configured https on this EC2 instance, so ensure you input the address in your browser using http:// and not https://

Success, the site is accessible through the browser and shows our custom message!

--

--