Hosting your Website using Linux EC2 Instance in AWS

Harshith Shetty
5 min readMar 27, 2021

If you are a beginner in the cloud computing the first most thing to try in cloud computing is to host your website on cloud.

All the cloud services Provider provides various services to host your website in their cloud. For this article, I will concentrate on the AWS Cloud Service provider and its service. For Hosting your Website AWS provides services such as EC2 instance (will we concentrate on Linux ).

Before Starting The Hosting of your Web site First, let's be ready with our Code. For website content and code, I am going to just use the Templates, which are available online for free. You Can download the template from the Freebiesbug. For this article I have used the Hook: A dark HTML landing page template.

Download the Code of the template, extract it, and create a git repository. Now we will start the hosting.

There are various Service you can use to host your website in AWS. We will see the common Services one by one.

Hosting On Linux EC2 Instance :

You can host your website on the cloud using EC2 instance by following the few steps :

Step 1: Launch the Linux Ec2 Instance

Go to AWS console and search For EC2 and then Select Launch Instance option in Instance.

To launch the Ec2 Instance, there are total 6 steps
1. Choose AMI
2. Choose Instance Type
3. Configure Instance
4. Add Storage
5. Add Tags
6. Configure Security Group
7. Review

  • Now Choose an Amazon Machine Image (AMI) as Amazon Linux 2 AMI (HVM), SSD Volume Type (64-Bit [x86]). You can select other Linux version of Ec2 instance while i have selected the instance which is included in the free tier.
Choose AMI
  • Choose an Instance Type as t2.micro, it is sufficient to host a static website and also include in the free tier.
Choose Instance Type
  • In configure Option, select your VPC in network option. If you don’t have an VPC, create the VPC in VPC management Console and leave rest of the option a default
Configure Instance
  • by default it will allot you with 8GB EBS for snapshot leave that as it because that is sufficient for our hosting.
Add Storage
  • For tags you can add it or else it is fine for hosting setup.
Add Tags
  • In Security Group, create a security group and add SSH, HTTP and HTTPS Rules or else if you have an existing Security Group with this 3 rules you can use that. SHH — it is used to connect the Linux server remotely from your device. HTTP & HTTPS is used for web.
Configure Security Group
  • In review section, you can see all the setting of the EC2 instance once you have confirmed with setup click on Launch button. It will ask to Create security Key (.pem) or use the existing one you can create by just giving it a name and download it. Keep the security key safe with you.
Review -1
Review -2

Step 2: Create Your Security Key for SSH connection to the Instance

  1. First Install putty.installer.msi (package file) from here.
  2. Open PuTTYgen on your device, click on load button and select the .pem file which you downloaded while launching instance (note: if you can’t see you .pem file in your files that is because while searching the default file format that is .ppk is selected. So change it to all files).
  3. Save private key.

Step 3: Connect To Ec2 Instance

  • Open PuTTY.
  • Under Session Category, In Host name Enter ec2-user@ [Public IPv4 address] for Example ec2-user@129.42.56.189
  • Now Select SSH category and then select Auth sub category and now Browse and select the security key in (.ppk format)
  • Click on the open button and it gets connected to the Linux Command line of your instance.

Step 4: Install LAMP SERVER (Linux, Apache, MySQL, PHP/Perl/Python)

  • After Connecting to your Linux Instance Update the software of the instance by running command,
sudo yum udate -y
  • Now we will install lamp-mariadb10.2-php7.2 and php7.2 by the command
sudo amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2
  • To install Apache web server, MariaDB and PHP software package runt he command
sudo yum install -y httpd mariadb-server
  • Now lets start the Apache Web Server
sudo systemctl start httpd

this Command will start the Apache Web Server

  • To check whether the Apache web server is running. Run the below command.
sudo systemctl is-enabled httpd

if the Apache web server is running, you would the output as “enabled” .

  • To configure Apache web server to start with the start of your Linux Instance
sudo systemctl enable httpd

Step 5: Bring Your Code to The EC2 Linux Instance

  • Since Apache Web server is installed so now, we should go to the directory where the code has to be there
cd /var/www/html
  • Check whether the html folder is empty. If not, make it empty by using commands such as.
ls # list of all files and folder in the directory
rm -rf [directory name] # to remove directory
  • First to clone the repository we must install the git in Linux
yum install git -y
  • Clone the Repository To the var/www/html Folder
git Clone [URL of Repository] .

Remember . in git clone command clones the repository without Git Project Folder.

Finally, you have Hosted your Website in the Cloud

Just enter the Public IPv4 address in your Browser you would See your index.html in your Browser which is now in the cloud.

--

--

Harshith Shetty

Aspiring Cloud Architect. Learning and Exploring Cloud Services.