Host a Dynamic Website on AWS

Host a Dynamic Website on AWS

In this article, I’d like to demonstrate how you can deploy a dynamic website with AWS by uploading your website content into S3 bucket, create an EC2 instance to host web app on it as in this scenario EC2 acts like a public server all people from the world can visit this server.

First of all, let me explain some of the terminologies.

What is Amazon S3?

Amazon S3 (Simple Storage Service) is a service offered by AWS for object storage through a web service interface. It can be used to store or retrieve any amount of data such as documents, images, videos, etc.

S3 bucket?is a resource in Amazon S3. It is a container where files and folders can be uploaded.

What is Amazon EC2?

Amazon EC2 (Elastic Compute Cloud) is a service offered by AWS. It is considered as a virtual server.

What is IAM Role?

IAM ( Identity and access management ) Role is used to give permission to service to do something on another service.

What is LAMP web server?

You can use this server to host a static website or deploy a dynamic PHP application that reads and writes information to a database. You can know more from here.

Prerequisites

If you’d like to follow this tutorial, please make sure the following requirements are met.

  • Create AWS account. You can sign up? here and follow this tutorial in setting it up.
  • ?Download MobaXterm.
  • Download a free source code from here.

Table of Contents

  1. Create S3 Bucket
  2. Upload web files to S3 bucket
  3. Create IAM Role
  4. Create an EC2 instance
  5. SSH with MobaXterm
  6. Install a LMP web server on Amazon Linux2

Now, let’s get into it!

Step 1 : Create S3 Bucket

You will need to create an S3 bucket to put your website’s files and folders.

To do this, login into your AWS management console and click on?Services?on the top navbar. From the?Services?drop-down, select?S3?from the?Storage?section. This should display the?S3?dashboard.

No alt text provided for this image

From the S3 dashboard, click on?Create bucket. Give the bucket a unique name, the name you choose must be globally unique.

Next, choose your preferred?AWS Region?from the drop-down.

No alt text provided for this image

Under?Block Public Access settings for this bucket?section, check the?Block all public access?checkbox. This is done to make the bucket not accessible to the public.

No alt text provided for this image

Click on?Disable?for Bucket Versioning.

You can also?Add tag?to the bucket for easy identification.

No alt text provided for this image

Under?Default encryption?section, click on?Enable?for Server-side encryption. Then check Amazon S3 Key (SSE-S3).

No alt text provided for this image

Then click on?Create bucket.

No alt text provided for this image

Step 2?: Upload web files to S3 bucket

After creating the bucket, you need to upload your website’s files and folders into it.

From the?S3?dashboard, click on the?name?of the bucket you just created.

On the?Objects?tab, you can see that the bucket is currently empty, click on the?Upload?button.

No alt text provided for this image

This should take you to the?Upload?page. Drag and Drop the website files that was downloaded from this .

No alt text provided for this image
No alt text provided for this image

After the necessary files and folders have been added, scroll down and click on?Upload.

The uploading should be done in a few minutes depending on your network and content size. Also, please do not close the tab while the upload process is going on.

Step 3 : Create IAM Role

Now, EC2 want to pull code from S3. So you want to create IAM Role to give EC2 permission to access S3.

To do this, from the?Services?drop-down, select?IAM?from the?Security, Identity& Compliance?section. From the IAM dashboard, click on Roles. Then Click on Create role.

No alt text provided for this image

Choose EC2 and click Next: Permissions.

No alt text provided for this image

Search for S3 and check AmazonS3FullAccess. Then click Next: Tags.

No alt text provided for this image

Click on Next: Review.

No alt text provided for this image

Give the role name and description. Then click on Create role.

No alt text provided for this image

Now, the role has been created successfully.

No alt text provided for this image

Step 4 : Create an EC2 instance

You will need to create an EC2 instance to install apache ( /var/www/html ) and copy the content of S3 to html directory.

To do this, from the?Services?drop-down, select?EC2?from the?Compute?section. This should display the?EC2?dashboard. From the EC2 dashboard, click on Launch instance.

No alt text provided for this image

For AMI, choose Quick Start and click on Select for Amazon Linux (Free tier eligible).

No alt text provided for this image

For an instance type, choose t2.micro (Free tier eligible). And click on Next: Configure Instance Details.

No alt text provided for this image

Determine 1 for Number of instances, default vpc for Network and Default in us-east-1a for Subnet.

No alt text provided for this image

Choose ec2s3role or whatever you named for IAM role. And Terminate for Shutdown behavior. Then click on Next: Add Storage.

No alt text provided for this image

Click on Next: Add Tags.

No alt text provided for this image

You can add tag Name: DynamicSite. Then click on Next: Configure Security Group.

No alt text provided for this image

Select Create a new security group. Give it Name: DynamicWebsiteSG and description: SG for DynamicWebApp. For SSH rule select My IP?for Source. Click on Add Rule and select?HTTP?for Type and?Anywhere?for Source. Last rule select?HTTPS?for Type and?Anywhere?for Source. Click on Review and Launch.

No alt text provided for this image

Click on Launch.

No alt text provided for this image

Select Create a new key pair and RSA for type. Give it name WebServerKey and click on Download Key Pair. Note: You should download the key to can ssh on EC2. Click on Launch Instances.

No alt text provided for this image

Now, instance is launching successfully.

No alt text provided for this image

Click on Review Instance and wait Status check will be 2/2 checks passed.

No alt text provided for this image

Step 5 : SSH with MobaXterm

Now, you want to connect to EC2 by using MobaXterm. First you should copy public IPv4 address of EC2 instance.

No alt text provided for this image

Open MobaXterm and start a new remote session by clicking on Session.

No alt text provided for this image

Click on SSH. Paste IP of your EC2 For example:(3.86.76.216). And ec2-user for Specify username. Click on Advanced SSH settings, check Use private key and browse location of key. Click OK.

No alt text provided for this image

Now, you connected to EC2 successfully.

No alt text provided for this image

Step 6 : Install a LAMP web server on Amazon Linux 2

The following procedures help you install an Apache web server with PHP and?MariaDB.

To ensure that all of your software packages are up to date, perform a quick software update on your instance.

sudo yum update -y        

Install the?lamp-mariadb10.2-php7.2?and?php7.2?Amazon Linux Extras repositories to get the latest versions of the LAMP MariaDB and PHP packages for Amazon Linux 2.

sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2        

Now, you can install the Apache web server, MariaDB, and PHP software packages.

sudo yum install -y httpd mariadb-server         

Start the Apache web server.

sudo systemctl start httpd         

Use the?systemctl?command to configure the Apache web server to start at each system boot.

sudo systemctl enable httpd        

You can verify that?httpd?is on by running

sudo systemctl is-enabled httpd         

Now, you want to copy content of website from S3 to directory /var/www/html in EC2 . Make sure you copy your S3 bucket name.

sudo aws s3 cp s3://dynamicwebappsm --region us-east-1 /var/www/html/ --recursive        

To verify that content is copied to /var/www/html .

cd? /var/www/html 
ls        

Copy public IPv4 DNS and paste it in a new tab.

No alt text provided for this image

Congratulations, you have deployed a dynamic website on EC2 successfully.

No alt text provided for this image

If you found this article helpful, please leave a comment.

Omkar Dharkar

?? Aspiring Data Scientist | Applied Data Science | Machine Learning | Big Data Analytics | Data Visualization | Python | R | SQL ??

3 周

thank's so much , but how am i going to setup my db ?

回复
Jabulile Mathaphelo Nene

AWS DevOps engineer Professional

1 个月

Well Detailed

回复
suvarn Patel

UDLR marketing.com (initiative)

4 个月

I take a few days to understand

回复
Kunal Sevak, BTech-IT, PGDM-Predictive Analytics, CSPO?

Strategic Data Leader | InsureTech Canada | Driving Data Governance | Helped Design 100% Compliant Data Repository | Boosted Actuarial & Underwriting Machine Learning Model Accuracy from 69% to 81%

1 年

Thank you Sara Mostafa, this was helpful.

回复
Alan Branch

Grammy Award Winning Recording and Mix Engineer, Producer, Songwriter

1 年

stuck at installing apache I get the error - sudo: amazon-linux-extras: command not found, using terminal in OSX

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了