Skip to content

a check list for deploying lightweight expressjs apps

Notifications You must be signed in to change notification settings

adhambadr/nodejsdeployment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

Deployment

just trying to consolidate all the stuff i end up googling everytime i deploy something on ubuntu. credit to DO and all other articles i reference from.

links

ubuntu installing necessary packages

# Update sources
sudo apt-get update
# Install Git
sudo apt-get install git
# Install Nginx 
sudo apt-get install nginx
# Install Certbot (For SSL with Let's encrypt)
# add sources for Certbot before this
sudo apt-get install certbot python-certbox-nginx 
# Install NodeJS (v13) (Change _13.x to whatever 14,15 or 100. 
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
sudo apt-get install -y nodejs
# By now you have node, npm, git & certbot ready
# Now some process management
npm -g pm2

Git Branch Deployment commands Checklist

Server

cd /var/source-code.git
git init --bare
cd hooks && touch post-receive
echo
"
#!/bin/sh
git --work-tree=/var/www/livefolder --git-dir=/home/source-code.git checkout -f
cd /var/www/livefolder
npm i
npm run build
pm2 restart process.json
" > post-receive
chmod +x post-receive

local

git remote add live ssh://user@server:/var/source-code.git
git add . && commit -m "pushing live"
git push live master 

Correct Rights to a live folder

sudo chown -R $USER:$USER /var/www/website-folder
sudo chmod -R 755 /var/www

Nginx Set up

sudo apt-get install nginx
nano /etc/nginx/sites-available/default

add this :

server {
    listen 80;
    server_name livedomain.com;
    location / {
        proxy_pass http://localhost:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
sudo service nginx restart

This assumes your express.js somewhere says :

app.listen("localhost",3001);

Pm2 set up

sudo npm install pm2 -g

Start Process & Restart it on file change (Deployment) more

pm2 start app.js

Sample Watch File: process.json

{
  "apps" : [{
    "script"      : "index.js",
    "watch"       : true,
    "ignore_watch" : ["logs","node_modules","values"],
    "env": {
      "NODE_ENV": "production",
    }
  }]
}

Setup procedure :

pm2 start process.json

Set it up for Starting on server restart

pm2 startup

Save the current proccesses to see them start

pm2 save

useful commands :

pm2 list
pm2 show [id]
pm2 logs
pm2 monit

Further readings:

About

a check list for deploying lightweight expressjs apps

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published