Host a node application on Ubuntu 20.04.2 LTS
Idea is to prepare the Ubuntu server by installing Node Using the Node Version Manager. Refresh your local package by running a quick update.
sudo apt update
Install NVM
$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
The script clones the nvm repository to ~/.nvm
, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile
, ~/.zshrc
, ~/.profile
, or ~/.bashrc
). Don’t forget to reload your bash config.
$ source ~/.bashrc
Verify your installation
$ nvm --version
0.38.0
Install Node, it will install the latest stable version of the node and make it default.
$ nvm install node
Downloading and installing node v16.1.0...
Downloading https://nodejs.org/dist/v16.1.0/node-v16.1.0-linux-x64.tar.xz...
####################################################################################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v16.1.0 (npm v7.11.2)
Creating default alias: default -> node (-> v16.1.0)
Lets’ install lts/erbium(v12.22.1) and make it default
$ nvm install v12.22.1
Downloading and installing node v12.22.1...
Downloading https://nodejs.org/dist/v12.22.1/node-v12.22.1-linux-x64.tar.xz...
####################################################################################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.22.1 (npm v6.14.12)$ nvm alias default v12.22.1
$ nvm list
-> v12.22.1
v16.1.0
default -> v12.22.1
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.1.0) (default)
stable -> 16.1 (-> v16.1.0) (default)
lts/* -> lts/fermium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.1
lts/fermium -> v14.17.0 (-> N/A)
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, reload them without downtime, and facilitate common system admin tasks.
Install PM2
npm install pm2 -g
Run an application via pm2
pm2 start app.js --name exmaple --log /home/ubuntu/example.log