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

Clone your private/public repo

#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

If you get permission denied error which might happen if the repo is private follow the following steps

# 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.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Laziness with Representable Functors

Finishing up NextJS migration

Implementing and Testing reCAPTCHA v3 in Your Front-End

Unit tests in Jest with supertest and MongoDB

Future of JavaScript?

倒数1天疯狂促销活动即将结束 💥!赶紧买起来吧 ⚡!!

Boost Your Productivity as a Web Developer

React Folder Structure

Build a Network Monitoring Application using React.

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
Arnav Zedian

Arnav Zedian

Finding Magic.

More from Medium

Development — Trading bot based on Binance Api (Part 1)

Protect from CSRF attack in Node.js

REST API using Node.Js