Installing Apache Web Server on AWS EC2

Sheniell Sinclair
8 min readAug 16, 2022

--

In this article we’ll finally be taking a dive into the amazing world of Amazon Web Services (aka. AWS). I’ll be installing a webserver that will host a website for a company that will be shifting on premises servers to the cloud.

Things you’ll need: AWS free-tier account to launch EC2 Instance with configured security groups, Windows Powershell, Github account, git utility and vim utility.

What is AWS? According to Amazon Web Services, “AWS is the world’s most comprehensive and broadly adopted cloud offering, with more than 200 fully featured services available from data centers globally. Millions of customers — including the fastest-growing startups, largest enterprises, and leading government agencies — are using AWS to lower costs, increase security, become more agile, and innovate faster.”

What is an EC2 Instance? In order to understand what an AWS EC2 Instance is, we my first understand what Cloud Computing is. According to Amazon, “Cloud Computing is the on-demand delivery of IT resources over the Internet with pay-as-you-go pricing. Instead of buying, owning, and maintaining physical data centers and servers, you can access technology services, such as computing power, storage, and databases, on an as-needed basis from a cloud provider like Amazon Web Services (AWS).” EC2 is in fact Amazon’s version of cloud compute, hence the name “Elastic Cloud Compute.” I’ll use the AWS EC2 to run my virtual cloud environment.

What is a Security Group? A security group is essentially a virtual firewall that controls inbound and outbound traffic to and from the AWS EC2 Instance. Security groups are stateful, mean that traffic allowed in will be remembered and allowed to exit the EC2 instance. They’re also permissive so creating deny access is not permitted. I’ll use the security group to allow me access into the EC2 Instance.

[Prior to starting have an Amazon Web Service account with an IAM user created to perform administrative tasks (DON’T USE ROOT) and GitHub account created.]

LET’S GET STARTED:

Phase 1: Launching an EC2 instance in a public subnet.

Step 1: Sign into Amazon Web Service Management Console with IAM user credentials and MFA code.

IAM user sign in landing page
MFA landing page

Step 2: In the search bar type in EC2 to access dashboard, then click Lauch instance. (NOTE: Make sure you are in us-east-1 region (N.Virgina))

EC2 instance dashboard

Step 3: Give the EC2 Instance a name then choose Amazon Linux 2 AMI (HVM) - Kernel 5.10, SSD Volume Type for the OS (NOTE: This AMI is Free tier eligible).

Step 4: For Instance type select t2.micro (Free tier eligible) and create a new keypair.

Step 5: Name your key pair and select key pair configurations (type and file format). Click “Create key pair”

Create Key pair menu

Step 6: In network settings menu select create security group. Then allow SSH traffic from 0.0.0.0/0, HTTP and HTTPS because this EC2 instance will serve as a web server. (Later we will do more configurations to the security group).

Network Settings

Final Step: We’re going to bypass the remaining menus (Configure storage and Advanced details) then take look at the summary to ensure all of configurations for the EC2 instance are correct. Finally click “Launch instance”.

Successful launch of EC2 menu

[Make sure EC2 Instance is running before starting this step]

Phase 2: Edit the security group to allow SSH directly from your local machine.

Step1: Click the EC2 menu button then Scroll down to network settings and select “Security group”

Step 2: Select the “lauch-wizard-1” and view inbound rules. Click “Edit inbound rules”.

Security group menu > Inbound rules

Step 3: Click “Add rule” then select configurations. For type select “SSH” and for source type select “My IP” then click “Save rules.”

Phase 3: SSH directly into AWS EC2 instance from local machine.

Step 1: Head back to “Instances” from the EC2 menu and select your running instance. In “Details” view the Public IP address and copy it. Finally click “Connect” and take a look at the username for the instance.

EC2 Instance Details

Step 2: Open up Windows Powershell and switch to the directory containing the Private access key. Finally SSH in the EC2 Instance

Phase 4: Create a bash shell script to install Apache webserver.

Step 1: Must have root access or superuser permissions in order to execute a script for this kind of workload. Navigate to the root user directory {$ sudo su -}

Step 2: Run command to create shell script file and open vim text editor. {vim apache-server.sh}

Step 3: Edit the script. I complied my script from several different sources. (NOTE: Always start script with {#!/bin/bash}) When finished, press [esc] key the type {:wq!} to write (save) and quit the vim text editor.

Step 4: Change the file permissions in order to execute the script. {chmod 744 apache-server.sh} (NOTE: Use {ls -al apache-server.sh} to view permission on the file)

Step 5: IT’S TIME TO RUN IT! { ./apache-server.sh }

Last Step: Let’s check that the script ran successfully. Open a web browser and input the public IP address to the EC2 instance into the address bar and…

Roadblock and Solution: I accidentally left SSH open to all IP addresses and had to go into my security group and delete the rule. It is not security best practice to leave this open because anyone in the world might be able to gain access to your EC2 Instance and all it’s data. There’s even an explicit note stating this fact and I simply bypassed this.

ADVANCED Project: Push script to git repository

Phase 1: Install Git and configure.

Step 1: Install git utility and configure. {yum install git} {git config — -global user.email “email for associated git account”}, {git config — -global user.name “username for git account”}, {git config — -global core.editor “path to editor”}, {git config — -list}

Step 2: Head over to GitHub.com and create a new repository to house the script. Choose name for remote repository, leave public and choose “add README file”.

Step 3: After creating repository, copy the HTTP link inside the green “code” button.

Step 4: Clone the remote git repository to local machine.

Step 5: Change to local git repo and take a look at the files

Step 6: Create scripts directory and move apache-server.sh into the new directory.

Step 7: Move the script directory to the git repository.

Phase 2: Commit and Push

Step 1: Create a branch and add the script to it. {git branch}

Step 2: Commit the file. {git commit -m “comments”}

Step 3: Merge the branches. {git merge main (branch you want to merge to main)} (NOTE: You must be in the main branch for this to work)

Step 4: Push the local repo back to the remote repo. {git push} (Must have Personal Access token put into password field. More about this in my previous publication Source Control with Git.)

Final Step: Check the github.com remote repo

Roadblock and Solution: Be extremely careful with the paths to the files that need to be moved. I accidentally deleted the script and had to rewrite it because moving the file incorrectly will result in it being deleted altogether. Be sure to write down direct paths so that commands are executed with proper syntax.

Thank you for stopping by and going on this journey with me!

Connect with me on LinkedIn:

www.linkedin.com/in/sheniell-sinclair

--

--

Sheniell Sinclair

DevOps/Cloud Engineer. Here I’ll document my learning and projects in the Cloud. www.linkedin.com/in/sheniell-sinclair