# VPS deployment — WhatsApp Node

Deploy `services/whatsapp-node` on Ubuntu (not cPanel for multi-session Chromium).

## Requirements

- Ubuntu 22.04 or 24.04
- **4 GB+ RAM** (more if Paytm Puppeteer runs on same host)
- Node.js 18+
- Nginx (reverse proxy)

## 1. Server setup

```bash
sudo apt-get update
sudo apt-get install -y git curl chromium-browser fonts-liberation libnss3 libatk-bridge2.0-0 libgbm1
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo npm install -g pm2
```

## 2. Deploy app

```bash
cd /var/www
git clone YOUR_REPO saas
cd saas/services/whatsapp-node
npm install
```

Create `/etc/environment` or PM2 ecosystem env:

```bash
export HOST=0.0.0.0
export PORT=3000
export DATA_DIR=/var/www/saas/services/whatsapp-node
export PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
export WWEBJS_HEADLESS=true
```

## 3. PM2

```bash
cd /var/www/saas/services/whatsapp-node
pm2 start app.js --name cps-whatsapp-node
pm2 save
pm2 startup
```

## 4. Nginx

```nginx
server {
    listen 80;
    server_name wa-node.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_read_timeout 120s;
    }
}
```

Add TLS with certbot. Laravel `node_base_url` = `https://wa-node.yourdomain.com`

## 5. Persistence

Keep on disk across deploys:

- `.wwebjs_auth/` — WhatsApp session auth (one folder per device `api_key`)

Do **not** delete `.wwebjs_auth` on deploy unless you want all customers to re-scan QR.

## 6. After deploy

1. Laravel **Owner → Settings → WhatsApp** → set Node base URL
2. Each customer creates device → scans QR
3. Run [TEST-CHECKLIST.md](./TEST-CHECKLIST.md) on production

## 7. Monitoring

```bash
pm2 logs cps-whatsapp-node
pm2 monit
```

Schedule Laravel cron: `php artisan whatsapp:ping-node` every 5 minutes (optional keep-alive).
