
Deploy a Node.js Application On AWS EC2 Server
In this article, I will show you how we can easily deploy the node js application on the Amazon EC2 server and run it on a live URL.
Deploy Node.js Application On AWS EC2 Server step by step:
Requirements to Deploy Node.js app to AWS
- AWS account: To utilize AWS services, you must have an AWS account.
- Terminal with SSH support: It establishes a connection to the EC2 instance. You can’t deploy Node.js to AWS without it.
- AWS PEM file: This file provides the safest and secure connection between your app and AWS. It is essential for security.
- An IP Address: With an IP address, AWS can locate your Node.js app and will provide zero interrupt deployment.
If you don't have EC2 created then first go to this link and 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.

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 Nodejs on EC2:
In the terminal type the below command to update packages :
sudo apt update
Install Node/NPM
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
node --version
Now we have nodeJs and npm installed.
Check this URL for other versions: https://github.com/nodesource/distributions
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_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;
}
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 python-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.🙏
This is how you can deploy your nodejs application using an AWS EC2 server.