# 🚀 Quick Deployment Reference

Quick commands for deploying LuckyChit on Proxmox LXC.

## 🎯 One-Command Setup (After LXC Creation)

```bash
# Run this inside your LXC container as root
curl -o- https://raw.githubusercontent.com/YOUR_REPO/deploy.sh | bash
```

Or follow the manual steps below:

---

## ⚡ Quick Manual Setup

### 1. Initial Setup (5 minutes)

```bash
# Update system
apt update && apt upgrade -y
apt install -y curl wget git nano ufw

# Create user
adduser --disabled-password --gecos "" luckychit
usermod -aG sudo luckychit
su - luckychit
```

### 2. Install Stack (10 minutes)

```bash
# Node.js
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

# PostgreSQL
sudo apt install -y postgresql postgresql-contrib nginx
sudo npm install -g pm2

# Flutter
cd /opt
sudo git clone https://github.com/flutter/flutter.git -b stable
sudo chown -R luckychit:luckychit /opt/flutter
echo 'export PATH="$PATH:/opt/flutter/bin"' >> ~/.bashrc
source ~/.bashrc
flutter config --enable-web
```

### 3. Setup Database (2 minutes)

```bash
sudo -u postgres psql << EOF
CREATE DATABASE luckychit;
CREATE USER luckychit WITH PASSWORD 'ChangeMe123!';
GRANT ALL PRIVILEGES ON DATABASE luckychit TO luckychit;
\q
EOF
```

### 4. Deploy Backend (5 minutes)

```bash
mkdir -p /home/luckychit/apps
cd /home/luckychit/apps

# Upload your code or clone from git
# Then:
cd chitfund/backend
cp env.example .env
nano .env  # Update passwords and secrets
npm install --production
node create-db.js
pm2 start src/server.js --name luckychit-api
pm2 save
```

### 5. Deploy Frontend (5 minutes)

```bash
cd /home/luckychit/apps/chitfund/luckychit
flutter pub get
flutter build web --release
sudo mkdir -p /var/www/luckychit
sudo cp -r build/web/* /var/www/luckychit/
sudo chown -R www-data:www-data /var/www/luckychit
```

### 6. Configure Nginx (3 minutes)

```bash
sudo nano /etc/nginx/sites-available/luckychit
# Paste config from main guide
sudo ln -s /etc/nginx/sites-available/luckychit /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
```

### 7. Enable Firewall (1 minute)

```bash
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
```

---

## 🔄 Common Operations

### Check Status
```bash
pm2 status                           # Application status
sudo systemctl status nginx          # Nginx status
sudo systemctl status postgresql     # Database status
pm2 logs                            # View logs
```

### Restart Services
```bash
pm2 restart luckychit-api           # Restart API
sudo systemctl restart nginx        # Restart Nginx
sudo systemctl restart postgresql   # Restart Database
```

### Update Application
```bash
cd /home/luckychit/apps/chitfund/backend
git pull
npm install --production
pm2 reload luckychit-api

cd /home/luckychit/apps/chitfund/luckychit
git pull
flutter pub get
flutter build web --release
sudo cp -r build/web/* /var/www/luckychit/
```

### Backup Database
```bash
pg_dump -U luckychit -h localhost luckychit > backup_$(date +%Y%m%d).sql
```

### Restore Database
```bash
psql -U luckychit -h localhost luckychit < backup_20241105.sql
```

---

## 🔍 Troubleshooting Commands

```bash
# Check if services are running
sudo netstat -tulpn | grep :80      # Check Nginx
sudo netstat -tulpn | grep :3000    # Check API

# View logs
pm2 logs luckychit-api --lines 50
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/postgresql/postgresql-14-main.log

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

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

# Check disk space
df -h

# Check memory
free -h
```

---

## 📝 Important Files & Paths

| Item | Path |
|------|------|
| Backend Code | `/home/luckychit/apps/chitfund/backend` |
| Frontend Code | `/home/luckychit/apps/chitfund/luckychit` |
| Web Files | `/var/www/luckychit` |
| Nginx Config | `/etc/nginx/sites-available/luckychit` |
| Backend Logs | `pm2 logs` or `/home/luckychit/logs/` |
| Nginx Logs | `/var/log/nginx/` |
| Environment | `/home/luckychit/apps/chitfund/backend/.env` |
| Backups | `/home/luckychit/backups/` |

---

## 🔐 Security Reminders

```bash
# Change these in .env:
JWT_SECRET=<random-32-character-string>
DB_PASSWORD=<strong-password>

# Never commit .env to git!
# Always use HTTPS in production
# Setup regular backups
```

---

## 📱 Access URLs

After deployment:
- Frontend: `http://YOUR_IP` or `http://yourdomain.com`
- API: `http://YOUR_IP/api` or `http://api.yourdomain.com`
- Health: `http://YOUR_IP/health`

---

## ⏱️ Total Setup Time: ~30 minutes

Good luck! 🎉

