Create an AWS EC2 instance in AWS to host Apache.
Peace readers! In this article I learned how to create an EC2 instance in AWS to host to an Apache web server with a custom webpage. To do this you will need an AWS account (free tier). Here is what we are looking to accomplish so let’s get started!
1. Launch an EC2 Amazon Linux t2.micro (free tier) in a public subnet.
2. Create a security group that allows inbound traffic on HTTP for 0.0.0.0/0 and allows inbound traffic on SSH from your IP address.
3. SSH into your EC2 instance and install Apache with a custom webpage using a BASH script.
Launch an EC2 Amazon Linux t2.micro in a public subnet.
We start in the AWS Management Console by searching EC2 in the search bar. It’s good practice to favorite your most used services.
Next, we select the Amazon Machine Image (AMI) we want to use. For this project it is the Amazon Linux 2 AMI (free tier) as shown below.
Create Key Pair
Key pairs allow you to connect to your instance securely. A key pair, consisting of a private key and a public key, is a set of security credentials that you can use to prove your identity while connecting to an instance. As you scroll down from creating your instance you will see the section to create a key pair.
After clicking on create new key pair this window will come up for you to create the key pair. Then select create key pair.
- Key pair name: Name of your choosing
- Key pair type: RSA (encrypted private and public key pair)
- Private key file format: .pem (for use with SSH)
Network Settings
For the network settings we want to create a security group that allows inbound traffic on HTTP for 0.0.0.0/0 and allows inbound traffic on SSH from your IP address. So, under network settings we do the following.
As you can see AWS sets things up for us through automation. You have a network name, a subnet that can be set by default, and a public IP address that is auto assigned. To complete the network settings select Allow SSH traffic from and Allow HTTP traffic from the internet.
BASH Script
Under user data in the instance wizard, you are able to write your bash script here. Later on in the terminal this or Vim did not work for me. So, I had to use Nano to get this portion done.
Create EC2 instance complete!
SSH into your Instance
Now that your instance is created you should see it on the AWS Management Console. Click on it.
Then check the box of your instance and click connect.
Here is where I ran into a roadblock because the instance wasn’t connecting. So, I had to go back into Security settings and change the security group settings.
After saving the settings and reconnecting we now have the EC2 prompt displayed.
As mentioned earlier in the article Vim was giving me problems. Erasing my script after each new line. So, I used Nano to get this portion of the project completed and it did not fail.
#!/bin/bash
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo echo “<h1> Peace welcome to the Blue Team! </H1>” > /var/www/html/index.html
Download Apache
Using the following commands:
sudo yum update -y
sudo yum install -y httpd
sudo systemctl start httpd
sudo systemctl enable httpd
Open a web browser
Open a web browser and use the public IP address to your webpage to see the custom page you created.