# Deployment Guide

Hosting is not chosen yet. These steps work for a VPS or for shared hosting with SSH. **Do not deploy without approval and a backup.**

## Requirements
- PHP 8.2+ with `pdo_mysql`, `mbstring`, `openssl`, `fileinfo`, `gd` (WebP support), `ctype`, `tokenizer`, `xml`, `bcmath`, `curl`.
- MySQL 8+ or MariaDB 10.4+.
- Composer 2. Node 20+ only on the machine that builds assets.
- HTTPS certificate.

## First release
1. Build assets locally or in CI: `npm ci && npm run build` (creates `public/build`).
2. Upload the code without `node_modules`, `.env`, `.mcp.json`, `storage/figma-raw` and `tests/e2e` output.
3. On the server:
   ```bash
   composer install --no-dev --optimize-autoloader
   cp .env.example .env        # then edit the values
   php artisan key:generate
   ```
4. `.env` production values: `APP_ENV=production`, `APP_DEBUG=false`, `APP_URL=https://<domain>`, database credentials, SMTP settings, `SESSION_SECURE_COOKIE=true`.
5. Create the database and a user that has rights on this database only.
6. Run:
   ```bash
   php artisan migrate --force
   php artisan db:seed --force          # pages, sections, settings, Figma images (run once)
   php artisan storage:link
   php artisan admin:create             # first Super Admin; asks for the password
   php artisan optimize                 # config, route, view and event cache
   ```
7. Point the web server document root to `public/`. The project root, `.env`, `storage` and `vendor` must not be reachable from the web.
8. Permissions: the web user needs write access to `storage/` and `bootstrap/cache/` only.

## Queue worker (enquiry emails)
Emails are sent by a queued job (`QUEUE_CONNECTION=database`).

- **VPS:** run a supervised worker:
  ```
  php artisan queue:work --sleep=3 --tries=5 --max-time=3600
  ```
  Example Supervisor program: `command=php /var/www/ananya/artisan queue:work --sleep=3 --tries=5 --max-time=3600`, `autorestart=true`, `user=www-data`.
- **Shared hosting without a long-running process:** add a cron entry every minute:
  ```
  * * * * * cd /path/to/app && php artisan queue:work --stop-when-empty --tries=5 >> /dev/null 2>&1
  ```
- Failed jobs are listed with `php artisan queue:failed` and retried with `php artisan queue:retry all`. The enquiry is always saved first, so a mail failure never loses an enquiry.

No Laravel scheduler is needed, because scheduled publishing is deferred.

## Enquiry retention (optional)
Archived enquiries can be deleted after the retention period (`CMS_ENQUIRY_RETENTION_DAYS`, default 730 days). Check first, then run, or add a monthly cron entry:
```
php artisan enquiries:prune --dry-run
0 3 1 * * cd /path/to/app && php artisan enquiries:prune >> /dev/null 2>&1
```
Active (not archived) enquiries are never deleted.

## Updating a release
```bash
php artisan down --render="errors::503"
# deploy new code + public/build
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan optimize:clear && php artisan optimize
php artisan queue:restart
php artisan up
```
Do **not** re-run `db:seed` to change content. The seeders skip existing pages, but content should be edited in the CMS.

## Backups
- Database: daily `mysqldump --single-transaction` with 30 days retention, stored off the server.
- Media: daily copy of `storage/app/public/media`.
- Test a restore on a staging copy at least every quarter.

## Rollback
1. `php artisan down`.
2. Restore the previous code release and `public/build`.
3. If a migration ran, restore the database backup taken before the release. Do not run `migrate:rollback` on production data without checking what it drops.
4. `php artisan optimize:clear && php artisan optimize && php artisan up`.

## Logs and monitoring
- `storage/logs/laravel.log`: set `LOG_CHANNEL=daily` and `LOG_DAILY_DAYS=14` for rotation.
- Watch for `Enquiry notification emails failed` errors and failed jobs.
- `/up` is the health check URL.
