# 📝 Your Actual Production Setup - LuckyChit

This document reflects your **actual** PM2 production setup based on your deployment history.

---

## 🏗️ Current Architecture

### Server Details
- **User**: `luckychit`
- **Server IP**: `192.168.8.148`
- **Domain**: `chitfund.deepteklabs.com`

### Directory Structure
```
/home/luckychit/apps/chitfund/
├── backend/                 # Node.js Express API
│   ├── src/server.js       # Entry point
│   ├── .env                # Environment variables
│   └── node_modules/
├── luckychit/              # Flutter app
│   ├── lib/
│   ├── web/
│   └── build/web/          # Built web files (served by PM2)
└── deploy-*.sh             # Deployment scripts
```

### PM2 Processes

| Process Name | Type | Command | Port | Status |
|-------------|------|---------|------|--------|
| `luckychit-api` | Backend | `pm2 start src/server.js` | 3000 | ✅ Running |
| `luckychit-frontend` | Frontend | `pm2 serve build/web 8080 --spa` | 8080 | ✅ Running |

### Key Differences from PM2_PRODUCTION_GUIDE.md

❌ **NOT using**: `ecosystem.config.js`  
❌ **NOT using**: Cluster mode  
❌ **NOT using**: nginx (disabled in your setup)  
✅ **Using**: Simple `pm2 start` command  
✅ **Using**: PM2's built-in static server for frontend  
✅ **Using**: Direct port access (3000 & 8080)

---

## 🚀 Deployment Commands

### Quick Deploy (Both Backend & Frontend)
```bash
cd /home/luckychit/apps/chitfund
./deploy-full.sh
```

### Backend Only
```bash
cd /home/luckychit/apps/chitfund
./deploy-backend-only.sh
```

### Frontend Only
```bash
cd /home/luckychit/apps/chitfund
./deploy-frontend-only.sh
```

### Manual Deployment (Step by Step)

#### Update 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
```

#### Update 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 PM2 Commands for Your Setup

```bash
# View all running processes
pm2 status

# View logs (live)
pm2 logs                              # All processes
pm2 logs luckychit-api                # Backend only
pm2 logs luckychit-frontend           # Frontend only
pm2 logs luckychit-api --lines 50     # Last 50 lines

# Restart processes
pm2 restart luckychit-api             # With brief downtime
pm2 reload luckychit-api              # Zero-downtime (better)
pm2 restart all                       # Restart everything

# Monitor real-time
pm2 monit                             # CPU, Memory, logs

# Stop processes (DON'T do this unless needed)
pm2 stop luckychit-api
pm2 stop all

# Delete process (removes from PM2 list)
pm2 delete luckychit-api

# Save current process list
pm2 save

# Clear logs
pm2 flush
```

---

## 🔧 Common Issues & Solutions

### ❌ Backend won't start: "invalid ELF header" (bcrypt error)

**Cause**: `node_modules` was installed on Windows, but server is Linux.

**Fix**:
```bash
cd /home/luckychit/apps/chitfund/backend
rm -rf node_modules package-lock.json
npm install
pm2 restart luckychit-api
```

### ❌ Frontend not updating after deployment

**Fix**:
```bash
cd /home/luckychit/apps/chitfund/luckychit

# Nuclear option - clean everything
flutter clean
rm -rf .dart_tool build
flutter pub get
flutter build web --release
pm2 restart luckychit-frontend

# Check browser cache too!
```

### ❌ Git pull conflicts

**Fix**:
```bash
git stash                  # Save local changes
git pull origin prodnew    # Pull updates
git stash pop             # Re-apply local changes (optional)
```

### ❌ PM2 process not found after reboot

**Check auto-startup**:
```bash
pm2 status
pm2 startup   # Re-setup if needed
pm2 save      # Save current processes
```

### ❌ Check if ports are accessible

```bash
# Test backend
curl http://localhost:3000/health
curl http://192.168.8.148:3000/health

# Test frontend
curl http://localhost:8080
curl http://192.168.8.148:8080

# Test from domain
curl https://chitfund.deepteklabs.com/health
```

### ❌ Firewall blocking ports

**Fix**:
```bash
sudo ufw status
sudo ufw allow 3000/tcp
sudo ufw allow 8080/tcp
sudo ufw reload
```

---

## 📊 Health Check URLs

After deployment, verify these URLs:

- **Backend Health**: `http://192.168.8.148:3000/health`
- **Frontend**: `http://192.168.8.148:8080`
- **Domain**: `https://chitfund.deepteklabs.com`

---

## 🔄 Your Git Workflow

**Production Branch**: `prodnew` (NOT `main` or `master`)

```bash
# On server - pull latest changes
cd /home/luckychit/apps/chitfund
git checkout prodnew
git pull origin prodnew

# From dev machine - push to production
git add .
git commit -m "Your changes"
git push origin prodnew
```

---

## 🛠️ Emergency Commands

### Restart Everything
```bash
pm2 restart all
pm2 status
```

### Kill Everything and Start Fresh
```bash
pm2 kill
pm2 save --force

# Then restart manually:
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
```

### View Server Resource Usage
```bash
pm2 monit       # PM2 monitoring
htop            # General server monitoring
df -h           # Disk space
free -h         # Memory usage
```

---

## 📋 Pre-Deployment Checklist

Before deploying to production:

- [ ] Test changes locally
- [ ] Commit and push to `prodnew` branch
- [ ] SSH into production server as `luckychit` user
- [ ] Run deployment script
- [ ] Check `pm2 status` - all green
- [ ] Check `pm2 logs` - no errors
- [ ] Test URLs work (backend & frontend)
- [ ] Verify functionality in browser

---

## 🎯 Quick Reference

```bash
# Daily deployment workflow
ssh luckychit@192.168.8.148
cd /home/luckychit/apps/chitfund
./deploy-full.sh

# Monitor after deployment
pm2 status
pm2 logs --lines 50

# If issues:
pm2 restart all
pm2 logs luckychit-api --lines 100
```

---

## 📞 Your PM2 Auto-Start Setup

Based on line 174 of your history:

```bash
pm2 startup systemd -u luckychit --hp /home/luckychit
pm2 save
```

This means PM2 automatically starts your apps when the server reboots. ✅

To verify:
```bash
systemctl status pm2-luckychit
```

---

## 🎓 Notes from Your History

1. You disabled nginx (lines 253-254) - direct port access instead
2. You're using the `prodnew` git branch for production
3. You had to fix bcrypt multiple times - always run `npm install` on the server
4. You tested with local IP `192.168.8.148` before domain
5. You set up firewall rules for ports 3000 and 8080

---

**Last Updated**: Based on your bash history (lines 173-403)

**Need Help?** Check PM2 logs first: `pm2 logs --lines 100`

