Skip to content

Instantly share code, notes, and snippets.

@nrbnayon
Created April 2, 2026 10:10
Show Gist options
  • Select an option

  • Save nrbnayon/c3a6b5df41745a38ed2f07cc7868d54e to your computer and use it in GitHub Desktop.

Select an option

Save nrbnayon/c3a6b5df41745a38ed2f07cc7868d54e to your computer and use it in GitHub Desktop.

🌐 Easy Hosting Guide on Vps (MEAN stack)

βœ… Step 0: Set Up Domain First

  1. Open your domain provider’s dashboard (e.g., Namecheap, GoDaddy, Cloudflare, Hostinger, Porkbun, etc.)
  2. Go to Domain List β†’ DNS Zone
  3. Now observe: πŸ” If there are old A records named @, api or dashboard πŸ‘‰ Delete them first.

⚠️ DNS will NOT work if there are multiple A records with the same name.

  1. Now add three new A records as shown below (use your VPS IP):
image

βœ… Step 1: Log in to VPS Using VS Code

  1. Open VS Code
  2. Go to Extensions (Ctrl + Shift + X)
  3. Search Remote - SSH β†’ Install
image
  1. Press Ctrl + Shift + P β†’ search:
image
  1. When asked, enter:
image
  1. Press Enter and you will be connected πŸŽ‰

βœ… Step 2: Install Necessary Software in VPS

Copy and run:

apt update && apt upgrade -y
apt install -y nginx curl unzip git certbot python3-certbot-nginx

curl -fsSL https://deb.nodesource.com/setup_24.x | bash -
apt install -y nodejs
npm i -g pm2

systemctl enable nginx
systemctl start nginx

πŸ‘‰ This installs all required packages & starts NGINX.


βœ… Step 3: Upload Your Projects (Drag & Drop)

In VS Code, open /var/www/ folder.

Drag your project folders here:

Project Upload To
Frontend (React) /var/www/frontend
Backend (Node.js) /var/www/backend
Dashboard (React) /var/www/dashboard

Then go inside each folder and run:

npm install

βœ… Step 4: Run Projects Using PM2

cd /var/www/frontend
pm2 start npm --name frontend -- start

cd /var/www/backend
pm2 start npm --name backend -- start

cd /var/www/dashboard
pm2 start npm --name dashboard -- start

Save and enable autostart:

pm2 save
pm2 startup

βœ… Step 5: Configure NGINX

πŸ“ Go to /etc/nginx/nginx.conf

http {
   # in this block 
   client_max_body_size 0; # Unlimited or replace 0 to value

   # Also consider adjusting timeouts for large uploads
   client_body_timeout 60s;
   client_header_timeout 60s;
   keepalive_timeout 60s;
   send_timeout 60s;

}

πŸ“ Go to /etc/nginx/sites-available/


⚠️ Important

πŸ”₯ Delete the file named default. πŸ›‘ Do not delete any other file.

Why? If it stays, your subdomains will not work and you will see the default NGINX page.


πŸ›  Create 3 files:

  • root
  • api
  • dashboard

Put the following configuration (change subdomain + port):

server {
    listen 80;
    server_name (subdomain.)yourdomain.com;

    location / {
        proxy_pass http://localhost:(port);
        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;
    }
}

Enable them:

sudo ln -s /etc/nginx/sites-available/root /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/dashboard /etc/nginx/sites-enabled/
image

πŸ” Reload NGINX

nginx -t
systemctl reload nginx

βœ… Step 6: Install SSL (HTTPS Free Certificate)

certbot --nginx -d yourdomain.com -d www.yourdomain.com -d api.yourdomain.com -d dashboard.yourdomain.com

Choose Redirect HTTP to HTTPS when asked.


πŸŽ‰ Done!

URL Shows
https://yourdomain.com, https://www.yourdomain.com Main App
https://api.yourdomain.com Backend API
https://dashboard.yourdomain.com Dashboard

βš™οΈ Useful Commands

🟒 PM2

pm2 ls
pm2 restart name/all
pm2 stop name
pm2 delete name
pm2 save
pm2 startup
pm2 logs (name)

🌐 NGINX

nginx -t
systemctl reload nginx

πŸ” SSL Renew Test

certbot renew --dry-run

πŸš€ VPS Deployment Guide

MEAN / MERN Stack Β· NGINX Β· PM2 Β· SSL

Complete Step-by-Step Production Deployment


πŸ“‹ Overview

This guide walks you through deploying a full-stack web application β€” React/Next.js frontend, Node.js/Express backend, and an admin dashboard β€” on a fresh Linux VPS, from DNS setup all the way to live HTTPS URLs.

Prerequisites:

  • A fresh Ubuntu VPS (20.04 / 22.04 / 24.04)
  • A registered domain name
  • Your project folders ready locally
  • VS Code installed on your local machine

βœ… Step 0 β€” Configure DNS Records

Do this before touching the VPS.

1. Open your domain provider's DNS Zone

(Namecheap, GoDaddy, Cloudflare, Hostinger, Porkbun, etc.)

2. Delete old conflicting records

⚠️ If there are existing A records named @, api, or dashboard β€” delete them first. Duplicate A records with the same name will break DNS resolution entirely.

3. Add these new A records

Name Type Value
@ A YOUR_VPS_IP
www A YOUR_VPS_IP
api A YOUR_VPS_IP
dashboard A YOUR_VPS_IP

πŸ’‘ DNS propagation can take a few minutes up to 48 hours. You can continue setting up the VPS while waiting.


βœ… Step 1 β€” Connect to VPS via VS Code (Remote SSH)

Using VS Code's Remote SSH extension is the most comfortable way to manage your VPS β€” you get a full file explorer and integrated terminal.

  1. Open VS Code β†’ Extensions (Ctrl + Shift + X)
  2. Search Remote - SSH β†’ click Install
  3. Press Ctrl + Shift + P, then type and select:
    Remote-SSH: Connect to Host...
    
  4. When prompted, enter your connection string:
    ssh root@YOUR_VPS_IP
  5. Press Enter, authenticate with your password or SSH key
  6. You are now inside your VPS πŸŽ‰ β€” open the integrated terminal to run commands

βœ… Step 2 β€” Install Required Software

Run this single block in your VPS terminal to install everything at once:

apt update && apt upgrade -y
apt install -y nginx curl unzip git certbot python3-certbot-nginx

# Install Node.js v24 LTS
curl -fsSL https://deb.nodesource.com/setup_24.x | bash -
apt install -y nodejs

# Install PM2 globally (process manager)
npm i -g pm2

# Start and enable NGINX
systemctl enable nginx
systemctl start nginx

βœ… This installs: NGINX (reverse proxy), Node.js, PM2 (process manager), Certbot (free SSL), and Git.


βœ… Step 3 β€” Upload Your Projects

In VS Code's remote file explorer, navigate to /var/www/ and drag your project folders from your local machine directly into it.

Project Upload To
Frontend (React / Next.js) /var/www/frontend
Backend (Node.js / Express) /var/www/backend
Dashboard (React) /var/www/dashboard

After uploading, install dependencies in each folder:

cd /var/www/frontend  && npm install
cd /var/www/backend   && npm install
cd /var/www/dashboard && npm install

⚠️ Make sure your .env files are present in each folder with production environment variables set correctly.


βœ… Step 4 β€” Run Projects with PM2

PM2 is a production-grade process manager. It keeps your apps alive, auto-restarts on crash, and survives server reboots β€” always prefer PM2 over tmux for production.

cd /var/www/frontend
pm2 start npm --name frontend -- start

cd /var/www/backend
pm2 start npm --name backend -- start

cd /var/www/dashboard
pm2 start npm --name dashboard -- start

# Save the process list & enable autostart on reboot
pm2 save
pm2 startup

⚠️ After running pm2 startup, it will print a command β€” copy and run that command to register PM2 as a system service.


βœ… Step 5 β€” Configure NGINX

5a β€” Global Settings

Open /etc/nginx/nginx.conf and add these lines inside the http { } block:

http {

    # Allow large file uploads (replace 0 with e.g. 100m for a specific limit)
    client_max_body_size 0;

    # Prevent timeouts on slow connections
    client_body_timeout   60s;
    client_header_timeout 60s;
    keepalive_timeout     65s;
    send_timeout          60s;

}

5b β€” Delete the Default Site

Navigate to /etc/nginx/sites-available/ and delete the default file. If left in place, NGINX will serve its own placeholder page instead of your apps.

rm /etc/nginx/sites-available/default
rm -f /etc/nginx/sites-enabled/default

5c β€” Create Virtual Host Config Files

Create 3 files in /etc/nginx/sites-available/:

  • root
  • api
  • dashboard

Use this template for each β€” replace server_name and proxy_pass port:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;  # change per file

    location / {
        proxy_pass         http://localhost:3000;    # change port per app
        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;
    }
}

Port assignments (typical):

File server_name proxy_pass Port
root yourdomain.com www.yourdomain.com :3000 (Frontend)
api api.yourdomain.com :5000 (Backend)
dashboard dashboard.yourdomain.com :3001 (Dashboard)

5d β€” Enable the Sites

sudo ln -s /etc/nginx/sites-available/root      /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/api       /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/dashboard /etc/nginx/sites-enabled/

5e β€” Test & Reload NGINX

nginx -t              # Always test before reloading
systemctl reload nginx

⚠️ If nginx -t reports an error, fix it before reloading β€” a broken config will take down all your sites.


βœ… Step 6 β€” Install Free SSL (HTTPS via Let's Encrypt)

Certbot automatically obtains and configures SSL certificates, and sets up HTTPS redirects in NGINX.

certbot --nginx \
  -d yourdomain.com \
  -d www.yourdomain.com \
  -d api.yourdomain.com \
  -d dashboard.yourdomain.com

πŸ’‘ When asked, select Redirect HTTP to HTTPS so all traffic is automatically secured.

βœ… Certbot auto-renews every 90 days. Test it with:

certbot renew --dry-run

πŸŽ‰ Your Live URLs

URL Serves
https://yourdomain.com Frontend / Main App
https://www.yourdomain.com Frontend / Main App
https://api.yourdomain.com Backend API
https://dashboard.yourdomain.com Admin Dashboard

βš™οΈ Quick Reference Commands

🟒 PM2 β€” Process Manager

Command Description
pm2 ls List all running processes
pm2 restart frontend Restart a specific app
pm2 restart all Restart all apps
pm2 stop backend Stop a specific app
pm2 delete dashboard Remove an app from PM2
pm2 logs frontend View live logs for an app
pm2 logs View logs for all apps
pm2 save Save current process list
pm2 startup Enable autostart on reboot

🌐 NGINX

Command Description
nginx -t Test config for syntax errors
systemctl reload nginx Reload NGINX (zero downtime)
systemctl restart nginx Full restart of NGINX
systemctl status nginx Check NGINX running status

πŸ” SSL / Certbot

Command Description
certbot renew --dry-run Test auto-renewal (no changes)
certbot renew Force certificate renewal
certbot certificates List all installed certificates

πŸ› οΈ Troubleshooting

Symptom Likely Cause & Fix
502 Bad Gateway Your Node.js app is not running. Check pm2 ls and restart the app.
NGINX default page shows You did not delete the default config. Remove it and reload NGINX.
SSL certificate fails DNS has not propagated yet. Wait and retry with certbot --nginx -d ...
App not starting Run pm2 logs <name> to see the exact error.
Port conflict Each app must listen on a unique port. Check .env and NGINX proxy_pass.
Changes not reflected Restart the app with pm2 restart <name> and reload NGINX.

πŸ’‘ Rule of thumb: nginx -t and pm2 logs are your first stops for any issue.


β€” End of Guide β€”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment