Install nodejs
Download
wget --no-check-certificate
https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux-x64.tar.xz
Move the downloaded file /usr/local
Then unpack
tar xf node-v10.15.1-linux-x64.tar.xz
Add node executables to the path by adding
symlinks to /usr/bin
ln -s /usr/local/node/bin/node /usr/bin/node
ln -s /usr/local/node/bin/npm /usr/bin/npm
ln -s /usr/local/node/bin/npx /usr/bin/npx
Install pm2
To run node as service we need to install a
nodejs package
npm install pm2@latest –g
We need to run set the path to pm2
ln -s /usr/local/node/bin/pm2 /usr/bin/pm2
ln -s /usr/local/node/bin/pm2
/usr/bin/pm2-runtime
For security reasons we should run our app NOT as root. We create a new user nodejs to run our app
useradd nodejs
chown -R
nodejs:nodejs /usr/local/node
chown –R
nodejs:nodejs /usr/local/node-v10.15.1-linux-x64
#
as root
pm2
startup system –u nodejs –-hp /home/nodjs
To start the application as the nodejs user, switch to the user nodejs
su nodejs
pm2 start /usr/local/node/apps/chat/server.js
pm2 list
# to automatically start the app on reboots
pm2 save
Likewise to stop
#other
commands
pm2 stop /usr/local/node/apps/chat/server.js
pm2
delete /usr/local/node/apps/chat/server.js