Node JS server in aws-ec2
Host your node js project in aws-ec2 server with the steps given below.
Assuming you have created your ec2 instance,selected outbound rules and logged into server using ssh configuration we move forward.
Step 1: Install Node js , Pm2, nginx and git
cd ~
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install git
npm install pm2 -g
sudo apt install nginx
git CLONE_YOUR_PROJECT
sudo pm2 start build/index.js
Node installation guide from : https://github.com/nodesource/distributions/blob/master/README.md
Step 2: Install ruby and code Deploy agent
sudo apt install ruby-full
sudo apt install wget
cd /home/ubuntu
wget https://aws-codedeploy-ap-south-1.s3.ap-south-1.amazonaws.com/latest/install (for mumbai region)
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent start
sudo service codedeploy-agent status
see Resource kit bucket names by Region. see code-deploy installation guide
Step 3: Setting Up Nginx as a Reverse Proxy Server
sudo nano /etc/nginx/sites-available/default
server {server_name posapi.sathifood.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;proxy_redirect off;}}server {if ($host = posapi.sathifood.com) {return 301 https://$host$request_uri;} # managed by Certbotlisten 80;server_name posapi.sathifood.com;
return 404; # managed by Certbot}
sudo nginx -t (this should return ok status as belows:)
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
sudo systemctl restart nginx
That’s it . Now you will have a node js server running on your http(not https:// will need to use certbot for ssl) with a custom name i.e posapi.sathifood.com as in above example.