Deploting NodeJS code on an EC2 instance We will be deploying a chat application written in Nodejs in the following steps Launch an EC2 instance and SSH into it. Install Node on EC2 instance. Copy code on your EC2 instance and install dependencies. Start server to run forever. So let’s get started. 1. Create account on Aws Go to https://www.aws.com and create new account on aws. If you are already registered on aws then go to aws console. An AWS new account lets you try the free tier of Amazon EC2, Amazon S3 and Amazon DynamoDb services for 12 months. 2. Launch an EC2 instance Now that the signup process is complete, go to aws console and provide login credentials. Once you are logged in you can see all the aws services under the AWS Services section. Click on the EC2 Service under Computer Service,it will show us EC2 Dashboard page, on this page just click on Launch Instance button under Create Instance Section. 3. SSH into your instance So we have successfully launched our EC2 server. Now we will access the server from our local machine using terminal on Linux and putty on Windows. If you are using Windows you need to change your .pem key to .ppk format. You can follow this link to convert your key. Go to your local directory where you downloaded the private key. Go to AWS instances console and click on the Connect button and follow the simple steps. 4. Install Nodejs curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash . ~/.nvm/nvm.sh nvm install 8.10 Now that we are connected to the instance, we can start installing the required packages in the environment for your node.js app to work. Follow this link to install the nodejs on server. I will install the latest version of Node JS which is 12.x. Once you’re done with the installation type “node -v ” on your terminal, it will show you v12.4.0 on terminal screen. 5. Install Git and clone repository from GitHub OR upload your ZIP folder and unzip on WinSCP Install git on Ubuntu We don’t need to install git on ubuntu 18.04. It is already installed on our server. You can check using git --help command. If it is not installed you can install using the following command. sudo apt-get install git Clone app on server You can take a clone of simple Node js basic app by using this command on terminal. git clone https://aemwaqas60@bitbucket.org/aemwaqas60/node-basics.git After cloning the repository, run cd node-basics and see all the content of the folder using the ls command: 6. Start the node.js app We have successfully cloned the app on our server. Run the following command inside our project directory: npm install It will install all the required packages for the app. There are several ways to start our app but we will use simple command: node app.js Copy public DNS from your instances page, paste it on your web browser, and append port :3000 at the end of the DNS address. As you will see this will not work and there is nothing to show on browser and the browser connection will timeout. This is where the Security Group comes in. In the AWS management console, go to Security Groups, select the one not named ‘default’ (launch-wizard-1 or your security group name) and edit the Inbound rules. Access the URL again and you should be able to see the app content now. 7. Keep App running using PM2 The app is running as soon as you open the terminal and it will terminate as you will close the terminal. We will install PM2 (Production manager 2) to keep live our app after closing our terminal or disconnect from remote server. Run the following command: npm install pm2 -g It will install PM2 package globally on server. Switch to our app directory and run: pm2 start app.js Now even if you close the terminal and check the url in the browser, the app will be running. Sweet! For automatically running PM2 when the server restarts, issue the following command: pm2 start app.js pm2 save pm2 startup Now you can reboot the instance using sudo reboot and connect after 1 min. You can still see the app is running on port 3000 using: PM2 list # Or PM2 show app if you change some code or add new file in deployed code then have to use: sudo su pm2 restart all pm2 status pm2 save if api's roll back then, You can try: pm2 list pm2 kill or find the running PM2 process with: ps aux | grep PM2 then kill with: kill -9 [pid] Information :- 1. https://www.tutsmake.com/deploy-node-js-app-to-amazon-aws-ec2/ 2. https://ourcodeworld.com/articles/read/977/how-to-deploy-a-node-js-application-on-aws-ec2-server 3. https://hackernoon.com/deploying-a-node-app-on-amazon-ec2-d2fb9a6757eb 4. https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html 5. https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html for install apace web server- https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Tutorials.WebServerDB.CreateWebServer.html