Hands-On Experience with AWS EC2 and Apache Web Server Configuration

Stephanie Tabares
7 min readFeb 15, 2023

--

Are you looking to gain hands-on experience with AWS EC2 and Apache web server configuration? If so, you’ve come to the right place! In this article, we’ll explore the basics of AWS EC2 and Apache web server configuration, so you can get up and running quickly and easily. We’ll cover the key concepts, provide step-by-step instructions, and offer helpful tips and tricks along the way. The project will guide you through my process of setting up an EC2 instance on AWS and configuring an Apache web server on it. You will be able to gain a better understanding of the AWS EC2 service and Apache web server by completing this project. Additionally, you will be able to practice and develop your skills in setting up and configuring web servers on EC2.

Project A

  1. Create a t2.micro EC2 instance with the OS of your choice (Just make sure it is free tier).
  2. In the user-data field, use a script that updates all packages, installs Apache, and starts the Apache service.
  3. Verify that the instance has the apache webserver downloaded and installed through the public IP

Prerequisites:

  1. AWS account
  2. A t2.micro EC2 instance
  3. An operating system of your choice
  4. Knowledge of basic Linux commands
  5. How to SSH
  6. A web browser

Objective #1: Create a t2.micro EC2 instance with the OS of your choice (Just make sure it is free tier).

  1. Once signed into AWS, search EC2 then Launch Instance
  2. Name your server
  3. In Application and OS Images (Amazon Machine Image), select your AMI (I stuck with Amazon Linux since I’m familiar with it)

4. In Instance Type select a free tier type

5. In Key Pair create or add existing key pair name

6. In Network Settings ensure that port 80 and port 443 are open for inbound traffic

Objective #2: In the user-data field, use a script that updates all packages, installs Apache, and starts the Apache service.

  1. In Advanced Details, scroll to the bottom until you see User Data
  2. We need to create a script next

3. If you are using an operating system other than Amazon Linux AMIs, you will need to use a different package manager. Yum is the default package manager for Amazon Linux AMIs, but other operating systems may require the use of a different package manager.

4. Launch Instance

Objective #3: Verify that the instance has the apache webserver downloaded and installed through the public IP

  1. SSH into your EC2-instance to connect

2. You can verify that the Apache web server has been installed and started by accessing the public IP of the EC2 instance. Replace <public-ip> with EC2 public IP.

http://<public-ip>

3. You should see the Apache welcome page if it’s been installed and started successfully

Advanced

Do all of this through the AWS CLI

Objective #1: Create a t2.micro EC2 instance with the OS of your choice through the AWS CLI (Just make sure it is free tier).

  1. Since I have Windows I need to download and run the AWS CLI MSI installer for Windows (64-bit)

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi

2. To confirm the installation, use the following command. **if it doesn’t work, close the terminal and re-open

aws --version

3. Install and set up the AWS CLI on your local machine

4. Before we run the aws ec2 run instances command we need to locate the AMI ID and create a Security Group

5. We need to find the VPC ID of the default VPC to create a security group because the security group needs to be associated with a VPC to control the network traffic to and from the instances launched in that VPC. Use the following command:

aws ec2 describe-vpcs --filters "Name=isDefault,Values=true"

6. Now that we have our VPC ID, we can create a security group. Enter the following command **create a name for your security group (add port 22 and 80) and enter your VPC ID

aws ec2 create-security-group --group-name my-security-group --description "My security group with port 22 and 80 open" --vpc-id vpc-02976ace0eb9d3eb7

7. After creating our security group we now have our security “GroupId” which we will need for the aws ec2 run-instances command.

(Here is a great article on creating security groups with aws CLI.)

8. Let’s make sure Port 22 is good to go **enter your security group name

aws ec2 authorize-security-group-ingress --group-name my-security-group --protocol tcp --port 22 --cidr 0.0.0.0/0

9. Lets make sure port 80 is good **enter your security group name

aws ec2 authorize-security-group-ingress --group-name my-security-group --protocol tcp --port 80 --cidr 0.0.0.0/0

10. To locate the AMI id click on the Instance ID

11. Then under Details on the right-hand side you’ll see AMI ID

Objective #2: In the user-data field, use a script that updates all packages, installs Apache, and start the Apache service through the AWS CLI.

  1. This is where I got stuck for a day. But after much research, we needed to create a string variable $userData that contains the necessary user data script. Once you have created the $userData variable, you can use it in your aws ec2 run instances command. The command should look something like this: **I’m using Windows PowerShell
$userData = "#!/bin/bash `n` 
sudo yum update -y `n`
sudo yum install httpd -y `n`
sudo systemctl start httpd `n`
sudo systemctl enable httpd"

(This article explains user data and the aws CLI)

2. The command below will run the instance with the user data script stored in the $userData variable. The script will be executed when the instance is launched. It is important to note that the user data script must be in the correct format for the command to work. **enter your ami ID

aws ec2 run-instances - image-id ami-xxxxxxxx - count 1 - instance-type t2.micro - user-data $userData

3. Now that we have everything we need let’s continue. After running the below command you should see the following *remember to insert your personal ami ID and security group ID. Thank you Zaire for noticing I had keyname on here which we didn’t need since we were bootstrapping.

aws ec2 run-instances --image-id ami-XXXXXX --count 1 --instance-type t2.micro --security-group-ids sg-XXXXX --user-data $userData

Objective #3: Verify that the instance has the apache webserver downloaded and installed through the public IP through the AWS CLI

  1. You can check the public IP address of your EC2 instance by running the following command:
aws ec2 describe-instances --query "Reservations[*].Instances[*].PublicIpAddress" --output=text

(Here is a great article on describe-instances with aws CLI.)

2.To view the content, open your web browser and enter “http://YOUR-PUBLIC-EC2-IP-ADDRESS" in the address bar

http://52.86.142.208

3. Now let’s stop our instance, to locate the instance ID type the following command. This command will filter the results to show only running instances.

aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query "Reservations[].Instances[].InstanceId" --output text

4. Enter the command below with your instance-id to stop the instance

aws ec2 stop-instances --instance-ids i-0c4d115f75c46c973

(This is a great article on how to stop instance via CLI)

In conclusion, configuring an AWS EC2 instance and Apache web server can be a daunting task for beginners, but with the right guidance and resources, it can be a rewarding experience. Through this article, I have provided a step-by-step guide to setting up an EC2 instance and Apache web server, as well as some tips and tricks to make the process easier. With the knowledge gained from this article, you will be able to confidently launch your own EC2 instance and configure an Apache web server.

--

--