Boman Avong
5 min readJul 25, 2021

--

Photo Credit: Yuri Blanc

Create an EC2 instance and host an Apache web server within AWS

In this project, we will be installing Apache Web Server on EC2 instance using custom user data script. The script will updates all packages, install Apache, and start the service.

Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the AWS Cloud. In other words, it provides us with a virtual server with different capacities to best suit your needs. Amazon EC2 enables you to scale up or down to handle changes in requirements or spikes in traffic with ease.

Objectives:

  1. Create a t2.micro EC2 instance.
  2. In the user-data field, use a script that [Updates all packages, installs Apache, and starts the service].
  3. Verify that the instance has the Apache web server downloaded and installed through the public IP.

Prerequisites:

  1. AWS Management Console with an IAM user.

Project Steps:

  1. Create a t2.micro EC2 instance

a. Log into the AWS Console as an IAM user. An IAM user has permanent long-term credentials and is used to directly interact with AWS services.

When you first create your AWS account you are provided with a root user account. This is different from your IAM account. If you are new to AWS, please this post here on how to create an IAM user account from AWS root account.

b. From the top left of corner of the page, click on “Services” and select “EC2” under “Compute”.

Fig. 1 AWS Services Dashboard

c. On the EC2 dashboard, select “Launch Instance”. You will notice that the instance will launch in the region closest to you. My region shows US East (N. Virginia).

Fig 2. AWS Launch Instance

c. For this project, we will be using the free tier eligible version of Amazon Linux 2 AMI.

An AMI is simply a template containing operating systems and applications. It provides the information required to launch an instance like type of architecture, launch permissions and storage for the root device.

You will see a list of AMIs with different configurations. Click Select to choose the Amazon Linux 2 AMI (NVM), SSD Volume Type.

Fig 3. Select AWS AMI Image

d. The next step will be to choose the EC2 instance type. Instances have varying combinations of CPU, storage, memory and networking. Think about it as a virtual computer with all the characteristics to fit your use case. A gamer for example will want to choose an instance type with high GPU and multiple CPU.

For our project, we will be using t2.micro which is also a free tier. You can see that our t2.micro has 2.5GHz CPU speed with 1 GiB memory which is sufficient to run our server. Select t2.micro and click “Review and launch”.

Fig 4. Select AWS instance type

d. On the “Configure Instance Details” page, scroll down the page till you see the “Advance Details” section. In the “User Data” section paste the following shell script to install the Apache Web Server.

yum update -y updates currently installed package.

yum install -y httpd.x86_64 installs the Apache web-server.

systemctl start httpd.service starts the server.

systemctl enable httpd.service automatically starts the service on boot.

Fig 5. Apache web server script
Fig 6. Configure Apache server with user data

e. Click on “Add Storage”, leave the default storage options and click on “Add Tags”.

Fig 7. Adding storage to EC2

f. On the “Add Tags” page, leave the default settings and proceed to “Configure Security Groups”. Notice by default the Security Groups has port 22 enabled which is for SSH. You can leave the default setting if you will SSH into the Linux machine.

For better security, replace the 0.0.0.0/0 with the range of IP addresses you'll use to connect to your instance on the SSH line. Under “Source” select “MyIP”.

Click on “Configure Security Group” and select “Add Rule”. You will be adding a rule to allow web traffic through HTPP port 80. Your final rule should be Type: HTTP, Protocol: TCP, Port Range:80, Source: Anywhere. The Description is optional. Click “Review and Launch”. Review the final configurations and click “Launch”.

Fig 8. Configuring security groups

g. Download the public and private keys and store them in a safe location. The keys allow you to connect to your instance securely. Click on “Launch Instances”.

Fig 9. Creating key pairs

h. Allow the instance to complete installation and then click “View Instances” on the lower right of the screen.

Fig 10. EC2 launch status

3. Verify that the instance has the Apache web-server downloaded and installed through the public IP

On the Instance dashboard, you will notice that the Instance State is set to “Running” and the Status check is set to 2/2 checks passed. Click on the “Instance ID” of the newly created EC2 instance it will bring you to the summary page. copy the “Public IPv4 address” and paste into your browser. If successfully created, you will get a response from the Apache server just created with “Hello World from-172–31–4–43.ec2.internal”.

Fig 11. Apache web server page

Congratulations! You have just created an Apache Web Server with an EC2 instance on AWS. In my next tutorial I will walk you through how to complete the same project using AWS CLI so watch. Let me know if you have any feedback or questions, thanks for reading!!

--

--