chitfund/README/DEPLOYMENT.md

6.4 KiB

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

# 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

# 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)

cd /home/luckychit/apps/chitfund/backend
node run-member-number-migration.js

Option B: Using SQL File

# 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

# 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

# Check status
pm2 status

# Check logs
pm2 logs chitfund-backend --lines 50

# Test health endpoint
curl http://localhost:3000/health

📱 Frontend Deployment

Android APK

cd luckychit

# Build release APK
flutter build apk --release

# APK location:
# build/app/outputs/flutter-apk/app-release.apk

iOS App

cd luckychit

# Build iOS
flutter build ios --release

# Then archive in Xcode

Web Deployment

cd luckychit

# Build web
flutter build web --release

# Deploy files from:
# build/web/

🔄 Updating Production

Update Backend

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

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:

cd backend
rm -rf node_modules package-lock.json
npm install
pm2 restart chitfund-backend

Database Connection Error:

# 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:

# 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

# 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

# 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)

# 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

  • Database migration applied
  • Backend running on PM2
  • Backend health check passes
  • Frontend built and tested
  • Admin features working
  • Member numbers displaying
  • All CRUD operations tested
  • Authentication working
  • WhatsApp integration configured
  • Large fonts for accessibility

After Deployment

  • Monitor error logs
  • Test with real users
  • Gather feedback
  • Document any issues
  • Plan next iteration

📞 Quick Commands Reference

# 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! 🎉