Deploy Node.js on ubuntu with GIT

Install Git on your server

ssh into your VM, then install Git:

apt-get update
apt-get install git
#For public repogit clone For private repogit clone you can use ssh. here is a free guide:
then git clone the ssh link
if you get a permission denial execute the follow
ssh-add ~/.ssh/youRSAname

Remove old node and add new node

sudo apt-get purge --auto-remove nodejs npm
#install node through nvm it is easier to upgrade and downgrade nodesudo apt-get update
sudo apt-get install build-essential
curl -o- | bash#install the latest versionnvm install node//upon installatin the new version is set as defaultgo here for more info on nvm, like hwo to switch version:

Install pm2

npm -g install pm2

Kill other PM2 Process

pm2 killor find the running PM2 process with:ps aux | grep PM2then kill with:kill -9 [pid]//see pm2 logs
pm2 logs

Create an environment variable file

//cd to the git repo
nano .env
//paste all variables by right clicking

Update the local repo

git pull origin master
# Make sure ssh agent is working
eval "$(ssh-agent -s)"
# Find the ssh that as given to the repo
ls -al ~/.ssh
# Make that ssh default
ssh-add ~/.ssh/youRSAname

Run PM2

pm2 start index.js --name "GingerCream"#orpm2 stop GingerCream#or pm2 restart GingerCream

For automatic deployment, u can follow the following methods

  • Github actions (CI & CD)
  • post-receive Git hook

Finding Magic.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store