# Deployment

SyncSHOP is released with [Deployer](https://deployer.org) from a developer machine to a Linux server. Nothing deploys on its own: each release is started by hand, and it stops before touching the server if any quality gate fails.

## What the server needs

- PHP 8.5 with `bcmath`, `curl`, `fileinfo`, `gd`, `intl`, `mbstring`, `openssl`, `pdo_pgsql`, `sodium`, `zip`
- PostgreSQL 16 or newer (18 recommended)
- Redis 7
- Node 22 and Composer 2 (assets are built on the server)
- nginx with PHP-FPM, and TLS for the shop's domain
- SSH access for the deploy user, and read access to the repository (a deploy key)

## One-time setup

1. **Database.** Create a database and an owner role. As the owner (or a superuser), enable the extensions:

   ```sql
   CREATE EXTENSION IF NOT EXISTS citext;
   CREATE EXTENSION IF NOT EXISTS unaccent;
   CREATE EXTENSION IF NOT EXISTS pg_trgm;
   CREATE EXTENSION IF NOT EXISTS ltree;
   ```

   The first migration creates them too, but only when the role is allowed to.

2. **Hosts file.** On your machine, copy `deploy/hosts.example.yml` to `deploy/hosts.yml` and fill in the server. The file is ignored by git, so server details never reach the repository.

3. **Shared `.env`.** Create `<deploy_path>/shared/.env` (mode `600`) from `.env.example`, with at least:

   ```dotenv
   APP_ENV=production
   APP_DEBUG=false
   APP_URL=https://shop.example.ro
   SESSION_SECURE_COOKIE=true
   LOG_LEVEL=warning
   BROADCAST_CONNECTION=reverb
   REVERB_HOST=shop.example.ro
   REVERB_PORT=443
   REVERB_SCHEME=https
   REVERB_SERVER_HOST=127.0.0.1
   REVERB_SERVER_PORT=8080
   ```

   Generate `APP_KEY` and the `REVERB_APP_*` values on the server, never on a shared machine.

4. **Services.** Install the templates from `deploy/server/` after replacing `<user>` and `<deploy_path>`:

   | Template | Installed as |
   |---|---|
   | `horizon.service` | `/etc/systemd/system/syncshop-horizon.service` (queues) |
   | `reverb.service` | `/etc/systemd/system/syncshop-reverb.service` (WebSockets) |
   | `sudoers` | `/etc/sudoers.d/syncshop` (lets the deploy reload PHP-FPM) |
   | `nginx-reverb.conf` | included in the shop's nginx `server` block |

   Add the scheduler to the deploy user's crontab; it runs every task registered in SyncSHOP's scheduler:

   ```cron
   * * * * * cd <deploy_path>/current && php artisan schedule:run >> /dev/null 2>&1
   ```

5. **Web root.** Point nginx's document root at `<deploy_path>/current/public`.

6. **First release, then install.** Run the first `vendor/bin/dep deploy`, then install the shop once, in the browser at `/install` or on the server with `php artisan syncshop:install` (see [Installation](installation.md)). Later releases don't touch the install.

## Releasing

```bash
vendor/bin/dep deploy production     # the host name from deploy/hosts.yml
vendor/bin/dep rollback production   # back to the previous release (the database stays as it is)
```

A release runs in this order:

1. **Quality gate, on your machine.** The branch must be pushed: the gate refuses if your local branch differs from the one on the server's remote. It exports that exact commit to a temporary folder, installs dependencies, builds the assets and runs `composer check`. Any failure ends the release here.
2. **On the server:** clone the commit, install Composer packages without dev dependencies, build and pre-compress the assets, link storage and cache the configuration.
3. **Database:** on hosts with `env_name: prod`, a `pg_dump` goes to `~/backups` (the last ten are kept), then migrations run, `permissions:sync` brings the permission catalog and system roles up to date, and `addresses:sync` loads the counties, localities and postcodes.
4. **Preflight:** `php artisan app:preflight` checks the configuration, Redis, PostgreSQL (including that the address data is loaded), PHP and storage. A failure stops the release before it goes live. Run it by hand on a new server first.
5. **Go live:** the `current` symlink switches to the new release, PHP-FPM reloads, and Horizon, Reverb and the queue workers restart on the new code.

Migrations only move forward in production: a rollback switches the code back, not the schema. Restore a `~/backups` dump if a migration must be undone.

## Updating SyncSHOP

On a server that runs SyncSHOP from Composer, `php artisan syncshop:update` checks, backs up, updates and rolls back
by itself on failure ([Updates, data feeds and releases](development/updates.md)). A shop deployed with Deployer
updates in its project instead: `composer update "syncshop/*"`, run the tests, commit `composer.lock`, then
`vendor/bin/dep deploy production` (the recipe in `syncshop/skeleton`).
