# Deploying on a VPS with Docker

One image runs three containers (website, queue worker, scheduler), plus MySQL and Caddy, which provides
HTTPS with automatic Let's Encrypt certificates.

## 1. Server

Any Ubuntu 24.04 VPS with 2 GB RAM or more. Install Docker Engine and the compose plugin
(https://docs.docker.com/engine/install/ubuntu/). Open ports 80 and 443 in the firewall.
Point the domain's DNS (A record, and `www` too) at the server's IP before the first start,
otherwise the certificate can't be issued.

## 2. First start

```bash
git clone <your repository> /opt/vnc-website && cd /opt/vnc-website   # or upload and unzip the project
cp .env.example .env
nano .env     # set APP_URL, SITE_DOMAIN, ACME_EMAIL, DB_PASSWORD, DB_ROOT_PASSWORD, MAIL_*
docker compose build
docker compose run --rm --no-deps app php artisan key:generate --show   # paste the result into APP_KEY in .env
docker compose up -d
docker compose logs -f app      # wait for "Empty site: loading starting content" and supervisord starting
docker compose exec app php please make:user --super    # create your control-panel login
```

The site is then live at `https://SITE_DOMAIN`, with the control panel at `https://SITE_DOMAIN/cp`.

On the very first start the website container runs the database migrations and, because the site has no
pages yet, loads the starting content. It never does this again once pages exist.

## 3. Backups

```bash
crontab -e
# add:
30 2 * * *  cd /opt/vnc-website && ./docker/backup.sh >> /var/log/vnc-backup.log 2>&1
```

This saves the database and uploaded files to `./backups` every night and keeps 14 days. **Also copy the
backups off the server** (for example `rclone` to Google Drive or S3); a backup on the same disk doesn't
survive losing the server.

Restore: `gunzip -c backups/db_X.sql.gz | docker compose exec -T db sh -c 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE"'`
and `docker compose run --rm --no-deps -T --entrypoint sh app -c 'tar -xzf - -C /var/www/html' < backups/files_X.tar.gz`.

## 4. Updating the site's code

```bash
cd /opt/vnc-website && git pull     # or upload the new files
docker compose up -d --build        # rebuilds the image; migrations run automatically
```

Content edited in the control panel lives in the database and the `assets` volume, so code updates never
overwrite it.

## Razorpay

1. In `.env`, set `RAZORPAY_KEY_ID`, `RAZORPAY_KEY_SECRET` and `RAZORPAY_WEBHOOK_SECRET`.
2. In the Razorpay dashboard, **Settings → Webhooks → Add**: URL `https://SITE_DOMAIN/!/vnc-payments/razorpay/webhook`,
   the same secret, and the events **payment.captured** and **payment.failed**.
3. Check **Addons → VNC Payments → Settings** in the control panel: plan names and fees must match VNC Roots.

## VNC Roots

1. In `.env`, set `VNC_ROOTS_SECRET` (VNC Roots → Settings → Website donations) and, optionally, `VNC_ROOTS_ALERT_EMAILS`.
2. Ask VNC to add the server's public IP address to VNC Roots' allowed list (`curl -4 ifconfig.me` on the server shows it).
3. The `queue` and `scheduler` containers must be running (`docker compose ps`): the queue sends payments,
   the scheduler retries anything that's waiting.
4. Make a ₹10 donation and check **Content → Payments**: the VNC Roots column should say "Sent to VNC Roots".

## Form emails

Set the SMTP details (`MAIL_*`) in `.env` and, in the control panel, the notification addresses under
**Globals → Site Settings → Forms**. The `queue` container sends the emails; failed sends are retried
automatically and listed by `docker compose exec app php artisan queue:failed`.

## Importing the old website's form submissions (once)

1. Export the old database from phpMyAdmin (or `mysqldump vncindia_website > old.sql`) and copy the old site's
   `uploads/` folder (the CVs) to the server, e.g. `/opt/vnc-website/old-uploads`.
2. Load the dump into a temporary database in the `db` container:
   ```bash
   docker compose exec -T db sh -c 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "CREATE DATABASE vncindia_website_old"'
   docker compose exec -T db sh -c 'mysql -u root -p"$MYSQL_ROOT_PASSWORD" vncindia_website_old' < old.sql
   ```
3. In `.env`, set `LEGACY_DB_PASSWORD` to the same value as `DB_ROOT_PASSWORD`, then run:
   ```bash
   docker compose up -d app
   docker compose cp old-uploads app:/tmp/old-uploads
   docker compose exec app php artisan vnc:import-old-forms --dry-run
   docker compose exec app php artisan vnc:import-old-forms --uploads=/tmp/old-uploads
   ```
   It's safe to run again; already imported rows are skipped, and no notification emails are sent.
4. Afterwards remove the temporary database (`DROP DATABASE vncindia_website_old`), the dump, the copied uploads
   and the `LEGACY_DB_*` lines.

## Control panel accounts

- Create named accounts for each person (`docker compose exec app php please make:user`), never a shared login.
- Give content editors the **Editor** role; only staff who handle donations need **View payments** /
  **Export payments** (Users → Roles).
- Ask every administrator to turn on **two-factor authentication** (their avatar → Two-factor authentication).

## Useful commands

| What | Command |
| --- | --- |
| See what's running | `docker compose ps` |
| Website errors | `docker compose logs --tail=100 app` or `docker compose exec app tail -n 100 storage/logs/laravel.log` |
| Clear caches after a config change | `docker compose exec app php artisan optimize:clear && docker compose restart app` |
| Reset a forgotten password | `docker compose exec app php please make:user --super` (new account), or use "Forgot password" on the login page once email is set up |
| Re-load starting content into an empty site | `docker compose exec app php artisan vnc:seed-content` |

## Volumes (where data lives)

| Volume | Contents |
| --- | --- |
| `db` | MySQL: all content, users, form submissions |
| `assets` | Uploaded images and PDFs (`public/assets`) |
| `storage` | Private files such as CVs (`storage/app`) |
| `statamic` | Generated image sizes and caches; safe to delete |
| `logs` | Application logs |
| `caddy-data` | HTTPS certificates |
