# Going live (pilot server)

> **On shared hosting (cPanel)?** Follow [SHARED_HOSTING.md](SHARED_HOSTING.md) instead.

How to put the ERP on a real server for the pilot schools. Local development stays as in
[LOCAL_SETUP.md](LOCAL_SETUP.md). At the end, `php artisan erp:preflight` must pass.

Example domain below: `example.in`. Schools live at `https://<school>.example.in`, the Platform Owner panel at
`https://admin.example.in`.

## 1. Server

- Linux VPS (Ubuntu 22.04/24.04 works well), 2 GB RAM is enough for the pilot.
- Nginx (or Apache), **PHP 8.2+** with php-fpm and the extensions `mbstring, xml, curl, zip, intl, gd, bcmath, mysql, sqlite3`.
- **MariaDB 10.4+ or MySQL 8**, and `mysqldump` (comes with the client package).
- Composer, Git. No Redis, no Node.
- A second server or storage at a **different provider** for backup copies (see section 7).

## 2. DNS and HTTPS

1. DNS at your registrar: an `A` record for `example.in` and a **wildcard** `A` record for `*.example.in`, both to the server IP.
2. A wildcard TLS certificate for `example.in` and `*.example.in`. With Let's Encrypt this needs the DNS challenge, for
   example `certbot certonly --manual --preferred-challenges dns -d example.in -d '*.example.in'` (or your DNS provider's
   certbot plugin so it renews by itself). Wildcard certificates cannot use the HTTP challenge.
3. Redirect all `http://` to `https://`.

## 3. Nginx

```nginx
server {
    listen 80;
    server_name example.in *.example.in;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.in *.example.in;
    root /var/www/school/public;
    index index.php;

    ssl_certificate     /etc/letsencrypt/live/example.in/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.in/privkey.pem;

    client_max_body_size 10m;   # photos, documents, imports

    location / { try_files $uri $uri/ /index.php?$query_string; }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }
    location ~ /\.(?!well-known) { deny all; }
}
```

## 4. Install

```bash
cd /var/www && git clone <repository> school && cd school
```

```bash
composer install --no-dev --optimize-autoloader
```

```bash
cp .env.production.example .env
```

Fill in `.env` (domain, database, mail, `PLATFORM_OWNER_EMAIL`), then:

```bash
php artisan key:generate
```

**Copy `APP_KEY` from `.env` to a password manager now.** Aadhaar numbers, 2FA secrets and Razorpay keys are encrypted
with it: a database backup is useless without it, so keep it apart from the backups.

```bash
php artisan migrate --force
```

```bash
php artisan db:seed --force
```

In production the seeder creates the plans, modules, permissions and the Platform Owner (a random password is printed
once if `PLATFORM_OWNER_PASSWORD` is empty). It does **not** create the demo school.

```bash
php artisan storage:link
```

```bash
php artisan config:cache && php artisan route:cache && php artisan view:cache
```

File owner: the web server user must be able to write `storage/` and `bootstrap/cache/`
(e.g. `chown -R www-data:www-data storage bootstrap/cache`).

## 5. Scheduler and queue worker

One cron line (as the web server user) runs fines, online payment checks, reminders and backups:

```
* * * * * cd /var/www/school && php artisan schedule:run >> /dev/null 2>&1
```

A queue worker for large imports, kept running by systemd (`/etc/systemd/system/school-queue.service`):

```ini
[Unit]
Description=School ERP queue worker
After=network.target

[Service]
User=www-data
Restart=always
WorkingDirectory=/var/www/school
ExecStart=/usr/bin/php artisan queue:work --sleep=3 --tries=3 --max-time=3600

[Install]
WantedBy=multi-user.target
```

```bash
systemctl enable --now school-queue
```

## 6. First login and the pilot schools

1. Open `https://admin.example.in`, log in as the Platform Owner, set up 2FA and save the recovery codes.
2. Create each pilot school (subdomain, main branch, its School Super Admin). The wildcard DNS and certificate cover
   new subdomains, so nothing changes on the server.
3. Give each school its Super Admin login; they change the password at first login.
4. Online fees: the school enters its own Razorpay keys in Fees > Fee Settings and adds the webhook
   `https://<school>.example.in/fees/razorpay/webhook` (events `payment.captured`, `payment.failed`) in its Razorpay
   dashboard. **Start with Razorpay test keys** and one test payment before switching to live keys.

## 7. Backups

- `backup:database` runs every night at 02:15: `mysqldump` (single transaction), gzipped, kept **30 days**.
- `backup:files` runs every Sunday at 03:20: all uploaded school files zipped, kept **8 weeks**.
- Both write to `storage/app/backups/` and, when `BACKUP_COPY_DISK` is set, the same file to that disk.

To copy to another provider, add a disk to `config/filesystems.php` and name it in `BACKUP_COPY_DISK`. Examples:

- S3-compatible storage (AWS S3, Backblaze B2, Wasabi, DigitalOcean Spaces):
  `composer require league/flysystem-aws-s3-v3 "^3.0"` and use the `s3` disk with its `AWS_*` variables.
- Another server over SFTP: `composer require league/flysystem-sftp-v3 "^3.0"` and an `sftp` disk.

Run one by hand to check:

```bash
php artisan backup:database
```

**Restore test, once a month, on the staging site** (never on production):

```bash
gunzip -c school_erp-YYYY-MM-DD-HHMMSS.sql.gz | mysql -u school_erp -p school_erp_staging
```

Then put the same `APP_KEY` in staging's `.env`, unzip the files backup into `storage/app/private/schools/`, and log in to
a school to check that students, documents and receipts open.

## 8. Releases

Use a staging site (`staging.example.in`, its own database) and release to production outside school hours:

```bash
php artisan down --refresh=15
```

```bash
git pull && composer install --no-dev --optimize-autoloader
```

```bash
php artisan migrate --force && php artisan permissions:sync
```

```bash
php artisan config:cache && php artisan route:cache && php artisan view:cache && php artisan queue:restart
```

```bash
php artisan up
```

## 9. Watching it

- Uptime monitor (UptimeRobot, Better Stack…) on `https://example.in/up` and one school's login page.
- Errors go to `storage/logs/laravel-YYYY-MM-DD.log` (kept 30 days, level `warning`). Check them daily during the pilot,
  or forward them to an error tracker.
- `php artisan erp:preflight` after every change to the server; it checks production mode, HTTPS, secure cookies,
  mail, queue, that the scheduler and the nightly backup ran, the offsite copy and that no demo password is left.
