How to create and launch your own secured (HTTPS) web site using AWS EC2, Ubuntu, Nginx, AWS Route 53 and LetsEncrypt?
Tags: aws, ubuntu, nginx, letsencrypt
Published: 2023-04-06 15:15:47

This post explains how you can register your own domain using AWS Route 53, then hosting your own website using this domain over nginx server on an Ubuntu server and securing it (HTTPS) with LetsEncrypt

## Step-1 Get domain name in AWS Route 53
You need your public domain name like google.com or amazon.com in order to make the public users access it. Following steps shows how you can purchase a public domain name in AWS Route 53

- In AWS console, search Route 53
- Select ‘Register domain’. You can select your domain name with extension based on the availability and your price selection.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yeoc58hcty52zvfdplv9.png)

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

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

- Add to cart and add registrant contact details. 
- Check your contact details. Accept Terms & conditions and complete the order.
In some cases you may need to complete the payment through AWS billing so that your domain is enabled and available.
Once your have your domain name registered with AWS Route 53, you can continue to use this domain for yourself.
You should receive an email to confirm your domain as shown: (this would take some time say 30 min)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5oujlsjcyp9a0zrdpknr.png)
Confirm this email to further setup your server for website.

## Step-2 Host an EC2 instance in AWS with Ubuntu
Goto AWS EC2.
Click Launch instances.
Give a suitable name for your server
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9a6i037jciy2px0miba.png)
Select ‘Ubuntu’ as application OS image
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lirnfjsdweo53dcxgtk9.png)
Choose instance type according to your purpose. Here I am choosing `t2.micro` 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nf84quv75vcomsnzb49k.png)
Select ‘Create Key pair’ option and create a new keypair to generate a new permission file (*.pem)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5aspbt3bsff5emyapx9c.png)
Save this file and it will be available only during creation. Make sure you store it carefully so you don’t lose it.
Edit network settings and provide appropriate values

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5hj2hp4rce4hi1vz1io.png)
Under inbound security group rules, add the types HTTP and HTTPS
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wwn8mefxkby2d402mqg.png)
Configure storage based on your purpose. Here I am leaving it as default 8GB.
Review all and select ‘Launch instance’
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1z5uojw0tk4z6o83mc2.png)

## Step-3 Attach an elastic IP
Once the instance is successfully launched, go to Network&Security -> Elastic IPs

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdkoatdqq79i7xq4syf1.png)
Select ‘Allocate Elastic IP address’
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swtoippy87x11mdxzo8e.png)
Add new tag ‘Name’ and provide a reasonable name.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trculwfbp4nbf5wcmcjk.png)
Select ‘Allocate’ button.
Once the elastic ip data is created, use ‘associate this elastic ip address’ button to assign IP to your above created ubuntu server.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gh1zpp5snth8wcql2i8h.png)
Select your server and associate this IP with it
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfhd2zas6xfz9zsb9pk7.png)
Now you would observe this elastic IP is same as your EC2 instance IP address
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xoftadvf0nf7dk0akj62.png)

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

## Step-4 Install and Nginx
You can now connect to your EC2 instance and setup the website
- Use ssh command to connect to your EC2 instance (on windows try using [Cygwin](https://www.cygwin.com/) or [Git Bash](https://git-scm.com/download/win)
- To start with make sure you set your pem file downloaded with read-only permission
`chmod 400 <path to your pem file>`
- Connect to your server instance using the ssh command, server name and pem file
`ssh -i <path to your pem file> ubuntu@your_server_name`
- Input ‘yes’ to add your finger print
- Switch root user (as installations will be done as an root user)
`sudo su -`
- Update ubuntu 
`apt update`
- Install nginx
`apt install -y nginx`
- This would install nginx and would have started it in background. You can verify the nginx status by 
`systemctl status nginx`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdda236j1c295hin4o3y.png)
- Following are the key locations/files where the configurations and files handled by nginx are located:

view the logs
`/var/log/nginx`

view the configurations 
`/etc/nginx/nginx.conf`
`/etc/nginx/sites-enabled/default`
`/etc/nginx/conf.d/`

website location
`/var/www/html`
- Test the nginx configuration without loading using the following command
`nginx -t`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ko5j9n7ar6inm3trdm26.png)
- Place your website static files in this location `/var/www/html`. If the location does not exist, create the same. By default nginx creates this location and place a default index file 
`/var/www/html/index.nginx-debian.html`
- You can verify this by going to a browser and typing the following: (replace the IP address with your EC2 instance public IP address)
`http://your_public_ip_address`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5a5na5xpzt76mlz252u.png)
- Now we can continue to have our own web page developed and placed in this location: `/var/www/html/`

Contents of the page:
```<html>
<head>
    <title>My Own page</title>
</head>
<body>This is my own web page</body>
</html>
```
Placed in this location on a file `/var/www/html/index.html`

- In order to point nginx to this new index page, open the default configuration of nginx
`vim /etc/nginx/sites-enabled/default`
- Locate the directive `index` and modify such that our new file is mentioned there, and other file names are removed
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vnaqvw5apo8lczy41btl.png)
- Specify the server_name directive with your domain name created above
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhkyrs416e6gp7yq4u39.png)
- Test and reload the nginx

`nginx -t`

`systemctl reload nginx`
- Now if you refresh the URL above in a browser, you should see your own page

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19cwy3wyv3bv1zhrgm8x.png)
## Step-5 Test your website with HTTP
Now instead of using the IP address we will map the domain name what we created to this web site
Once you have the domain successfully registered,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rbelxa18pu3up8fr5uh.png)
- Go to ‘Hosted zone’ and select your hosted zone name
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nx5trw14zv7k51aje38v.png)
- Create a new record by specifying your instance public IP address
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5smhxb0tjpb21gmibdnr.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0s3tw1msmr2gdtbkxdgz.png)
- After the record is created, you can view the status. Make sure that this is in INSYNC
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yr906uls2wvfep9z32uu.png)
- You can verify this domain mapping in Ubuntu using the ‘dig’ command

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5opvxoygh8lh955aee3.png)
- The same can be verified in windows using ‘nslookup’
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/miqw8svq134yogg4qx1q.png)
- Once the above is in place, you now access your website using the domain name what you created
http://mugunthanselvaraj.link

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

## Step-6 Securing the website with https using letsencrypt

The current website what we have is http based and is not secured. In order to secure it with SSL/TLS encryption so that it can be accessed over https protocol, we can use letsencrypt to add a SSL certificate to our nginx server.
- In order to use certbot (letsencrypt) we need to have snap installed (package manager) in ubuntu. By default this is available. You can verify this by
`snap version`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sh7bql54mod3nuq9l9dm.png)
- Verify for latest version
`apt policy snapd`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7yg6hvwysiwnazyi9a26.png)
- Download and update it by using the following commands

`snap install core;  snap refresh core`
- Remove if you have already have a certbot installed

`apt-get remove certbot`
- Install latest certbot 

`snap install --classic certbot`
- Add it to your bin, so that it can be executed using certbot command

`ln -s /snap/bin/certbot /usr/bin/certbot`
- You can verify the cerbot version

`certbot --version`
- Continue to install ssl certificates for your nginx server

`certbot -–nginx`
This will ask for you email id, in-case you need to be contacted for SSL certificate renewal and notices
Type ‘Y’ for accept terms of service
- It would show the available server-names for which the certificate has to be installed for

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5dsaf09h032c9f4dizz.png)
- Just press enter and proceed. This would generate a ssl certificate and configure it in the nginx server.
- Now if you access the above website, it would be in https

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tab7w9jmf9je1chm8949.png)
Finally you have your own website configured in AWS EC2 instance severed through HTTPS protocol.

## Step-7 Auto renew certificate:
- You can schedule your server to auto renew your SSL/TLS certificate using the following command

`certbot renew --dry-run`
- This would create a timer, which check the SSL certificate expiry periodically and would auto renew it.
- You can verify this by the following command and checking the current timers running

`systemctl list-timers`

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