# ⚡ PM2 Quick Start - LuckyChit

Get your LuckyChit app running in production in 5 minutes!

---

## 🚀 Super Quick Deployment

### 1. Install PM2 (if not installed)

```bash
npm install -g pm2
```

### 2. Setup Environment

```bash
cd backend
cp .env.example .env
# Edit .env with production credentials
nano .env
```

### 3. Deploy with One Command

```bash
cd backend
chmod +x deploy.sh
./deploy.sh production
```

**Done!** ✅

---

## 📱 What You Get

- ✅ Backend running on port 3000
- ✅ Auto-restart on crashes
- ✅ Load balanced across all CPU cores
- ✅ Daily scheduled restart at 3 AM
- ✅ Automated payment reminders (9 AM IST)
- ✅ Comprehensive logging

---

## 🎯 Essential Commands

```bash
# Status
pm2 list

# Monitor
pm2 monit

# Logs
pm2 logs luckychit-backend

# Restart
pm2 restart luckychit-backend

# Stop
pm2 stop luckychit-backend
```

---

## 🔄 Updates (Zero Downtime)

```bash
git pull origin main
cd backend
npm install
pm2 reload luckychit-backend
```

---

## 🤖 Auto-Start on Server Reboot

**One-time setup:**

```bash
pm2 startup
# Run the command it outputs (copy/paste it)

pm2 save
```

Now your app automatically restarts after server reboot!

---

## 🔍 Troubleshooting

### ⚠️ bcrypt Error (Most Common)

**Error:** `invalid ELF header` or `ERR_DLOPEN_FAILED`

**Quick Fix (Run on Linux server):**

```bash
cd backend
chmod +x fix-bcrypt.sh
./fix-bcrypt.sh
```

Or manually:
```bash
cd backend
rm -rf node_modules package-lock.json
npm install
pm2 restart luckychit-api
```

**Cause:** node_modules installed on Windows, deployed to Linux. Native modules must be rebuilt on the target OS.

---

**App not starting?**

```bash
pm2 logs luckychit-backend --lines 100
```

**Database connection failed?**

Check `.env` file has correct credentials:

```bash
cat backend/.env
```

**Port already in use?**

```bash
# Find what's using port 3000
lsof -i :3000

# Kill it
kill -9 <PID>

# Restart
pm2 restart luckychit-backend
```

**More help?** See `DEPLOYMENT_TROUBLESHOOTING.md`

---

## 📊 Monitoring URLs

- **Backend Health Check:** `http://localhost:3000/api/health` (add this endpoint)
- **API Documentation:** `http://localhost:3000/api/docs`
- **Share Endpoint:** `http://localhost:3000/api/share`
- **Notifications:** `http://localhost:3000/api/notifications`

---

## 🎯 Production Checklist

- [ ] PM2 installed (`pm2 --version`)
- [ ] `.env` file configured
- [ ] Database running and accessible
- [ ] App started with PM2 (`pm2 list` shows running)
- [ ] Auto-start configured (`pm2 startup` + `pm2 save`)
- [ ] Test API endpoints
- [ ] Setup nginx reverse proxy (optional)
- [ ] Setup SSL certificate (recommended)

---

## 📚 Need More Details?

See the complete guide: `PM2_PRODUCTION_GUIDE.md`

---

**Your LuckyChit backend is now running in production! 🎉**

