Hosting a static website on AWS EC2.
Tags: aws, cloud, host, devops
Published: 2022-08-31 16:59:23

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
  2. Hostinger
  3. Bluehost
  4. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/769h01hocplr3n0cg5j9.jpg)
After clicking u will see the Nameservers as shown below

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fasb5mlqoccis6y0h7o5.jpg)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4a5s6eoizm2gw7hdh1yv.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4n3w7usdp9bgyukcompq.jpg)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ohz7eqzsv6vu27vcvlun.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6oosz20knwol48vjdgt.png)

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

- Connect to the root by following the below commands.

```
sudo su
cd
```

- 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_
[](https://winscp.net/eng/download.php) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yujgf44shf0iwfa5lrhs.png)
Then give ec2-user as user-name.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iow8x8szawtilkqo4v9c.png)
Then move your file to the /var/www/html directory.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/194sg9e147sare3yip5t.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq9agdiwi58qyb3yymf7.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wuxfvwgncmpej2fhhi9.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56tyn1uegmf726sj8day.png)
There you go. If your website is successfully deployed you can check by hitting your subdomain and domain name on the browser.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l11otlgvdow3xudafofa.png)
**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/
  ```
   -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
   ```
-Enable EPEL as shown in the following command.

```
sudo yum-config-manager --enable epel*
```
-You can confirm that EPEL is enabled with the following command.

```
sudo yum repolist all
```
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
...
```
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>
```
Save the file and restart Apache.

```
sudo systemctl restart httpd

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp0lktp77x3oz0eaiywf.png)




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