# 🚀 Quick Reference - LuckyChit Production

## 🚨 EMERGENCY: 502 Bad Gateway Error?

```bash
ssh luckychit@192.168.8.148
cd /home/luckychit/apps/chitfund
./fix-502-auto.sh
```

See `FIX_502_ERROR.md` for details.

---

## 📍 Server Info
```
Server IP: 192.168.8.148
Domain: chitfund.deepteklabs.com
User: luckychit
Project: /home/luckychit/apps/chitfund
Branch: prodnew
```

---

## 🎯 Most Common Tasks

### 1️⃣ Deploy Full Update (Backend + Frontend)
```bash
ssh luckychit@192.168.8.148
cd /home/luckychit/apps/chitfund
./deploy-full.sh
```

### 2️⃣ Deploy Backend Only
```bash
ssh luckychit@192.168.8.148
cd /home/luckychit/apps/chitfund
./deploy-backend-only.sh
```

### 3️⃣ Deploy Frontend Only
```bash
ssh luckychit@192.168.8.148
cd /home/luckychit/apps/chitfund
./deploy-frontend-only.sh
```

### 3B️⃣ Deploy Frontend with Force Cache Clear
```bash
ssh luckychit@192.168.8.148
cd /home/luckychit/apps/chitfund
./force-cache-bust.sh
```

### 4️⃣ Check Status
```bash
pm2 status
```

### 5️⃣ View Logs
```bash
pm2 logs                              # All logs (live)
pm2 logs luckychit-api --lines 50     # Backend last 50 lines
pm2 logs luckychit-frontend --lines 50 # Frontend last 50 lines
```

### 6️⃣ Restart Services
```bash
pm2 restart luckychit-api            # Backend
pm2 restart luckychit-frontend       # Frontend
pm2 restart all                      # Everything
```

### 7️⃣ Test Health
```bash
curl http://localhost:3000/health      # Backend
curl http://localhost:8080             # Frontend
```

---

## 🔧 Manual Deployment (When Scripts Fail)

### Backend:
```bash
cd /home/luckychit/apps/chitfund
git pull origin prodnew
cd backend
npm install
pm2 restart luckychit-api
pm2 logs luckychit-api --lines 20
```

### Frontend:
```bash
cd /home/luckychit/apps/chitfund
git pull origin prodnew
cd luckychit
flutter pub get
flutter build web --release
pm2 restart luckychit-frontend
pm2 logs luckychit-frontend --lines 20
```

---

## ⚠️ Common Issues & Quick Fixes

### Frontend Not Showing Latest Changes (Cache Issue)
```bash
# On server
cd /home/luckychit/apps/chitfund
./force-cache-bust.sh

# Clear Service Worker (Browser)
# 1. Press F12 (DevTools)
# 2. Application tab → Service Workers
# 3. Click "Unregister" for all workers
# 4. Hard Refresh: Ctrl + Shift + R (Windows) or Cmd + Shift + R (Mac)

# Or test in Incognito mode (no cache)
# Ctrl + Shift + N (Windows) or Cmd + Shift + N (Mac)
```

### "invalid ELF header" Error
```bash
cd /home/luckychit/apps/chitfund/backend
rm -rf node_modules package-lock.json
npm install
pm2 restart luckychit-api
```

### Frontend Not Updating
```bash
cd /home/luckychit/apps/chitfund/luckychit
flutter clean
rm -rf .dart_tool build
flutter pub get
flutter build web --release
pm2 restart luckychit-frontend
```

### Git Pull Conflicts
```bash
git stash
git pull origin prodnew
```

### PM2 Process Not Running
```bash
pm2 status
pm2 restart all
# If still not working:
pm2 logs --lines 100
```

---

## 🎮 PM2 Command Cheat Sheet

| Command | Description |
|---------|-------------|
| `pm2 status` | Show all processes |
| `pm2 logs` | View all logs (live) |
| `pm2 logs luckychit-api` | View backend logs |
| `pm2 logs --lines 100` | Last 100 lines |
| `pm2 monit` | Real-time monitoring |
| `pm2 restart all` | Restart everything |
| `pm2 restart luckychit-api` | Restart backend |
| `pm2 reload luckychit-api` | Zero-downtime restart |
| `pm2 stop all` | Stop everything |
| `pm2 flush` | Clear all logs |
| `pm2 save` | Save current process list |
| `pm2 startup` | Setup auto-start on reboot |

---

## 📊 Current PM2 Setup

```bash
# Backend (Node.js API)
pm2 start src/server.js --name luckychit-api
# Running on: http://192.168.8.148:3000

# Frontend (Flutter Web)
pm2 serve /home/luckychit/apps/chitfund/luckychit/build/web 8080 --name luckychit-frontend --spa
# Running on: http://192.168.8.148:8080

# Auto-start configured
pm2 startup systemd -u luckychit --hp /home/luckychit
pm2 save
```

---

## 🔄 Upgrade to Better Setup (Optional)

### Use ecosystem.config.js (Recommended)

**Benefits**: Cluster mode, memory limits, auto-restart, scheduled restarts

```bash
cd /home/luckychit/apps/chitfund/backend

# Stop current process
pm2 delete luckychit-api

# Start with ecosystem config
pm2 start ecosystem.config.js --env production

# Save new setup
pm2 save
```

---

## 📞 Emergency Commands

### Restart Everything From Scratch
```bash
pm2 kill
cd /home/luckychit/apps/chitfund/backend
pm2 start src/server.js --name luckychit-api
cd ../luckychit
pm2 serve build/web 8080 --name luckychit-frontend --spa
pm2 save
```

### Check Server Resources
```bash
pm2 monit          # PM2 monitoring
df -h              # Disk space
free -h            # Memory
top                # CPU & processes
```

---

## 🐛 Debugging

### Backend Issues
```bash
# Check logs
pm2 logs luckychit-api --lines 200

# Check environment
cd /home/luckychit/apps/chitfund/backend
cat .env | grep -v PASSWORD  # View env vars (hiding password)

# Test database connection
psql -U luckychit -h localhost -d luckychit

# Test manually (without PM2)
cd /home/luckychit/apps/chitfund/backend
npm start
```

### Frontend Issues
```bash
# Check logs
pm2 logs luckychit-frontend --lines 100

# Check build directory
ls -la /home/luckychit/apps/chitfund/luckychit/build/web

# Rebuild from scratch
cd /home/luckychit/apps/chitfund/luckychit
flutter clean
flutter pub get
flutter build web --release
pm2 restart luckychit-frontend
```

### Network Issues
```bash
# Check if ports are open
netstat -tulpn | grep -E '(3000|8080)'

# Check firewall
sudo ufw status

# Allow ports if needed
sudo ufw allow 3000/tcp
sudo ufw allow 8080/tcp
sudo ufw reload

# Test from local machine
curl http://localhost:3000/health
curl http://localhost:8080

# Test from network
curl http://192.168.8.148:3000/health
curl http://192.168.8.148:8080
```

---

## 📋 Pre-Flight Checklist

Before any deployment:

- [ ] Changes tested locally
- [ ] Committed to `prodnew` branch
- [ ] Pushed to remote repo
- [ ] SSH'd into server as `luckychit` user
- [ ] Backed up database (if major changes)

After deployment:

- [ ] `pm2 status` shows all green
- [ ] No errors in `pm2 logs`
- [ ] Backend `/health` endpoint responds
- [ ] Frontend loads in browser
- [ ] Test critical functionality (login, etc.)

---

## 🔗 Quick Links

- **Backend Health**: http://192.168.8.148:3000/health
- **Frontend**: http://192.168.8.148:8080
- **Domain**: https://chitfund.deepteklabs.com
- **Documentation**: 
  - `ACTUAL_PRODUCTION_SETUP.md` - Your real setup
  - `PM2_PRODUCTION_GUIDE.md` - Recommended best practices
  - `PRODUCTION_DIFFERENCES.md` - Comparison

---

## 📱 Contact for Emergencies

If PM2 is completely broken:
```bash
pm2 kill
pm2 save --force
cd /home/luckychit/apps/chitfund/backend
pm2 start src/server.js --name luckychit-api
cd ../luckychit
pm2 serve build/web 8080 --name luckychit-frontend --spa
pm2 save
pm2 status
```

---

**Last Updated**: November 5, 2025  
**Version**: Based on your actual production history

