Deploy Your Apps to Digital Ocean

31 December 2020

A guide to deploy ur apps to Digital Ocean

First, Install Node JS version 12. and Npm in Ubuntu Server ( server in Digital Ocean )


  1. curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
  2. clone github
  3. add whitelist to your atlas monggodb

Second, Install PM2 project manager for javascript Nodejs You need install this to make app run automaticaly

  1. npm i -g pm2
  2. pm2 start server.js
  3. pm2 status ( to see status )
  4. pm2 restart server ( to restart )
  5. pm2 startup ubuntu ( to make automation running when startup )
  6. pm2 logs ( to see any logs )

okay next step, Set up NGINX PROXY

  1. apt install nginx

  2. ufw enable

  3. ufw allow ssh

  4. ufw status

  5. ufw allow http ( add 80 port )

  6. ufw allow https ( add 433 port )

  7. sudo nano /etc/nginx/sites-available/default

  8. delete all text

  9. add this to server block

    server {
    server_name domain.com www.domain.com;
    
     location / {
    proxy_pass http://localhost:5000;
    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;
    }
    }
  10. service nginx restart

  11. nginx -t

Other option Add SSL with Cloudflare

  1. sudo rm /etc/nginx/sites-enabled/default
  2. sudo nano /etc/nginx/conf.d/nginx.conf
  3. insert this
server {
    listen 80;
    listen [::]:80;
    server_name www.domain.com domain.com; # the hostname
    return 302 https://$server_name$request_uri; ## all traffic through port 80 will be forwarded to 443
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl  on;
    ssl_certificate  /etc/ssl/certs/cert.pem; #path to your public key
    ssl_certificate_key  /etc/ssl/private/cert.key; #path to your private key

    server_name www.domain.com domain.com; # the hostname
    location / {
        proxy_pass http://127.0.0.1:3001; # URL Rest API
    }
}
  1. nano /etc/ssl/certs/cert.pem
  2. paste certificate key from cloudflare
  3. nano /etc/ssl/private/cert.key
  4. paste private key from cloudflare
  5. nginx -t
  6. Service nginx reload