How to host a static website in AWS EC2

How to host a static website in AWS EC2

Amazon Elastic Compute Cloud is a part of Amazon.com's cloud-computing platform, Amazon Web Services, that allows users to rent virtual computers to run their own computer applications.

In layman's terms, imagine it as another computer in some location where we have remote access. It is designed to make Web-scale cloud computing easier for developers.

Let us Deploy a Static Website in EC2 (Prerequisite: AWS account )

Step1: Goto AWS Management Console, Search for EC2 instance and click on it. 1.PNG

Step2: On EC2 Dashboard Click on Launch instance

2.PNG

Step 3: Give a Name for the instance that you are going to create (e.g : MyDemoWebsite). You can also give additional tags.

3.PNG

Step4: Choose Amazon Linux 2 and Instance type t2.micro (Since i am using AWS Free tier)

Amazon EC2 provides a wide selection of instance types optimized to fit different use cases. Instance types include varying combinations of CPU, memory, storage, and networking capacity. you can use any according to your needs.

4.PNG 5.PNG

Step5: Click on "Create a new pair" and give a name say "MyDemoWebsite" to it.

6.PNG 7.PNG 8.PNG

Now click "Download Key Pair" and note the folder where it gets downloaded as we will need it later.

Step6: Add Storage

Amazon EC2 stores data on a network-attached virtual disk called Elastic Block Store. You will launch the Amazon EC2 instance using a default 8 GiB disk volume but you can get up to 30 GiB in the free tier. This will be your root volume (also known as a 'boot' volume).

10.PNG

Step7: Configure Security Group

check all the three box

  • Allow SSH traffic from

  • Allow HTTPs traffic from the internet

  • Allow HTTP traffic from the internet

This allows the external traffic from the internet to communicate with your instance.

9.PNG

Note: There are other things to do as well since we are launching a simple static site so, we are considering basic requirements only

Step8: Click on Launch instances and wait till the instance is ready

11.PNG 12.PNG

Step8: Click on your running instance and copy the Public IPv4 address

(Note: This IP address changes every time we terminate and start the instance. So, we need to attach Elastic IP but we are not doing this for now) 12.PNG 13.PNG

Step9: Connect the instance

We have several different ways to connect to an instance using putty, PowerShell, and even directly through the Amazon Console. I have used PowerShell but you can use your convenient method.

  • Launch Powershell and go to the folder you have your "MyDemoWebsite.pem" file.
  • To connect your instance use the below structure
ssh -i <name of key file><space><Public ipv4 DN>
ssh -i MyDemoWebsite.pem ec2-user@44.202.161.172

and type yes

you are now successfully connected to your instance

14.PNG

Step10: Installing a Web Server

  • First elevate your privileges
    sudo su
    

15.PNG

  • Update all of the packages on the instance
yum install httpd -y
  • Starting the webserver
service httpd start

Step11: Check server Status

  • Copy Public IPV4 of your Instance .

13.PNG

  • Paste this on your browser you will see a test page as shown below

16.PNG

Step12: Adding static Website file

By default, the apache web server will display the index.html file found in /var/www/html directory in the root path of your website so go to that directory.

cd /var/www/html
  • Create index.html file
nano index.html
  • Add contents to your HTML file
'<h1>' Congratulations !! you have sucessfully deployed static website in amazon EC2</h1>
  • Boom!! You are all Done .

18.PNG

Thank you !!

Did you find this article valuable?

Support Anup kafle by becoming a sponsor. Any amount is appreciated!