Link Domain to EC2 server Nodejs

if you don’t know how to get start with EC2 https://medium.com/@nvtuehcmus/complete-your-web-app-on-aws-ec2-a3db072b16e4 following the article first

This article I will introduce to you how to link you domain to Web app on EC2

  • go to services find or search Service name Router 53

create hosted zone

you should to create hosted zone enter your domain you bought

addition some record

  • type A value is public ip of EC2 server

copy:

ns-81.awsdns-10.com.

ns-1945.awsdns-51.co.uk.

ns-1465.awsdns-55.org.

ns-952.awsdns-55.net.

go to domain manage -> name Server manager edit nameServer to point to host on aws seem like

or contact to the company you bought domain

you should waiting about 24 hour to update nameServer

in this time we will go back to EC2 to config some thing

the “things” you need is Nginx, pm2

let’s set up:

$ sudo apt-get update && sudo apt-get upgrade -y
$ sudo apt-get install nginx -y

Setup Nginx

Check status of Nginx and start it using the following commands

$ sudo systemctl status nginx    # To check the status of nginx
$ sudo systemctl start nginx # To start nginx

Make sure that Nginx runing on system startup

$ sudo systemctl enable nginx

Nginx should be up and running,

Setting up Nginx as a reverse proxy for Nodejs application

$ wget -q -O — ‘http://169.254.169.254/latest/meta-data/local-ipv4'$ sudo rm /etc/nginx/sites-available/default
$ sudo vi /etc/nginx/sites-available/default

paste config, and change your_domain.com

server {
listen 80; server_name your_domain.com; location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

Test the configuration of Nginx using the command below

$ sudo nginx -t

Then reload Nginx if OK is displayed.

$ sudo /etc/init.d/nginx reload

.PM2

Install PM2 Advanced, production process manager for Nodejs and start Nodejs app with PM2

npm i -g pm2pm2 install ts-nodepm2 start index.ts 

we going to. http://your_public_ip:3000 to check process is open

http://your_public_ip to check successfully to redirect from port 3000 to port 80

and then waiting NameServer is update and try: http://yourdomain

good luck, and thank you

Reference :https://medium.com/@utkarsh_verma/configure-nginx-as-a-web-server-and-reverse-proxy-for-nodejs-application-on-aws-ubuntu-16-04-server-872922e21d38