# Deploy Guide — Daneshyar

## Requirement

```
pip install paramiko
```

---

## First-time deploy (3 commands)

### 1. Build assets locally

```bash
python scripts/build.py
```

Runs `composer install --no-dev`, `npm run build`, `artisan config/route/view cache`.
Output lands in `public/build/`.

---

### 2. Deploy to server

```bash
python scripts/deploy.py
```

Asks for SSH password (never stored). Then:

1. Shows server status
2. Installs PHP 8.3, Apache2, MySQL, Composer, Node.js 20
3. Configures Apache (VirtualHost, security headers, compression)
4. Optionally creates MySQL database + user
5. Pauses so you can create `.env` on the server (see below)
6. Uploads project zip via SFTP
7. Runs `composer install`, `artisan migrate`, `artisan key:generate`, caches, reloads Apache

---

### 3. Create `.env` on the server

During deploy you will be asked to create `.env`. SSH in separately:

```bash
ssh root@91.107.131.171
nano /var/www/html/.env
```

Minimum values to fill in:

```
APP_ENV=production
APP_DEBUG=false
APP_URL=https://mydaneshyar.ir
ASSET_URL=https://mydaneshyar.ir
SESSION_SECURE_COOKIE=true
SESSION_DOMAIN=mydaneshyar.ir
LOG_CHANNEL=daily
LOG_LEVEL=warning
LOG_DAILY_DAYS=7

DB_DATABASE=daneshyar
DB_USERNAME=daneshyar
DB_PASSWORD=your_db_password
```

Press Enter in the deploy script when done.

---

## Re-deploy (after code changes)

```bash
python scripts/build.py
python scripts/update.py
```

`update.py` is the fast update path. It hashes the release locally, uploads only
changed files as one compressed archive (native PSCP on Windows), prepares
Laravel caches in an isolated release, and atomically switches the canonical
`/var/www/daneshyar` symlink after every step succeeds. `/var/www/html` remains a
compatibility symlink for Apache and points to `/var/www/daneshyar`. The script
keeps only the newest release after a successful update, repairs production-safe
`APP_URL`/`ASSET_URL` values, and reapplies Laravel file permissions each run.

To benchmark the upload route independently:

```bash
python scripts/update.py --speed-test
```

The first run converts `/var/www/html` to the release layout and moves `.env`
and `storage/` into `/var/www/daneshyar-shared`. Releases live in
`/var/www/daneshyar-releases`, shared runtime files live in
`/var/www/daneshyar-shared`, and legacy/source material can sit beside them in
`/var/www/daneshyar-sources`. Later updates reuse unchanged files and skip
Composer unless `composer.json` or `composer.lock` changed.

Use `deploy.py` only for first-time server provisioning or infrastructure setup.

### Online-class server services

The first deploy now offers to install the complete classroom runtime: Laravel
Reverb, its queue worker, coturn, Redis, Apache's WebSocket proxy, and a media
health/load timer. To install or repair it later on the application server:

```bash
cd /var/www/daneshyar
sudo python3 scripts/classroom_server.py install \
  --app-dir /var/www/daneshyar \
  --domain mydaneshyar.ir
```

Check all classroom services and registered media capacity:

```bash
sudo python3 scripts/classroom_server.py status --app-dir /var/www/daneshyar
```

To add another TURN relay, run the PC-side deployer. It installs coturn on the
new server, registers it through the main application server, and verifies
health/load automatically:

```powershell
scripts\deploy_media_server.bat
```

New rooms choose the active server with the lowest load percentage and remain
pinned to it for the session. `drain ID` stops new rooms from using a server
without interrupting rooms already assigned to it.

See [`CLASSROOM_SERVER_INSTALL.md`](CLASSROOM_SERVER_INSTALL.md) for first-time
primary installation, multi-server JSON configuration, credential options,
firewall ports, unattended runs, and status checks.

### Enable or repair HTTPS

After DNS for `mydaneshyar.ir` and `www.mydaneshyar.ir` points to the server,
run the idempotent HTTPS provisioner locally:

```bash
python scripts/enable_https.py --accept-new-host-key
```

It installs the isolated Certbot snap, obtains a trusted Let's Encrypt
certificate without stopping Apache, configures redirects and renewal, updates
Laravel's HTTPS environment values, and verifies the live certificate. The SSH
password is read from `DANESHYAR_SSH_PASSWORD` or prompted without echo. Use
`--accept-new-host-key` only for the first connection after independently
confirming the server IP; subsequent runs should use the saved/known SSH key.

---

## Config

Edit the top of `scripts/deploy.py` to change server details:

```python
SERVER_HOST = "91.107.131.171"
SERVER_PORT = 22
SSH_USER    = "root"
APP_DIR     = "/var/www/html"
PHP_VERSION = "8.3"
```

---

## Apache config only (if needed separately)

```bash
python scripts/configure_apache.py
```

Run **on the server** as root. Writes VirtualHost config, enables modules, reloads Apache.
