# Deployment

## Introduction

When you're ready to deploy your Laravel Bash application to production, there are some important things you can do to make sure it runs as efficiently as possible. There aren't many, because there isn't much of it.

## Server Requirements

The image in the `Dockerfile` has everything: Bash 5, the GNU tools, `socat`, `sqlite3`, `jq` and [tini](https://github.com/krallin/tini). Whatever runs it needs to give the app:

- **One long-running container.** `./artisan dev` starts the server, the queue workers, the event listener and the scheduler, and keeps them running.
- **A database that survives restarts.** Either Postgres, or SQLite on a disk mounted at `storage/`. The queue folders, the broadcast log and the rate limiter's counts live in `storage/` too, but losing them in a restart costs no more than a few jobs that hadn't run yet.
- **Exactly one instance.** Queue workers claim jobs with `mv`, events wake the listener with `pkill`, and broadcasts follow a file with `tail -F`. All of that assumes one machine. A second instance would have its own disk and its own processes, and neither would know about the other.
- **Responses that stream**, for [broadcasting](/docs/broadcasting).
- **A health check**, which `/up` answers.

## The Image

Locally, compose mounts the project into the container. In production, the image is the app, so the `Dockerfile` copies it in, and `.dockerignore` leaves out `.git`, `.env`, the local database and everything in `storage/`.

`tini` is PID 1. It passes `SIGTERM` on to `./artisan dev`, which stops the others and exits, and it reaps the processes `socat` forks for each request:

```dockerfile
ENTRYPOINT ["tini", "-s", "--"]
CMD ["./artisan", "dev"]
```

When `./artisan dev` starts, it makes any `storage/` folders that are missing, because a new, empty volume hides the folders the image came with. Then it waits up to a minute for the database to answer, and if it has never been migrated, it runs `migrate --seed` before anything else starts.

## Configuration

There's no `.env` in production. Set real environment variables instead:

```bash
APP_ENV=production
APP_DEBUG=false                                # error pages without the stack trace
APP_URL=https://laravel-bash.mrstebo.co.uk     # canonical URL for sitemap, Open Graph, llms.txt
DB_CONNECTION=pgsql                            # with DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD
PGSSLMODE=require                              # psql reads this itself
TRUSTED_PROXY_HEADER=X-Envoy-External-Address  # see Rate Limiting in the routing docs
MAX_CHILDREN=32                                # requests at once; the rest wait to be accepted
APP_DEMO=true                                  # run demo:reset every hour
```

`MAX_CHILDREN` goes to `socat` as `max-children`. Each open Server-Sent Events stream holds one until its tab closes, so leave room for them.

## Health Checks

Like Laravel, Laravel Bash has a health route at `/up`. It answers `200 Application up` when the database does, and `500` when it doesn't.

## Streaming Through a Proxy

Proxies close connections that go quiet. Broadcasting streams send a `: ping` comment line after 15 seconds without an event, which browsers ignore. Set `BROADCAST_HEARTBEAT` to change the interval.

## SQLite on a Volume

To keep everything in one container, put the SQLite database on the same volume as everything else:

```bash
DB_SQLITE_PATH=storage/database.sqlite
```

A volume can only be mounted by one container at a time, so deploys have to stop the old container before they start the new one. Expect half a minute of downtime on every deploy.

## Northflank

The repository has a [Northflank](https://northflank.com) template in `northflank.json`. It creates a project with:

- a Postgres addon;
- a secret group that hands the addon's connection details to the app as `DB_HOST`, `DB_PORT`, `DB_DATABASE`, `DB_USERNAME` and `DB_PASSWORD`;
- a combined service that builds the `Dockerfile` on every push to `main`, except for changes to files the app doesn't serve, like the README and the tests.

With nothing on the service's disk worth keeping, deploys are rolling: the new container starts, passes its health check, and only then does the old one stop.

With GitOps switched on for the template, Northflank reads the template from the repository and runs it again whenever `northflank.json` changes on `main`. There's no deploy pipeline.
