Deploy a Node.js Application On AWS EC2 Server

Deploy a Node.js Application On AWS EC2 Server

In this article, I will show you how we can easily deploy node js application on Amazon EC2 server and run on a live URL

Step-1: 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.

Congratulations! You're connected with your EC2 remote server.

After generating the key pair click on the Launch Instances button.

1: Log in to EC2 instance using SSH

2.In terminal type below command to update packages :

sudo apt update

Step-2 : .Install Node/NPM

curl -sL | sudo -E bash -

sudo apt install nodejs

node --version

Step-:3 Git Repo Setup

Type beloe command to create direcory and move to that directory:

mkdir websites

cd websites

Step-4 : Clone your project from Github

git clone yourproject.git

go to your project using

cd yourProjectName

install depencies

npm install

Step-5 : Run app

Now you can run your application using

node app.js


npm start (or whatever your start command)

then go to borwser and use your port with IP adress,something like

Step-6 : Security grupe setup

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.

However, we don't want to use port to access our website, so here we need to use NGINX

Step-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:

Setup PM2 process manager to keep your app running

Innstall Pm2 globally

sudo npm i pm2 -g

Switch to our app directory and run

pm2 start app (or whatever your file name)

 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:

sudo 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

Great! You can access the app on your browser also.

You should now be able to access your app using your IP and port. Now we want to setup a firewall blocking that port and setup NGINX as a reverse proxy so we can access it directly using port 80 (http)

Step-5 : Setup ufw firewall

sudo ufw enable
sudo ufw status
sudo ufw allow ssh (Port 22)
sudo ufw allow http (Port 80)
sudo ufw allow https (Port 443)

Step-6 : Install NGINX and configure

sudo apt install nginx
sudo nano /etc/nginx/sites-available/default

Add the following to the location part of the server block


    location / {
        proxy_pass http://localhost:5000; #whatever port your app runs on
        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;

Check NGINX config

sudo nginx -t

Restart NGINX

sudo service nginx restart

You should now be able to visit your IP with no port (port 80) and see your app.

Add SSL with LetsEncrypt

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
sudo certbot --nginx -d -d

# Only valid for 90 days, test the renewal process with

certbot renew --dry-run

Subscribe to our Newsletter

Stay up to date! Get all the latest posts delivered straight to your inbox.

If You Appreciate What We Do Here On TutsCoder, You Should Consider:

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Leave a Comment