# 🚀 PRODUCTION DEPLOYMENT - STEP-BY-STEP MANUAL SETUP

## Current System Status

✅ Flask App: Valid  
✅ Dev Mode: Working (localhost:5000)  
✅ Prod Mode: Configured (0.0.0.0:5000)  
✅ nginx.conf: Ready (certificates configured)  

---

## Quick Start (3 Simple Phases)

### PHASE 1: Firewall Setup (2 minutes - needs admin cmd)

**Open PowerShell as Administrator and run:**

```powershell
# Allow HTTP traffic (port 80)
New-NetFirewallRule -DisplayName "Sales Dashboard HTTP" `
    -Direction Inbound -Protocol TCP -LocalPort 80 `
    -Action Allow -ErrorAction SilentlyContinue

# Allow HTTPS traffic (port 443)
New-NetFirewallRule -DisplayName "Sales Dashboard HTTPS" `
    -Direction Inbound -Protocol TCP -LocalPort 443 `
    -Action Allow -ErrorAction SilentlyContinue

# Verify rules were created
Get-NetFirewallRule -DisplayName "Sales Dashboard*"
```

Then come back here and continue.

---

### PHASE 2: Server Configuration (30 minutes - manual at registrar/router)

#### 2a. Get Free SSL Certificate (5 min)

1. Go to: https://zerossl.com/free-ssl
2. Enter domain: **sales.globalerc.pt**
3. Choose **Email Validation**
4. Check your email, click validation link
5. Download as **PEM format**
6. Extract and you'll get:
   - `certificate.crt`
   - `private.key`
   - `ca_bundle.crt`

**Copy these files to:** `C:\nginx\certs\`

```powershell
# Create directory if it doesn't exist
New-Item -ItemType Directory -Path "C:\nginx\certs" -Force -ErrorAction SilentlyContinue

# Copy certificate files (adjust paths to where you saved them)
Copy-Item "C:\path\to\certificate.crt" "C:\nginx\certs\" -Force
Copy-Item "C:\path\to\private.key" "C:\nginx\certs\" -Force
```

#### 2b. Update DNS Record (5 min)

Go to your **domain registrar** (GoDaddy, Namecheap, Registrar.pt, etc.)

Find the **A Record** for `sales` or `sales.globalerc.pt`

| Setting | Current | Change To |
|---------|---------|-----------|
| Name | sales | sales |
| Type | A | A |
| Value | 94.46.169.199 | **78.137.202.90** |
| TTL | 3600 | 3600 |

**Save** and wait **5-15 minutes** for DNS to propagate.

**Verify DNS updated:**
```powershell
# Check if DNS now points to your public IP
nslookup sales.globalerc.pt

# Should show: 78.137.202.90
```

#### 2c. Router Port Forwarding (10 min)

1. Open browser: **http://192.168.10.1** (your router)
2. Login with admin credentials
3. Find **Port Forwarding** settings
4. Add these two rules:

| External Port | Internal IP | Internal Port | Protocol |
|---------------|-------------|---------------|----------|
| 80 | 192.168.10.67 | 80 | TCP |
| 443 | 192.168.10.67 | 443 | TCP |

5. **Save** and **restart router**

#### 2d. Update Google OAuth (2 min)

1. Go to: https://console.cloud.google.com
2. Select your Sales Dashboard project
3. Go to: **APIs & Services** → **OAuth 2.0 Client IDs**
4. Click your **Web Application**
5. Under **Authorized redirect URIs**, add:
   ```
   https://sales.globalerc.pt/oauth2callback
   ```
6. **Save**

---

### PHASE 3: Start Services (5 minutes)

Once DNS propagates (and firewall/certs are ready):

**Terminal 1 - Start Flask in Production Mode:**
```powershell
cd "C:\Users\Tiago Rebelo\Desktop\Globale RC\2-Vibe Coding\Sales Dashboard App_VScode"
.\run_prod.ps1
```

**Terminal 2 - Start Nginx (reverse proxy):**
```powershell
cd C:\nginx
.\nginx.exe
```

---

## Testing

### Test 1: Local (on your PC)
```powershell
# Check if HTTPS is working
Invoke-WebRequest -Uri "https://sales.globalerc.pt" -UseBasicParsing
# Should get Status: 200 OK
```

### Test 2: From Another Network (Phone, Tablet, Different WiFi)

1. **Connect to different WiFi** (mobile hotspot or guest network)
2. **Open browser:** https://sales.globalerc.pt/dashboard
3. **You should see:**
   - 🔒 Green padlock (SSL certificate valid)
   - Globale RC dashboard loads
   - Login button ready

---

## Troubleshooting

| Problem | Check |
|---------|-------|
| **"Connection Refused"** | Is DNS updated? Run: `nslookup sales.globalerc.pt` (must show 78.137.202.90) |
| **"Certificate Error"** | Are cert files in C:\nginx\certs\ ? Check nginx error log: `C:\nginx\logs\error.log` |
| **"Can't connect from external"** | Router port forwarding correct? Try: `Test-NetConnection -ComputerName 78.137.202.90 -Port 443` |
| **"Nginx won't start"** | Check syntax: `cd C:\nginx; .\nginx.exe -t` |
| **timeout errors** | Restart router and wait for DNS propagation |

---

## File Locations Reference

| Component | Path |
|-----------|------|
| Flask App | C:\Users\Tiago Rebelo\Desktop\Globale RC\2-Vibe Coding\Sales Dashboard App_VScode\app.py |
| Dev Launcher | .\run_dev.ps1 |
| Prod Launcher | .\run_prod.ps1 |
| Nginx | C:\nginx\nginx.exe |
| Nginx Config | C:\nginx\conf\nginx.conf |
| Certificates | C:\nginx\certs\ |
| Nginx Logs | C:\nginx\logs\error.log |

---

## What Each Component Does

- **Flask (.\run_prod.ps1):** Your Python web application running on port 5000
- **Nginx (.\nginx.exe):** Handles HTTPS, redirects HTTP→HTTPS, forwards traffic to Flask
- **SSL Certificate:** Encrypts traffic (from ZeroSSL - free)
- **DNS:** Points sales.globalerc.pt to your public IP (78.137.202.90)
- **Router Port Forwarding:** Directs external requests to your PC

---

## Daily Operations

**To restart everything:**
```powershell
# Terminal 1 - Stop Flask
Get-Process python | Stop-Process -Force

# Terminal 2 - Stop Nginx
taskkill /F /IM nginx.exe

# Wait a few seconds
Start-Sleep -Seconds 3

# Terminal 1 - Start Flask
.\run_prod.ps1

# Terminal 2 - Start Nginx
cd C:\nginx; .\nginx.exe
```

**Daily health check:**
```powershell
# Check if both services are running
Get-Process python, nginx

# Test HTTPS locally
Invoke-WebRequest -Uri "https://sales.globalerc.pt" -UseBasicParsing | Select-Object StatusCode
```

---

## Support

For detailed instructions, see:
- **DNS_ROUTER_SETUP_GUIDE.md** - Full step-by-step for each registrar
- **DEPLOYMENT_QUICK_REFERENCE.md** - Printable checklist
- **COMPLETE_OPERATIONS_GUIDE.md** - All operations procedures

---

**Next Step:** Complete Firewall Setup (Phase 1) above, then follow Phase 2.

Good luck! 🚀
