# VelaPanel Deployment Guide

## Requirements

- Windows Server 2019 or 2022 (64-bit)
- 2 GB free disk space on C:
- PowerShell 5.1+ (built into Windows Server)
- Internet access (for IIS feature installation from Windows Update)
- Run all scripts as **Administrator**

---

## Step 1 — Create the deployment package (on your current/dev machine)

Open PowerShell as Administrator:

```powershell
Set-ExecutionPolicy Bypass -Scope Process
C:\velapanel.com\setup\package-for-deployment.ps1
```

This rebuilds the panel and creates:
```
C:\velapanel.com\VelaPanel-Deploy-YYYYMMDD-HHMM.zip
```

---

## Step 2 — Transfer to the fresh server

Copy the zip file to the fresh Windows Server via:
- RDP (copy-paste files)
- USB drive
- Network share
- SCP / SFTP

---

## Step 3 — Extract the zip

On the fresh server, open PowerShell as Administrator:

```powershell
# Create the target folder
New-Item -ItemType Directory -Path "C:\velapanel.com\publish" -Force

# Extract (adjust zip filename to match)
Expand-Archive -Path "C:\VelaPanel-Deploy-20260610-1200.zip" `
               -DestinationPath "C:\velapanel.com\publish" -Force
```

---

## Step 4 — Run the setup script

```powershell
Set-ExecutionPolicy Bypass -Scope Process
C:\velapanel.com\publish\setup\setup-server.ps1
```

The script will:
1. Check OS version and disk space
2. Install all required IIS features
3. Create `C:\velapanel.com\data\` and subdirectories
4. Create a `PanelSvc` Windows service account
5. Register VelaPanel as a Windows Service (auto-start)
6. Open firewall ports 5100 and 5101
7. Start the panel and confirm it responds

---

## Step 5 — First login

Open a browser on the server (or from another machine on the same network):

```
http://<server-ip>:5100
```

| Setting  | Value                       |
|----------|-----------------------------|
| Username | admin@localhost             |
| Password | CHANGE-THIS-NOW-Admin1!     |

**Change the admin password immediately after logging in.**

---

## Configuration

Edit `C:\velapanel.com\publish\appsettings.json` to set:

| Setting | Where | Description |
|---------|-------|-------------|
| `Seeding:AdminEmail` | appsettings.json | Admin login email |
| `Seeding:AdminPassword` | appsettings.json | Admin login password |
| `Database:MySqlConnectionString` | appsettings.json | MySQL server details |
| `Database:MsSqlConnectionString` | appsettings.json | MSSQL server details |
| `Acme:AccountEmail` | appsettings.json | Email for Let's Encrypt SSL |
| `Notifications:DefaultSmtpHost` | appsettings.json | SMTP server for alerts |

Restart the service after editing:
```powershell
Restart-Service VelaPanel
```

---

## Service Management

```powershell
Start-Service VelaPanel      # Start
Stop-Service VelaPanel       # Stop
Restart-Service VelaPanel    # Restart
Get-Service VelaPanel        # Check status
```

---

## Health Check & Repair

Verify every module's backend (DB, IIS, MySQL/MSSQL/Postgres/Mongo, mail, DNS,
firewall, jobs, SSL) is working:

- **In the panel:** log in as Admin → **Panel Settings → Health Check** (`/admin/health`).
  Click **Run Check** for a green/amber/red report, or **Repair Panel** to run safe,
  idempotent fixes (start stopped services, re-apply firewall rules, re-ensure IIS
  byte-logging, apply pending DB migrations — never deletes data).

- **From the command line** (runs the smoke checks + unit tests):
  ```powershell
  powershell -ExecutionPolicy Bypass -File C:\velapanel.com\tools\healthcheck.ps1
  ```
  Exit code 0 = all passed, 1 = one or more failed. Good to run after `deploy-update.ps1`.

- **For external uptime monitors:** `GET http://localhost:5100/api/health/live`
  returns `200` (up) or `503` (degraded). Anonymous, leaks no data, works even when
  the license is invalid.

---

## Logs

```
C:\velapanel.com\data\logs\panel-YYYYMMDD.log   ← daily rolling log
C:\velapanel.com\data\logs\archive\             ← archived event logs
```

---

## Uninstall

Run the same setup script and choose option **[2] Uninstall**:

```powershell
C:\velapanel.com\publish\setup\setup-server.ps1
# Select: 2
```

The uninstall routine will:
1. Stop and delete the VelaPanel Windows Service
2. Delete the `PanelSvc` service account
3. Remove the firewall rules for ports 5100 and 5101
4. Optionally delete all files under `C:\velapanel.com\`

> **Note:** If `sc.exe delete` returns error 1072, close **services.msc** and
> **Task Manager** (both hold SCM handles), then reboot. The service entry is
> marked for deletion and will be fully removed after the reboot.
