How to Deploy Node.js Application on AWS EC2 Server

How to Deploy Node.js Application on AWS EC2 Server

It is very easy to deploy Node.js applications to an Amazon AWS EC2 instance, Simply open ssh terminal window and connect to AWS EC2 instance, then install nodejs, npm, and git on your aws ec2 instance, and you can deploy node.js application from GitHub repository on the AWS EC2. And you can run node js express application on aws ec2 with linux 2 ubuntu server.

How to Deploy Node.js Express Application on Amazon AWS EC2 Instance with Linux 2 Ubuntu

Here are the quick steps to deploy a Node.js Express application on an Amazon AWS EC2 instance with Linux 2 Ubuntu:

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

Simply type 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; for example:

git clone https://github.com/tutsmake/Simple-Node-JS-App.git

Once you have clone your node js application, navigate into it by using cd command on ssh terminal window:

cd node-app

And after that, install Node dependencies, which will create the node-modules folder into your node js app on aws ec2 instance, Simply type 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

Once you start your Node JS app, it will run as long as the ssh terminal is running, if you close the ssh terminal window, it will close it.

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

This is an alternative command to use globally to start a node JS app:

sudo pm2 start index.js

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.

Thanks for reading this tutorial.

Recommended AWS Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *