In this tutorial, you will learn how to deploy node.js application on aws ec2 instance.
How to Deploy Node.js Express Application on Amazon AWS EC2 Instance with Linux 2 Ubuntu
Here are steps:
Step 1: Log in to the AWS Console
Go to https://console.aws.amazon.com/ec2/ to log in to your AWS console management account.
Step 2: Connect to AWS EC2 Instance via SSH
To connect AWS EC2 instance via SSH, So open your ssh terminal and connect it.
If you do not how to connect aws ec2 instance via ssh, Read this => Connect aws ec2 instance via ssh for Windows, mac and linux user.
Step 3: Install NodeJS and NPM
Once you are connected to your aws ec2 instance, and to update the Linux 2 Ubuntu packages on your AWS EC2 server by typing the sudo apt-get update
command in the SSH terminal window and pressing Enter:
sudo apt-get update
Type the curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
command on ssh terminal to install nvm (node version manager) on your server:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
To enable nvm by typing the ~/.nvm/nvm.sh
command on ssh terminal window and press enter:
~/.nvm/nvm.sh
To install node js latest version by typing nvm install <node-version>
command on ssh terminal window and press enter:
nvm install 21
It will install node js 21 version on your aws ec2 instance.
Type node -v
command on ssh terminal window to verify nodejs version:
node -v
Step 4: Install GIT
Just type sudo apt-get install git
command on terminal window and press enter, this will install git on aws ec2 server:
sudo apt-get install git
Git is a free and open-source distributed version control system. You will clone a git repository containing a simple node app from Github, modify it and deploy. Git might already be installed on the instance.
Step 5: Deploy NodeJS Application on AWS EC2 Instance Server
Run the command git clone url
on the ssh terminal window and press enter, this will clone your Node JS express application files from the GitHub repository to aws ec2 linux 2 ubuntu instance:
git clone https://github.com/tutsmake/Simple-Node-JS-App.git
Navigate into node js application directory:
cd node-app
Run npm install on ssh terminal window and press enter:
npm install
Step 6: Start the NodeJS Application
To start a Node JS app on an AWS EC2 instance, you can do so by typing the node index.js
command on an SSH terminal window and pressing Enter:
node index.js
To keep the Nodejs app running after you close your terminal or disconnect from the remote server, you can use the sudo npm install pm2 -g
command on an SSH terminal window:
sudo npm install pm2 -g
Step 7: Access node.js application in browser
Now, open your browser and see the applications running on the port number:
15.158.91.210:80 (public IP of the machine : port 80)
Conclusion
That’s it; You have learned how to deploy a Node.js app on an Amazon AWS EC2 instance by installing Node.js, npm, and Git on the server.