Deploy Your Node.js Application on AWS EC2 Server Like a Pro

Deploy Your Node.js Application on AWS EC2 Server Like a Pro

Amazon Web Services (AWS) EC2 is a great choice if you have written a Node.js application and want to put it on a cloud server.

However, especially for new users, setting up and configuring an EC2 instance can be challenging.

We'll walk you step-by-step through the process of deploying your Node.js application on an AWS EC2 server in this article.

Deploy Node.js Application On AWS EC2 Server step by step:

Requirements to Deploy Node.js app to AWS

  1. AWS account: To utilize AWS services, you must have an AWS account. 
  2. Terminal with SSH support: It establishes a connection to the EC2 instance. You can’t deploy Node.js to AWS without it.
  3. AWS PEM file: This file provides the safest and secure connection between your app and AWS. It is essential for security.
  4. An IP Address: With an IP address, AWS can locate your Node.js app and will provide zero interrupt deployment.

Launch an EC2 Instance

The first step is to launch an EC2 instance on AWS. To do this, log in to your AWS account and go to the EC2 dashboard. Click on the "Launch Instance" button and choose an Amazon Machine Image (AMI) that suits your needs. We recommend selecting an Ubuntu Server AMI, which comes with pre-installed Node.js and npm.

Next, select the instance type and configure the instance details. You can leave most of the options as default, but make sure to configure the security group to allow incoming traffic on port 80 (HTTP) and 443 (HTTPS).

Click here for step by step guide create it first.

SSH into your instance

So we assume you have successfully launched your EC2 server and have access to the server from your local machine using the terminal.

Go to the AWS instances console and click on the Connect button and copy the connection string.

Connect-Ec2-Instance
Connect-Ec2-Instance

Now Go to your local directory where you downloaded the private key and paste your connection string.

Now You're connected with your EC2 remote server.

Install Node.js and npm

Once you are connected to the EC2 instance, the next step is to install Node.js and npm if they are not already installed. To do this, run the following commands:

In the terminal type the below command to update packages :

sudo apt update

Install Node/NPM

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

sudo apt-get install nodejs

node -v

Now we have nodeJs and npm installed.

Check this URL for other versions: https://github.com/nodesource/distributions

Upload Your Node.js Application

Now it's time to upload your Node.js application to the EC2 instance. You can use several methods to do this, such as SCP, FTP, or Git. In this tutorial, we will use GIT to transfer the files from our local machine to the EC2 instance.

Type below command to create a directory and move to that directory:

mkdir websites

cd websites

Clone your project from Github

git clone <yourproject git URL>

Go to your project using cd <yourProjectName> and install dependencies using:

npm install

Once all dependencies are installed you can run app by typing the below command:

node app.js

or

npm start(or whatever your start command)

Then go to the browser and use your port with IP address, something like 12.14.11.5:4000

Security Group setup

Copy public DNS from your instances page, paste it on your web browser, and append port:4000 at the end of the DNS address.

As you will see this will not work and there is nothing to show on the browser and the browser connection will timeout.

This is where the Security Group comes in, Select the EC2 Instance and click on the security group link in the Description section.

 Click on Edit the Inbound rules.

By clicking on the Edit button available in the Inbound tab, it will open the Edit Inbound rules popup.

By default, it will show SSH configurations, since our application is configured for port number 4000, we need to add a new rule “Custom TCP Rule”. Enter port range as 4000 and select Source as "Anywhere". After saving the changed rules, it will allow us to access our application from anywhere.

Access the URL again and you should be able to see the app content now using both Public IP or Public DNS and port 4000 in browsers.

For example,

ec2–204–256–556–125.compute-1.amazonaws.com:4000

or

18.243.171.48: 4000

Note: As we didn't set up SSL yet, so make sure to use this URL with HTTP only.

Keep the App running using PM2

As of now, the app is running as soon as you open the terminal and it will terminate once you close the terminal.

For that, We need to install PM2 (Production manager 2) to keep live our app after closing our terminal or disconnect from remote server.

Run the following command to Install Pm2 globally :

sudo npm i pm2 -g

Switch to your app directory and run

pm2 start app.js --name "sampleapp"

 Now even if you close the terminal and check the URL in the browser, the app will be running.

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 4000 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.

Configure NGINX:

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

Now we want to set up a firewall blocking that port and set up NGINX as a reverse proxy so we can access it directly using port 80 (HTTP)

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)

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

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;;

    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;
    }
}

You need to replace your domain name with "your-domain-name" and your application's port with "your-port." After you've saved the configuration file, run the command sudo ln -s /etc/nginx/sites-available/your-config-file> /etc/nginx/sites-enabled/ to create a symbolic link to the sites-enabled directory.

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 apt-add-repository -r ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

By Default this SSL is Only valid for 90 days, you can run the below command to renew it after expiration.

certbot renew

Meantime if you want to test the command you can dry-run it like this:

certbot renew --dry-run

Conclusion:

Thanks for reading.🙏

In this tutorial, we have shown you how to deploy your Node.js application on an AWS EC2 server like a pro. By following these steps, you can create a robust and scalable cloud-based infrastructure for your Node.js application. Remember to always follow best practices for security and performance when deploying applications to the cloud.

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