Deploy Node js API on AWS EC2 Instance
Great for testing simple deployments on Cloud, VPS
Install node version manager (nvm) by typing the following at the command line.
sudo su -
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
Activate nvm by typing the following at the command line.
. ~/.nvm/nvm.sh
Use nvm to install the latest version of Node.js by typing the following at the command line.
nvm install node
Test that node and npm are installed and running correctly by typing the following at the terminal:
node -v
npm -v
To install git, run below commands in the terminal window:
sudo apt-get update -y
sudo apt-get install git -y
or
sudo yum update -y
sudo yum install git -y
Just to verify if system has git installed or not, please run below command in terminal:
git --version
This command will print the git version in the terminal.
Run below command to clone the code repository from Github:
git clone https://github.com/dmhaui/ocr-eop.git
Get inside the directory and Install Packages
cd ocr-eop
npm install
Start the application To start the application, run the below command in the terminal:
npm start
Now, we will need to run the api 24/24 even when the EC2 console is closed
When you close the console tab in web view, even though the instance is still running, the code will not function properly
PM2 will help us continue running our code even after the terminal is closed
Install PM2 by typing the following at the command line.
npm install pm2 -g && pm2 update
pm2 start app.js: Start a Node.js application.
pm2 stop app: Stop a running application.
pm2 restart app: Restart an application.
pm2 list: List all running processes.
pm2 monit: Display a monitoring interface for all running processes.
pm2 logs: View logs of running processes.
pm2 delete app: Remove a process from PM2.
pm2 start server.js
pm2 list
We will see our server.js file displayed in a table.
pm2 stop 0