How to Deploy Dynamic Website to AWS EC2


A dynamic website is a type of website that contains dynamic pages that keep changing on different sessions, unlike static pages that require manual updating before the contents change. It supports both server-side scripting and client-side scripting in making the content of the website dynamic. A dynamic website display different content each time the website is opened. On a dynamic website, you can create user accounts, delete accounts and you can log in with correct credentials to a dynamic website. Amazon Elastic Compute Cloud (Amazon EC2) is a secure web service provided by AWS that has a computing capacity that can be resizable. It is also known as virtual machines which are designed to make cloud computing easier for solution architects. With EC2 there is no stop in your configuration capacity, it provides you with complete control of available computing resources.

If you want a guide on how to host a static website on S3 click here. In this guide we will be discussing how easy it is to deploy a dynamic website to AWS EC2.

Setting up your AWS Account

Create an account via

AWS Account Sign up

Log in to the AWS management console:

AWS Account Login

After you have successfully created an AWS account and logged in, then navigate to S3 under the services→storage option. The next step is to create an S3 Bucket where you will upload your dynamic websites files. If you want to know how to create an S3 bucket read this Hosting Static Website and Versioning on AWS S3

Follow the below steps as we create our EC2 and upload our dynamic files from the S3 bucket we created. But note that when one service needs another service there is a need to create a role.

Let’s create a role first in IAM before creating EC2.

  1. Under access management in IAM, click on Roles.
  2. Click on Create role
Creating Roles

3. Select AWS Service as the type of trusted service, we are picking this because we need a role for EC2. Under the common use cases click on EC2 and click on Next:Permissions.

Creating Roles

4. Since we need access to files on S3 search for AmazonS3FullAccess and select it and click next.

Adding S3 Full Access Role

5. On the review page enter the Role name and click Create role.

Creating S3 Full Access Role

Now our Role is created

Role Created

Now let’s go-ahead to configure our EC2.

6. Under compute services click on EC2

EC2 Compute Service

7. Click on Instances (running) and Launch instances

Instances Running

8. Now select your Amazon Machine Image. But in this guide we will be using Amazon Linux 2 free tier.

Selecting Amazon Machine Image

9. Click Next: Configure Instance Details.

This is the page you configure your instances by entering all needed details to launch your VM. Confirm that you select the IAM role we created earlier. Click Next: Add Storage. If you want you can add tags in the next page after adding storage.

Configuring your Instance

10. Click on Configure Security Group. This is a firewall that helps you to control the traffic to your instance. Add all the rules as shown in the image below: click on Review and Launch.

Configuring Security Group

11. Create a new key pair that is authorized for connecting securely to the EC2. This is very necessary for secured access to your instance. Save it and keep it secret to yourself. After saving the key then go ahead to launch the instance.

Creating a Key Pair

12. Next you need to download an application to SSH into the instance. I will recommend either AWS System Manager or MobaXterm but in our case we will be making use of the latter in this guide. Download MobaXterm from here get the installer edition.

MobaXterm Setup

13. Make sure you copy the Public IPv4 address or Public IPv4 DNS. it will be useful in the MobaXterm for SSH connection to the instance. Click on settings on the MobaXterm.

Mobaxterm Setup for Access into EC2 Instance

14. On the Persistent home directory location where you have the key pair. Click ok. It will request you restart the application, grant the request.

15. Click on Start local terminal and on the terminal type ls –l | grep “name of key pair” to confirm if the key exists on MobiXterm.

MobaXterm Key Pair Setup

16. Let’s connect to our instances from MobiXterm. Select the instances and make sure its running. Click on Connect.

Connecting your Instance

17. Click on SSH and copy the command shown as an example and paste to your MobiXterm terminal and enter.

Connecting Via SSH

18. Now you should get something like the image below that will confirm you are inside the EC2 Instance. The private IP of our EC2 Instance now display on the terminal.

Successful Connection to EC2 Instance

19. Is time to start entering the below commands one after the other to set up the web server environment on our instance and also copy the dynamic files into the /var/www/html/ folder. You can cd to this directory to confirm the directory exists and use ls to confirm all the files in it.

Commands needed to install webserver and copy your files from S3

20. Now copy the Public IPv4 DNS from the EC2 Console and paste it into a browser.Congratulation you just deployed a dynamic website to AWS EC2.

Webpage Displayed Successfully

I hope you found this blog post on How to Deploy Dynamic Website to AWS EC2 very interesting and helpful. In case you have any questions do not hesitate to ask in the comment section.

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x