DEV Community

meera123
meera123

Posted on

Hosting a static website on AWS EC2.

Hello everyone.
In this blog, we will be discussing hosting a static website on AWS EC2.

What I will cover
1:Buying a domain and hosting the domain on AWS ROUTE 53
2:Hosting a website
3:How to configure SSL

1:Buying a domain and hosting the domain on AWS ROUTE 53.

  • There are multiple buying options in the market for domain buying and hosting platforms like
    1.GoDaddy

    1. Hostinger
    2. Bluehost
    3. Freenom(free domains) But, I would suggest using GoDaddy. And if you want to buy only for practice purposes then you can go for Freenom. In this blog, I will be using GoDaddy.
  • After buying a domain from Godaddy. Go to manage DNS.

Image description
After clicking u will see the Nameservers as shown below

Image description

  • Then log in to your AWS console and go to ROUTE 53. There we will be hosting our DNS. To do that first create a hosted zone by giving your domain name.

Image description

  • After that, you will be seeing some nameservers on route 53 you have to copy that and paste that on Godaddy nameserver. Then your DNS hosting will shift from GoDaddy to AWS.

Image description

Image description

2: Hosting a website

  • Launch an EC2 instance. Here I am using amazon Linux 2. Make sure to allow port 80(HTTP) and port 443(HTTPS) for everyone.

Image description

  • After that connect to the instance using an ssh client.

  • Connect to the root by following the below commands.

sudo su
cd
Enter fullscreen mode Exit fullscreen mode
  • After connecting to the root install apache to make a web server by throwing the below commands. yum install httpd Then go to the following directory.

cd var/www/html

Here you need to move your website files.
For that, we will be using winscp
Here is the link to download winscp.

  • After installing winscp. Open WinSCP and paste your public IP of the ec2 instance in the hostname. Then go to Advance->Authentication. There you need to give your private key.

Image description
Then give ec2-user as user-name.

Image description
Then move your file to the /var/www/html directory.

Image description

  • Come back to your ssh client and check if the files are available in the/var/www/html directory or not by throwing

ls

  • If your files are there the throw below commands.
    systemctl start httpd
    systemctl enable httpd

  • Your website is ready for hosting. You can copy your IP in chrome and check whether the website is live or not.

Image description
Okay, you successfully hosted your domain and deployed your website on your server.

  • After completing the previous steps you need to register your IP with your domain.

  • For that go to hosted zone you have created in ROUTE 53 and click on create record there you have to create a record by giving www as a subdomain(Give names by using those you want your customers to come over to your website) and paste your public IP in the value box.

Image description

Image description
There you go. If your website is successfully deployed you can check by hitting your subdomain and domain name on the browser.

Image description
3:How to configure SSL using Let's Encrypt
1.Download the Extra Packages for Enterprise Linux (EPEL) 7
repository packages. These are required to supply dependencies
needed by Certbot.
-Navigate to your home directory (/home/ec2-user). Download
EPEL using the following command.

  sudo wget -r --no-parent -A 'epel-release-*.rpm' 
   https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/
Enter fullscreen mode Exit fullscreen mode

-Install the repository packages as shown in the following
command.

sudo rpm -Uvh dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-*.rpm
Enter fullscreen mode Exit fullscreen mode

-Enable EPEL as shown in the following command.

sudo yum-config-manager --enable epel*
Enter fullscreen mode Exit fullscreen mode

-You can confirm that EPEL is enabled with the following command.

sudo yum repolist all
Enter fullscreen mode Exit fullscreen mode

It should return information similar to the following.

[ec2-user ~]$ 
...
epel/x86_64                          Extra Packages for Enterprise Linux 7 - x86_64                               enabled: 12949+175
epel-debuginfo/x86_64                Extra Packages for Enterprise Linux 7 - x86_64 - Debug                       enabled:      2890
epel-source/x86_64                   Extra Packages for Enterprise Linux 7 - x86_64 - Source                      enabled:         0
epel-testing/x86_64                  Extra Packages for Enterprise Linux 7 - Testing - x86_64                     enabled:    778+12
epel-testing-debuginfo/x86_64        Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Debug             enabled:       107
epel-testing-source/x86_64           Extra Packages for Enterprise Linux 7 - Testing - x86_64 - Source            enabled:         0
...
Enter fullscreen mode Exit fullscreen mode

2.Edit the main Apache configuration file, /etc/httpd/conf/httpd.conf. Locate the "Listen 80" directive and add the following lines after it, replacing the example domain names with the actual Common Name and Subject Alternative Name (SAN).

<VirtualHost *:80>
    DocumentRoot "/var/www/html"
    ServerName "example.com"
    ServerAlias "www.example.com"
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Save the file and restart Apache.

sudo systemctl restart httpd

Enter fullscreen mode Exit fullscreen mode

Image description

Your website is ready.
I hope you will find this informative.:>

Top comments (0)