chitfund/README/DEPLOYMENT.md

387 lines
6.4 KiB
Markdown

# Deployment Guide
Complete guide for deploying LuckyChit to production.
---
## 📋 Prerequisites
- PostgreSQL 12+
- Node.js 14+
- PM2 (for process management)
- Flutter SDK (for mobile app)
---
## 🚀 Backend Deployment
### 1. Initial Setup
```bash
# Clone repository
cd /home/luckychit/apps/chitfund/backend
# Install dependencies
npm install
# Setup environment
cp env.example .env
nano .env # Configure your settings
```
### 2. Database Setup
```bash
# Create database
sudo -u postgres psql
postgres=# CREATE DATABASE luckychit;
postgres=# CREATE USER luckychit WITH PASSWORD 'your_password';
postgres=# GRANT ALL PRIVILEGES ON DATABASE luckychit TO luckychit;
postgres=# \q
```
### 3. Apply Member Number Migration
**Option A: Using Node.js Script** (Easiest)
```bash
cd /home/luckychit/apps/chitfund/backend
node run-member-number-migration.js
```
**Option B: Using SQL File**
```bash
# Copy to accessible location
sudo cp migrations/20251106_add_member_number.sql /tmp/
sudo chmod 644 /tmp/20251106_add_member_number.sql
# Apply migration
sudo -u postgres psql -d luckychit -f /tmp/20251106_add_member_number.sql
# Clean up
sudo rm /tmp/20251106_add_member_number.sql
```
### 4. Start Backend with PM2
```bash
# Install PM2 globally (if not installed)
npm install -g pm2
# Start backend
pm2 start ecosystem.config.js
# Save PM2 configuration
pm2 save
# Setup auto-start on reboot
pm2 startup
# Follow the command it gives you
```
### 5. Verify Backend is Running
```bash
# Check status
pm2 status
# Check logs
pm2 logs chitfund-backend --lines 50
# Test health endpoint
curl http://localhost:3000/health
```
---
## 📱 Frontend Deployment
### Android APK
```bash
cd luckychit
# Build release APK
flutter build apk --release
# APK location:
# build/app/outputs/flutter-apk/app-release.apk
```
### iOS App
```bash
cd luckychit
# Build iOS
flutter build ios --release
# Then archive in Xcode
```
### Web Deployment
```bash
cd luckychit
# Build web
flutter build web --release
# Deploy files from:
# build/web/
```
---
## 🔄 Updating Production
### Update Backend
```bash
cd /home/luckychit/apps/chitfund/backend
# Pull latest code
git pull origin main
# Install any new dependencies
npm install
# Run migrations (if any)
node run-member-number-migration.js # If not already applied
# Restart
pm2 restart chitfund-backend
# Check logs
pm2 logs chitfund-backend
```
### Update Frontend
```bash
cd luckychit
# Pull latest code
git pull origin main
# Get dependencies
flutter pub get
# Build new APK
flutter build apk --release
# Distribute to users
```
---
## 🔧 Troubleshooting
### Backend Won't Start
**Module Not Found Error**:
```bash
cd backend
rm -rf node_modules package-lock.json
npm install
pm2 restart chitfund-backend
```
**Database Connection Error**:
```bash
# Check PostgreSQL is running
sudo systemctl status postgresql
# Check credentials in .env file
cat .env | grep DB_
# Test connection
node test-db-connection.js
```
**Port Already in Use**:
```bash
# Find what's using port 3000
lsof -i :3000
# Stop old process
pm2 delete all
pm2 start ecosystem.config.js
```
---
### Migration Issues
**Permission Denied**:
- Use Node.js script: `node run-member-number-migration.js`
- Or copy SQL to /tmp first
**Peer Authentication Failed**:
- Use: `sudo -u postgres psql`
- Or configure md5 auth in pg_hba.conf
**Column Already Exists**:
- Migration was already applied, safe to ignore
- Verify: `SELECT member_number FROM group_members LIMIT 1;`
---
### Frontend Issues
**API Connection Error**:
- Check API URL in `lib/core/services/api_service.dart`
- Should be: `https://chitfund.deepteklabs.com/api`
**Member Numbers Not Showing**:
- Check backend migration was applied
- Check API response includes `member_number` field
- Restart both backend and frontend
---
## 📊 Health Checks
### Backend Health
```bash
# Check PM2 status
pm2 status
# Check if responding
curl https://chitfund.deepteklabs.com/health
# Check database
sudo -u postgres psql -d luckychit -c "SELECT COUNT(*) FROM users;"
# Check member numbers
sudo -u postgres psql -d luckychit -c "SELECT member_number FROM group_members LIMIT 5;"
```
### Frontend Health
```bash
# Build test
flutter build apk --debug
# Check for errors
flutter analyze
# Run on device
flutter run
```
---
## 🔐 Security Checklist
- [ ] Change default passwords
- [ ] Set strong JWT secret in .env
- [ ] Enable rate limiting in production
- [ ] Use HTTPS only
- [ ] Configure CORS properly
- [ ] Keep dependencies updated
- [ ] Regular database backups
- [ ] Monitor PM2 logs
---
## 📦 Environment Variables
### Required (.env file)
```bash
# Database
DB_HOST=localhost
DB_PORT=5432
DB_NAME=luckychit
DB_USER=luckychit
DB_PASSWORD=your_secure_password
# JWT
JWT_SECRET=your_very_long_secret_key_here
JWT_EXPIRY=7d
# Server
PORT=3000
NODE_ENV=production
# Optional
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100
```
---
## 📝 Maintenance Tasks
### Daily
- Monitor PM2 logs: `pm2 logs`
- Check disk space: `df -h`
### Weekly
- Review error logs
- Check database size
- Update dependencies if needed
### Monthly
- Database backup
- Review and archive old logs
- Check for security updates
---
## 🎯 Production Checklist
### Before Going Live
- [x] Database migration applied
- [x] Backend running on PM2
- [x] Backend health check passes
- [x] Frontend built and tested
- [x] Admin features working
- [x] Member numbers displaying
- [x] All CRUD operations tested
- [x] Authentication working
- [x] WhatsApp integration configured
- [x] Large fonts for accessibility
### After Deployment
- [ ] Monitor error logs
- [ ] Test with real users
- [ ] Gather feedback
- [ ] Document any issues
- [ ] Plan next iteration
---
## 📞 Quick Commands Reference
```bash
# Backend
pm2 restart chitfund-backend # Restart
pm2 logs chitfund-backend # View logs
pm2 status # Check status
npm install # Install deps
# Database
sudo -u postgres psql -d luckychit # Connect
node run-member-number-migration.js # Migrate
# Frontend
flutter pub get # Get deps
flutter build apk --release # Build APK
flutter run # Run app
```
---
## ✅ Deployment Complete!
After following this guide:
- ✅ Backend running on PM2
- ✅ Database migrated with member numbers
- ✅ Frontend built and deployed
- ✅ All admin features working
- ✅ Ready for production use
**Your chitfund management system is live!** 🎉