How to Install Node.js on AWS EC2 and Make Node.js App Keep Running

In this post, I will show you how to install Node.js and use PM2 as process manager to make node.js app keep running, I use my AWS EC2 instance of Ubuntu 18.04 for the installation, you can follow this post to launch an EC2 instance or you can use your local computer for the installation.

If you use an AWS EC2 instance to follow this post, you have to connect to instance first and add Custom TCP at port 3000 in inbound rules, this port is used for test to run node.js app.

Install Node.js using NVM

Install NVM

If you’ve connected to your instance or use your local computer, now time to install node.js. In this post I’ll install node.js using nvm, nvm is a version manager for node.js.

The reasons I install node.js using nvm are that I can easily switch node.js version that currently running to another version, and I can install global packages npm i -g without doing sudo.

Run the following commands in a terminal to install or update nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

The command above will download a script and runs it. The script clones the nvm repository to ~/.nvm, and attempts to add the source lines to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

Install nvm

Install nvm

You can find the updated version of nvm here

Run the command below to check that nvm was successfully installed

nvm --version

The terminal will show Command ‘nvm’ not found as the code has been added to your profile but hasn’t run yet

nvm not found

nvm not found

Just close the currently opened terminal and launch a new terminal and run nvm –version again

nvm --version

nvm –version

Install Node.js

To install node.js using nvm, you can use nvm install node to install the latest version of node, or use nvm install 6.14.4 to install a specific version of node, or use nvm install –lts to install the latest LTS version of node.

NVM will install Node.js include with NPM (Node Package Manager), see the complete guide to use nvm here.

Install the latest LTS version of Node.js

nvm install --lts
nvminstall --ltsn

nvm install –lts

Check the installed version of Node.js

node --version

Check the installed version of NPM

npm --version
node npm --version

node npm –version

Run Example Node.js App

Node.js has successfully installed in your system, now time to run some node.js application. I’ll run an application that cloning from my github repository, the app is quite simple, it just a ‘Hello World’ app, it use just for test that node.js running correctly.

The main code app.js of this repository looks like this:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))

Clone the repository

git clone https://github.com/budimanfajarf/node-hello-world.git

Change directory to node-hello-world

cd node-hello-world

Install packages using npm

npm install

Run Node.js app

node app.js
node npm --version

node npm –version

Open a browser and navigate to localhost:3000 if you’re using a local computer, or navigate to public-dns-or-ip-address:3000 if you’re using an AWS EC2 Instance.

node npm --version

node npm –version

Make Node.js App Keep Running

Install PM2

The app will run as long as you open the terminal, but when you close the terminal or press Ctrl-C, the app will stop running. To make node.js app keep running, we can use a process manager called PM2, find more information about PM2 here.

First, stop the app.js running before by press Ctrl-C

Install PM2 globally using npm

npm install pm2 -g

Start Node.js app using PM2

pm2 start ~/node-hello-world/app.js
pm2 start app.js

pm2 start app.js

Now even if you close the terminal, the app will keep running and can access using a browser on a url like localhost:3000 if you use a local computer, or public-dns-or-ip-address:3000 if you use an AWS EC2 instance.

Configure Startup

When you reboot your server, the app won’t automatically be running, we can set up startup configuration by generating startup script using pm2.

To generate the startup script, use the following command:

pm2 startup

Copy the generated script and hit enter to run it

pm2 startup script

pm2 startup script

Save the configuration

pm2 save
pm2 save

pm2 save

Now when you reboot your server, the pm2 will automatically running and start node.js app! :)

If you want to remove the startup configuration, the steps are similar, generate unstartup script with the command below

pm2 unstartup

Copy and run the generated script, then save the configuration with pm2 save.

Useful PM2 Commands

List all running apps:

pm2 list

Managing apps:

pm2 stop <app_name|namespace|id|'all'|json_conf>
pm2 restart <app_name|namespace|id|'all'|json_conf>
pm2 delete <app_name|namespace|id|'all'|json_conf>

Details on a specific app:

pm2 describe <id|app_name>

Monitor logs, custom metrics, apps information:

pm2 monit

Find more info about PM2 on npmjs or keymetrics

#nodejs   #aws  


comments powered by Disqus