This document describes how to deploy the Pankaj Bhatia trading system to production on AWS EC2 (Ubuntu) using Apache, PM2, and MobaXterm for SSH/SFTP access. It is intended for developers and operations staff. Replace placeholder values (your-domain.com, /var/www/your-app/) with your actual domains and paths during setup.

Overview

The system uses a split-domain layout in production:
  • https://app.your-domain.com — React dashboard (static files)
  • https://api.your-domain.com — Node.js API + Socket.io (Apache reverse proxy)

Architecture

Request flow:
  1. Browser loads the React SPA from Apache (DocumentRoot).
  2. API calls go to https://api.your-domain.com/api/... — Apache proxies to Node on 127.0.0.1:5000.
  3. Socket.io connects to https://api.your-domain.com — Apache upgrades WebSocket traffic to the same Node process.
  4. PM2 keeps the backend running; MongoDB and Zerodha Kite are external dependencies.

Server directory layout

Confirm actual paths on your EC2 instance during first deploy. Example layout:
Example from existing ops notes: some deployments use /var/www/NiftyBankNifty/server for the backend. Adjust paths to match your server.

One-time EC2 setup

Run on a fresh Ubuntu EC2 instance (via MobaXterm SSH).

System packages

Install Node.js (LTS recommended — use NodeSource or nvm if your version requirements differ):
Install PM2 globally:

Apache modules

Enable modules required for reverse proxy and WebSocket support:
See Apache configuration for virtual host files.

Timezone

Trading cron jobs use IST. Set server timezone:

AWS Security Group

Allow inbound traffic: Restrict SSH (port 22) to trusted IPs where possible.

MongoDB

Set MONGO_URI in the backend .env pointing to:
  • MongoDB Atlas (recommended for production), or
  • A self-hosted MongoDB instance (same EC2 or separate)
No MongoDB install steps are included here — configure according to your chosen provider. Use Let’s Encrypt Certbot after Apache vhosts are configured:

MobaXterm access

SSH session

  1. Open MobaXterm → SessionSSH.
  2. Remote host: EC2 public IP or DNS name.
  3. Username: ubuntu (default for Ubuntu AMIs).
  4. Advanced SSH settings → use your AWS .pem key file.
  5. Connect.

SFTP (file transfer)

In the same SSH session, MobaXterm opens an SFTP browser panel automatically. Typical uses: Recommended workflow:
  • Use git pull on the server for code updates (backend and optionally frontend source).
  • Use SFTP for .env secrets, one-off file uploads, or emergency hotfixes.
  • Never commit .env files to git.

Backend deployment (PM2)

See PM2 deployment for first-time setup, routine deploy, and verification commands. Environment variables are loaded via src/env.ts (dotenv.config()). The .env file must live in the server working directory.

Frontend deployment (Apache static)

See Apache configuration for Vite build env, static deploy, and virtual host files. Vite bakes environment variables at build time. Set production values in .env before running yarn build.

Environment variables checklist

Backend — server/.env

Never commit this file. Create on server via MobaXterm SFTP or nano. Optional market hour overrides: MCX_START_HOUR, NFO_START_HOUR, CDS_*, etc. Full Zerodha auto-login setup: see Zerodha auto-login.

Frontend — build-time .env

Post-deploy verification

Zerodha-specific checks

After deploy or server restart, see Zerodha troubleshooting for log patterns:

Troubleshooting

For Zerodha token refresh and streaming issues, see Zerodha auto-login and Zerodha troubleshooting.

Summary

  • Deploy on AWS EC2 Ubuntu with Apache (static React + reverse proxy) and PM2 (Node backend).
  • Use MobaXterm for SSH commands and SFTP file transfer (especially .env secrets).
  • Typical layout: app.your-domain.com for UI, api.your-domain.com for API + Socket.io.
  • Backend: git pullnpm installnpm run buildpm2 restart nifty-backend.
  • Frontend: set Vite env vars → yarn build → copy build/ to Apache docroot.
  • PM2 cwd must be the server folder so .env loads correctly.
  • Rebuild frontend whenever API or Socket URLs change — Vite env is compile-time.
  • Confirm paths, domains, and SSL on your EC2 instance during first deploy.