How to Host an HTML Website on an EC2 Instance: Using Terraform

Emmanuelibok
7 min readMay 22, 2023

--

Hosting a HTML website on AWS/TERRAFORM

Requirements:

Aws,

Terraform and

Vscode

Case Study and Introduction:

In the second quarter of 2023, a start-up internet landmarktech business intends to build a website that can handle the demand for visitors from foreign workers sending money to their families. The organization wants to make sure that their website is easy for their clients to use. In this tutorial, we will host a website on Amazon EC2 using HTML and an Apache server.

HTML is a lightweight language that can readily interface with other languages. It will also benefit from EC2’s excellent availability, security, cost-effectiveness, and scalability.

Once the website is live, we will SSH into EC2 using Terminal to ensure that everything is operating properly. We will clear up the resources we used once we have completed all of the stages.

Objectives:

  • Create a VPC.
  • Make a subnet and a region for this project.
  • Create a security group and allow ports 80, 443, and 22.
  • Create an instance.
  • Check if you can connect to the Apache2 web server.
  • Clean up

Step 1: Select your preferred cloud provider. I’m sticking with AWS.

Step 2: Establish a VPC.

Step 3:Make a subnet and a region for this project

I’m utilizing two subnets in various availability zones in my situation.

Step 4 Create a security group and allow ports 80, 443, and 22.

Write the name and description of your security group on the block designated for it. Choose the VPC you made in step 2 from the list. In our situation, we chose the VPC we established.

The first rule we’re going to add is for ports 443 and 80. Scroll down under Inbound Rules. Internet users will be able to visit our website after this port has been opened. Select HTTP — always on ports 443 and 80, then click Add Rule.

The next thing we’re going to do is enable port 443 and 80 traffic from any source on the internet under Source. Select Anywhere on IPv4 under Source to bring up 0.0.0/0 automatically.

Inbound regulations are still in effect. We’re going to choose SSH as the next rule. The TCP protocol and the range of ports 22 will then appear. Your IP address will be displayed if you choose My IP under Source.

This implies that only traffic coming from your IP address will be permitted to use port 22. You can SSH into your EC2 instance using port 22.

You should always limit the IP address that can SSH into your EC2 instance to your own IP address for security best practices.

Step 5: Create an instance.

In order to connect to your EC2 instance using SSH, you will include SSH.

Once your Terraform configuration code is complete,

Initialize Terraform in the directory containing your Terraform configuration file by running the following command:

terraform init

This command initializes Terraform, downloads the necessary provider plugins, and sets up the backend configuration.

Plan your infrastructure deployment by running the following command:

terraform plan

This command shows you the execution plan, including the changes that will be made to your infrastructure.

If the plan looks good, you can apply the changes and deploy your infrastructure by running the following command:

terraform apply

Terraform will prompt you to confirm the changes before applying them.

Review the changes that Terraform intends to make. If you are satisfied, enter yes to proceed with the deployment.

Terraform will now provision your infrastructure according to your configuration. You can monitor the progress in the command output.

After the deployment is complete, Terraform will provide the final output, including the details of the resources created.

Entering your EC2 instance requires using ssh.

The next command to run is sudo apt update. When we see the green show prompt, that signifies we can run the next command.

Next, let’s type Sudo apt install apache2

The next command means change directory," meaning we want to go to the HTML directory. Let’s type cd /var/www/html

Previously we were in the ec2 directory

Notice that we’re already in the html directory after performing the command.

The next command we need to type in our terminal session is wget.

https://github.com/azeezsalu/techmax/archive/refs/heads/main.zip — a

Let’s define each command:

Wget is a Linux tool that is used to get files into our EC2 instance.

https://github.com/azeezsalu/techmax/archive/refs/heads/main.zip

The URL of the file that we are attempting to download.

It was extracted from the main.zip file. To see the file, type ls.

Once the web files are downloaded into our EC2 instance, they will be placed in this zip folder, which brings us to the next stage.

Step 6: Place the web files in the HTML folder.

Keep in mind that we are in the html directory. To view our website, we must extract all of the web files from the zip folder and place them in the html directory. This is what our next command will do. Let’s try unzipping main.zip.”

The ls command displays the zip folder as well as the unzipped version.

The next step is to put all of our web files from the unzipped version into the html directory. Type sudo cp -r techmax-main/* /var/www/html/ to get started.

cp means copy

When I type the ls command again, it no longer displays the main. zip and techmax-main

That’s it! We are now ready to launch the Apache service.

Step 7: launch the Apache service.

Let’s run these 2 commands systemctl enable apache2 and systemctl start apache2

To see if Apache2 is up and running, type systemctl status apache2.

Let’s return to our AWS console and then to the EC2 instance dashboard. Select the Web-Server instance and go to the details section below. Make a note of the public IPv4 address.

aws console

Let’s open another tab or window and paste the address that we copied in the address bar.

There you go! We can access our website.

Conclusion and Clean Up:

A new internet landmarktech company is employing HTML and an Apache server to run a website on Amazon EC2. To accomplish this, they will construct a VPC, a subnet and region, a security group, an instance, test connectivity to the Apache2 web server, and clear resources. Create a security group and grant access to ports 80, 443, and 22. Allow traffic from any internet source. Select SSH as the next rule.

Make an instance that supports SSH. To ensure security, limit the IP address that allows SSH into the EC2 instance to your own. The processes to launch Terraform, design an infrastructure deployment, apply changes, monitor progress, and enter data are the most significant details in this text.

Terraform destroy must be used to end our EC2 instance so that we don’t pay any fees.

At this point, Terraform will be so hot.

In addition, I would appreciate it if you could “clap” or leave a comment below. It enables this type of content to reach more DevOps professionals.

--

--

Emmanuelibok

As a DevOps pro, I automate workflows, boost reliability, and foster teamwork. My goal: agile, secure, and efficient software delivery.