How to Launch an EC2 Instance with Apache Server using AWS CLI

Abidoye Joshua mayowa
5 min readFeb 11, 2023

--

In this project, we are migrating to the Cloud (AWS). This project aims to shift from local servers to cloud servers to save on expenses from purchasing the infrastructure for a data center. We will need AWS to host the company’s website. First, we must launch an EC2 Amazon Linux t2.micro in a public subnet. Second, we need to create a security group that allows inbound traffic on HTTP for 0.0.0.0/0 and inbound traffic on SSH from your IP address. The final step is SSH into our EC2 instance and installing Apache with a custom webpage using a BASH script.

Prerequisites

  • sudo privileges on the system as a non-root user.
  • AWS account access.
  • Command Line Interface (CLI) of your choice
  • Basic Bash Script knowledge

Step 1: Selecting an Ec2

In the AWS search bar, type “EC2” and click on EC2. Once EC2 is loaded, you must click “Launch Instance”. Attached below is what will pop up after searching for ec2.

Step 2: Naming and Choosing your Instance

Name your “Instance” and choose your “Amazon Machine Image”. An Amazon Machine Image (AMI) is a supported and maintained image provided by AWS that provides the information required to launch an instance. Amazon Machine ImageFor this project, we will use Amazon Linux AWS. In Level Up In Tech, our first few weeks of the program were all about Linux and learning the essentials.

we need to select the amazon linux 2 AMI machine. Below is the selected AMI image.

Step 3: Configure your EC2 — Key Pair

The next is to setup is to setup a KeyPair. This will allow you to SSH to your EC2; if you don't have one, you need to create one by clicking the “Create new key pair”. Once it is created (save it in your home directory), select it as your Key pair.

Step 4: Configure your EC2 — Network Settings

We need to Create a security group that allows inbound traffic on HTTP for 0.0.0.0/0 and inbound traffic on SSH from your IP address.AWS security groups operate as a firewall, with rules determining what network traffic can enter and leave. This process is very simple but key to completing this project since we are creating a web server. Select “Create security group” and check to Allow for all SSH and HTTP traffic.

Note: SSH is Port 22, HTTP is Port 80, and HTTPS is Port 443. We are not using HTTPS for this project.

The image above and below showed that our EC2 is up and running.

Step 5: SSH into your EC2 Instance

The next step is to go to your EC2 Instance, click connect, click SSH client and follow the instructions on logging in. The importance of saving your Key pair in the home directory makes it easier to login following the directions provided by AWS. I found that doing it this way made it easier for me to SSH to my EC2.

If you follow the instructions above, you should get the image below.

Step 6: Update the Apache server Packages

As we can see above, we were asked to install all updates. We can use the following command to update the server and install Apache as follows:

sudo yum update

Step 6: Install Apache with a custom webpage using a Vim Editor.

Vim editor will help us to create an editable bash file, and we will run the following commands on vim to install Apache.

vim Apache.sh

To execute the vim file we just created, we need to add the following command so we can execute the file.

chmod +x file name

We need to run the command below to confirm if our file is working.

sudo ./Apache.sh

Step 7: Check IP Address

Now let’s open up a webpage and paste our IP address, to confirm if it's up and running.

Congratulations, guys, we did it. We have successfully completed this task!

So that we won't get charged by amazon, we need to stop our instance, by clicking on the “Instance State” and then clicking “Stop instance.”

Thank you for taking the time to read this write-up, I will be posting more hands-on project. Join me next time to see more of my project.

--

--

Abidoye Joshua mayowa

DevOps Engineer. I'm interested in collaborating with anyone interested in cloud engineer or cloud DevOPs.