A Journey into the cloud: Setting up a web server in the AWS cloud.

Aaron Bachman
9 min readDec 11, 2022

--

This will be a short guide on how to set up an EC2 instance using AWS, then installing an Apache web server using a BASH script via ssh on your new EC2 instance.

There are many reasons why you would choose to go with a cloud server over an on premises server; cost, accessibility, scalability, redundancy and security.

First things:

  1. Open up an amazon web services account here

2. When you have started an account this will be your root account login. For security purposes you may want to set up multi-factor authentication. Then set up a user with Admin privileges. There are other best practices that you should follow and more can be found about this subject here.

3. You will need to be able to ssh from your computer using terminal to your aws web services account.

4. Basic knowledge of linux and command line.

5. Basic knowledge of BASH scripting.

Section 1

Creating an EC2 instance:

Let’s log into our aws web services account with your user Admin login and navigate to the EC2 page. You can type EC2 into the search bar and this will pop up.

Notice In the upper right corner I am in the N. Virginia us-east-1. This is the current Availability zone that I am currently in. You should choose yours based on your geographical location and or the location of your clients, and whether or not there are compliance issues that you need to observe. I am also logged in as a user with Admin privileges. Next click on the orange Launch Instance button, again click Launch instance in the small pop up window.

Name your instance.

For this tutorial I am setting up an Amazon Linux aws AMI.

I am selecting a t2.micro instance which is free tier eligible. There are many to choose from.

We are then asked to create a Key pair which consists of a private and public key that will allow us to log into our EC2 instance via ssh when used together. Click create new key pair.

Name your key pair. I am using the RSA Key pair type, and the .pen file format. I did have some issues with using my keys to logging on via ssh, but I will explain what I needed to do to resolve the problem later on. Click create key pair and your key will automatically be downloaded. Keep this key safe.

For my Network setting I am choosing to create security group that will be call ‘Launch-wizard-8’ with the following rules.

  1. Allow ssh traffic from anywhere. I am using this setting in this case because this is a tutorial. This is not best practice, and it tells you so. Use only known IP addresses.
  2. Allow HTTP traffic from the internet. This will allow traffic from all IP addresses on the internet.

I am leaving the configure storage with the default setting settings selected.

Then click the orange Launch Instance button.

Congratulations on creating your very own EC2 instance on amazon web services!

Click the view all Instances button, and then you will be able to select which instance to view/ perform actions with. You see with my New server currently selected that underneath are several tabs to look at and view info about my server. Click the connect tab in the top middle area.

As stated earlier we will be connecting to this server via ssh, there are several options to choose from.

The first time around I did not save my key file to a specific place. I had it stored in downloads, and had a hard time finding it. This time around I saved it to my Keys file so it was easier to find. A tip one of our coaches at Level Up in Tech suggested was to open your key in a code editor, you can view it and then save it from there. Before that I was stuck getting this error message.

I use a mac and ended up going down a rabbit hole trying to solve this problem, only to realize that I can open and view it in a code editor, and save it to a file I choose.

Next we are going to change the file permissions for the key. Go to your terminal or CLI and navigate to where you have your keys stored. I have a file called “Keys”. Enter the command

You can see in my example how my permissions have changed to read only for the owner.

Copy the example link under Example as shown here.

Paste the link into your terminal or command line. It will ask you are you sure you ant to continue connecting. Type yes. Mine does not show this but that is because I exited the session and then logged back in right after to document this.

Congratulations on completing another step! Small victories are worth being celebrated!

Section 2

Next we will be installing an Apache server on your EC2 instance using a BASH script.

I am using the text editor VIM in this example. To open it I typed.

This also creates a new file named filename.sh after I save the changes I will make in VIM. I created a simple BASH script that will update the the EC2 instance and install an Apache web server.

Save by typing :wq

Now we will need to change the permissions of the file to make it executable. You can see below the changes made to the permissions of the file by using the chmod 744 Scripts.sh command.

Now lets run our Script: Type ./Scripts.sh

I had already completed this step but you will be asked do you want to install, type yes and hit enter.

In your instance page copy the public ipv4 address.

Paste this address into your web browser. If you click open address right after the ip address it will not work because that is an https link, and we have only enable http.

My custom webpage looks like this:

Time to celebrate! I’m going for a coffee!

Section 3

This week along with the regular project we were presented with an advanced, and a complex add on to the original project.

This weeks advanced portion of the project tasked us with connecting to our EC2 instance via SSH, then creating a new scripts directory, and moving our BASH script that we created earlier to this new directory. Creating a new repository in our GitHub for Bash scripts, and finally adding our BASH script to our GitHub repo using Git commands in terminal or command line.

To follow along you will need to know the basics of git, and GitHub. If you don’t please follow my guide from last week here.

Let’s start by creating a new directory:

An ls command will show you your new directory along with your previously made executable file.

Lets then use the mv command to move our filename.sh file to our scripts directory.

For the next step; go to your GitHub account and create a repository for your Bash Scripts. Click Repositories:

Click the green button New:

Name your new Repo.

At the bottom of the page will be a green “Create repository” button, click it.

This new repo is file online that will hold our BASH scripts. Other people can view and collaborate with you.

Next we need to install Git onto our EC2 instance.

We need to configure git with our information.

Next up: we need to clone our new repository on GitHub. Click the green Code button and copy the url.

In terminal or your command line enter:

Now we have a newly cloned repo!

We are now going to move the BASH script we created and moved once already into our new BASH-Scripts repo.

Typing git status will show up the untracked files that we have. In my case I have 2.

Type: “git add filename.sh

Then type: “git commit -m “BASH Script” A better description for my changes would have been better.

Our changes have been made and now we can push them to GitHub.

I had already cloned and pushed today from my GitHub account so I only typed git push.

My BASH script “filename.sh” is now in my repo!

I have been working on the complex portion of this weeks project, I will post that completed part down the road.

--

--

Aaron Bachman

Level Up in Tech student. DevOps, Cloud engineering, AWS, Terraform.