# Shinecode Deployment Standard

## Deployment Model
Frontend and backend deploy independently. The frontend uses Next.js 16 with a Node.js server managed by PM2, placed behind an Nginx reverse proxy. The backend uses Laravel 13 on a separate server or subdomain.

## Production Rule
Production must never depend on: Developer machines, Local files, Debug mode, Development credentials, Unpinned dependencies.

## VPS Deployment Guide (Frontend)

### 1. Requirements
- **Node.js**: >= 22.0.0 (Node 22 LTS required for Next.js 16)
- **npm**: >= 9.0.0
- **PM2**: Latest version (`npm install -g pm2`)
- **Nginx**: Installed and configured

### 2. Environment Configuration
Create a `.env.production` file in `apps/web/` containing safe, browser-accessible values:
```env
NEXT_PUBLIC_API_URL=https://shinecode.ae/api
INTERNAL_API_URL=http://127.0.0.1
INTERNAL_HOST_HEADER=shinecode.ae
NEXT_PUBLIC_BOOKING_URL=https://shinecode.ae
NEXT_PUBLIC_ENV=production
NEXT_PUBLIC_POSTHOG_KEY=your_key
NEXT_PUBLIC_POSTHOG_HOST=https://app.posthog.com
```

### 3. Installation & Build
```bash
# Clone or pull the repository
git pull origin main

# Install dependencies cleanly
npm ci

# Run production build across all workspaces
npm run build
```

### 4. PM2 Start & Restart
```bash
# First time start (using standard shinecode-frontend or shinecode-web)
pm2 start npm --name "shinecode-frontend" -- start

# Restart after updates
pm2 reload shinecode-frontend --update-env
# (or pm2 reload shinecode-web --update-env)
```

### 5. Web Server VirtualHost Binding & Reverse Proxy
Configure Nginx to proxy `www.shinecode.ae` to the Next.js Node server (port 3000), and ensure the Laravel virtual host accepts loopback `127.0.0.1` and `localhost`:

#### Frontend Next.js Proxy (Nginx)
```nginx
server {
    listen 80;
    server_name www.shinecode.ae shinecode.ae;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
```

#### Backend Laravel VirtualHost (Nginx or Apache)
Ensure the Laravel backend accepts loopback and dev IP under `server_name` (or `ServerAlias` in Apache):
- **Nginx**: `server_name shinecode.ae www.shinecode.ae 192.168.1.111 127.0.0.1 localhost;`
- **Apache**: `ServerAlias www.shinecode.ae 192.168.1.111 127.0.0.1 localhost`

#### Laravel Trusted Domains (`.env` in backend)
```env
SANCTUM_STATEFUL_DOMAINS=shinecode.ae,www.shinecode.ae,192.168.1.111,192.168.1.111:3000,localhost,127.0.0.1
```

### 6. Health Verification
Verify the application is healthy after deployment:
```bash
curl -s -o /dev/null -w "%{http_code}" https://www.shinecode.ae/en
# Should return 200
```

### 7. Rollback Procedure
If a deployment fails:
```bash
# Revert to the previous stable commit
git checkout <previous_commit_hash>

# Rebuild the application
npm ci
npm run build

# Restart PM2
pm2 restart shinecode-web
```
