Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# interpolated by accident.

# --- Site mode -------------------------------------------------------------
# Exactly one site mode must be selected: `local` or `production`.
# Exactly one site mode must be selected: `local` or `production`, or `shared-caddy`
# for a production site behind the shared Caddy (docs/caddy.md#the-shared-caddy).
# Optional per-site profiles are added to the same list: `analytics`,
# `activitypub`, `supervisor`, and for a local site `mailpit`. Profiles are additive; adding
# an optional profile never changes the site mode.
Expand Down Expand Up @@ -73,6 +74,12 @@ GHOST_PORT="2368"
HTTP_PORT="80"
HTTPS_PORT="443"

# Behind the shared Caddy, and in its own directory: its network, which the
# manager created and no Compose project owns. Unset, a site with its own
# Caddy gets a network of its own.
# INGRESS_NETWORK="ghost-ingress"
# INGRESS_EXTERNAL="true"

# --- Lifecycle ------------------------------------------------------------
# Applied to long-running services only. One-shot jobs keep `restart: "no"`.
# production: unless-stopped
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ jobs:
- { name: Ghost nightly channel, script: ghost-nightly }
- { name: Migration from the released main layout, script: migrate-main }
- { name: Backup and restore, script: backup }
- { name: Shared Caddy, script: shared-caddy }
- { name: Import, script: import, node: "22" }
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ Linux. There is no native Windows launcher.
## Site modes

`install --local` selects Ghost and MySQL on loopback. A production install
adds Caddy with automatic HTTPS. [Configuration](docs/configuration.md#site-modes-and-profiles)
adds Caddy with automatic HTTPS; several production sites on one server share
one Caddy, set up with `install --infra` ([caddy.md](docs/caddy.md#the-shared-caddy)). [Configuration](docs/configuration.md#site-modes-and-profiles)
defines the modes and optional profiles; [installation](docs/install.md#optional-services)
explains how to enable them.

Expand Down
61 changes: 45 additions & 16 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
# credentials (MySQL root) that Ghost must not receive.
#
# Site modes are selected through COMPOSE_PROFILES and are additive:
# local ghost + db, Ghost published on 127.0.0.1:${GHOST_PORT}
# production ghost + db + caddy (HTTPS ingress)
# no-caddy ghost + db, a production site behind the operator's own proxy
# (nginx, Apache) on 127.0.0.1:${GHOST_PORT}
# local ghost + db, Ghost published on 127.0.0.1:${GHOST_PORT}
# production ghost + db + caddy (HTTPS ingress)
# no-caddy ghost + db, a production site behind the operator's own proxy
# (nginx, Apache) on 127.0.0.1:${GHOST_PORT}
# shared-caddy ghost + db, a production site behind the shared Caddy
# and `infra` is the shared Caddy's own directory: caddy alone, for the
# sites' routes (docs/caddy.md#the-shared-caddy).
#
# Variables a site needs and the shared Caddy does not are not `:?`-guarded,
# because Compose interpolates services its profiles leave out too:
# `./ghost-docker config validate` requires them for a site.
#
# Optional, per-site profiles: analytics, activitypub, and for local sites
# mailpit, and `supervisor`, which runs Ghost updates asked for in Ghost Admin
# (docs/upgrade-supervisor.md).
Expand All @@ -35,7 +43,7 @@ services:
# Do not alter this without updating the Tinybird Sync container as well
image: ${GHOST_IMAGE_REF:-${GHOST_IMAGE:-ghost}:${GHOST_VERSION:-6-next-alpine}}
restart: ${RESTART_POLICY:-unless-stopped}
profiles: [local, production, no-caddy]
profiles: [local, production, no-caddy, shared-caddy]
labels:
<<: *site-labels
org.ghost.docker.lifecycle: long-running
Expand All @@ -51,7 +59,7 @@ services:
# Container-owned keys. These override anything set in ghost.env.
environment:
NODE_ENV: ${NODE_ENV:-production}
url: ${URL:?URL is required (for example https://example.com or http://localhost:2368)}
url: ${URL:-}
admin__url: ${ADMIN_URL:-}
server__host: 0.0.0.0
server__port: "2368"
Expand All @@ -60,9 +68,9 @@ services:
database__connection__host: ${DATABASE_HOST:-db}
database__connection__port: ${DATABASE_PORT:-3306}
database__connection__user: ${DATABASE_USER:-ghost}
database__connection__password: ${DATABASE_PASSWORD:?DATABASE_PASSWORD is required}
database__connection__password: ${DATABASE_PASSWORD:-}
database__connection__database: ${DATABASE_NAME:-ghost}
tinybird__tracker__endpoint: ${URL}/.ghost/analytics/api/v1/page_hit
tinybird__tracker__endpoint: ${URL:-}/.ghost/analytics/api/v1/page_hit
tinybird__tracker__datasource: analytics_events
tinybird__adminToken: ${TINYBIRD_ADMIN_TOKEN:-}
tinybird__workspaceId: ${TINYBIRD_WORKSPACE_ID:-}
Expand Down Expand Up @@ -110,21 +118,24 @@ services:
# Unique alias. Generated proxy routes and helper clients address Ghost
# through this name, never through the bare `ghost` service name.
- ghost-${COMPOSE_PROJECT_NAME:-ghost}
ingress:
aliases:
- ghost-${COMPOSE_PROJECT_NAME:-ghost}

db:
image: mysql:8.0.44@sha256:f37951fc3753a6a22d6c7bf6978c5e5fefcf6f31814d98c582524f98eae52b21
restart: ${RESTART_POLICY:-unless-stopped}
profiles: [local, production, no-caddy]
profiles: [local, production, no-caddy, shared-caddy]
labels:
<<: *site-labels
org.ghost.docker.lifecycle: long-running
logging: *default-logging
expose:
- "3306"
environment:
MYSQL_ROOT_PASSWORD: ${DATABASE_ROOT_PASSWORD:?DATABASE_ROOT_PASSWORD is required}
MYSQL_ROOT_PASSWORD: ${DATABASE_ROOT_PASSWORD:-}
MYSQL_USER: ${DATABASE_USER:-ghost}
MYSQL_PASSWORD: ${DATABASE_PASSWORD:?DATABASE_PASSWORD is required}
MYSQL_PASSWORD: ${DATABASE_PASSWORD:-}
MYSQL_DATABASE: ${DATABASE_NAME:-ghost}
MYSQL_MULTIPLE_DATABASES: ${DATABASE_EXTRA_DATABASES:-activitypub}
volumes:
Expand Down Expand Up @@ -186,10 +197,12 @@ services:
networks:
- ghost_network

# A production site's own Caddy, or with `infra`, the shared Caddy, which
# serves the members' routes from caddy/sites/.
caddy:
image: caddy:2.10.2-alpine@sha256:953131cfea8e12bfe1c631a36308e9660e4389f0c3dfb3be957044d3ac92d446
restart: ${RESTART_POLICY:-unless-stopped}
profiles: [production]
profiles: [production, infra]
labels:
<<: *site-labels
org.ghost.docker.lifecycle: long-running
Expand All @@ -214,8 +227,11 @@ services:
depends_on:
ghost:
condition: service_started
# The shared Caddy has no Ghost of its own.
required: false
# Only what Caddy routes to is on this network: never MySQL.
networks:
- ghost_network
- ingress

traffic-analytics:
image: ghost/traffic-analytics:1.0.450@sha256:33c0ff8a369040e1455b790cf1711c2ea7f3174bcd26230dc9f7fc77b401816c
Expand All @@ -241,6 +257,9 @@ services:
ghost_network:
aliases:
- traffic-analytics-${COMPOSE_PROJECT_NAME:-ghost}
ingress:
aliases:
- traffic-analytics-${COMPOSE_PROJECT_NAME:-ghost}

activitypub:
image: ghcr.io/tryghost/activitypub:1.2.16@sha256:b440548b8a87eaf70c9440d4b157beee4898b85ba4294509926517a85e7573c7
Expand All @@ -260,10 +279,10 @@ services:
MYSQL_HOST: ${DATABASE_HOST:-db}
MYSQL_PORT: ${DATABASE_PORT:-3306}
MYSQL_USER: ${DATABASE_USER:-ghost}
MYSQL_PASSWORD: ${DATABASE_PASSWORD:?DATABASE_PASSWORD is required}
MYSQL_PASSWORD: ${DATABASE_PASSWORD:-}
MYSQL_DATABASE: ${ACTIVITYPUB_DATABASE_NAME:-activitypub}
LOCAL_STORAGE_PATH: /opt/activitypub/content/images/activitypub
LOCAL_STORAGE_HOSTING_URL: ${URL}/content/images/activitypub
LOCAL_STORAGE_HOSTING_URL: ${URL:-}/content/images/activitypub
depends_on:
db:
condition: service_healthy
Expand All @@ -273,6 +292,9 @@ services:
ghost_network:
aliases:
- activitypub-${COMPOSE_PROJECT_NAME:-ghost}
ingress:
aliases:
- activitypub-${COMPOSE_PROJECT_NAME:-ghost}

# Local sites only: catches the mail Ghost sends (staff invites, password
# resets, member sign-in links) and shows it in a web inbox. Newsletters go
Expand Down Expand Up @@ -324,7 +346,7 @@ services:
org.ghost.docker.lifecycle: one-shot
logging: *default-logging
environment:
MYSQL_DB: mysql://${DATABASE_USER:-ghost}:${DATABASE_PASSWORD:?DATABASE_PASSWORD is required}@tcp(${DATABASE_HOST:-db}:${DATABASE_PORT:-3306})/${ACTIVITYPUB_DATABASE_NAME:-activitypub}
MYSQL_DB: mysql://${DATABASE_USER:-ghost}:${DATABASE_PASSWORD:-}@tcp(${DATABASE_HOST:-db}:${DATABASE_PORT:-3306})/${ACTIVITYPUB_DATABASE_NAME:-activitypub}
depends_on:
db:
condition: service_healthy
Expand Down Expand Up @@ -413,3 +435,10 @@ volumes:

networks:
ghost_network:
# What Caddy reaches the site's services on. A site with its own Caddy gets
# one of its own. Behind the shared Caddy, and in its directory, it is the
# shared Caddy's network, which the manager creates and no project owns
# (`.env` names it).
ingress:
name: ${INGRESS_NETWORK:-${COMPOSE_PROJECT_NAME:-ghost}_ingress}
external: ${INGRESS_EXTERNAL:-false}
20 changes: 19 additions & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ The strict schema in [`src/meta.ts`](../manager/src/meta.ts) defines
`.ghost-docker.json`. It records installation provenance, resolved Ghost identity
and managed-file checksums. It is private, gitignored, atomically written and
machine-owned. Missing metadata is distinct from invalid metadata; commands
requiring it refuse either with an actionable diagnostic.
requiring it refuse either with an actionable diagnostic. The shared Caddy's
directory has metadata of its own `mode`, `infra`, with no site URL and no
Ghost; `requireMetadata` refuses it, so only `self-update` and `info` read it.

`source` distinguishes files installed from the image from files used in a
checkout. `stack.image` is the manager pin. `payload` stores the checksum of each
Expand Down Expand Up @@ -123,6 +125,22 @@ sites installed before, keeping any file in them that was edited. Caddy routes a
`templates/site.caddy`, then belong to the operator. [caddy.md](caddy.md) owns
route edits and reload instructions.

`src/shared.ts` is the shared Caddy: one Caddy project per daemon (`install
--infra`, the `infra` profile, and metadata of the `infra` mode with no
Ghost, so that `self-update` can keep its stack files and launcher current
without a backup, as it holds no data), on a network the manager creates
and labels with its directory, so no Compose project's `down` removes it. A
production install finds it by that network and selects the `shared-caddy` profile: Caddy's
own payload is left out, and its routes are a file in the shared Caddy's
`caddy/sites/`. The manager cannot see that directory, so it reads and writes
it through the running Caddy container (Engine API exec), as the directory's
owner; Caddy validates the whole configuration before it is reloaded, and
routes it refuses are removed. Duplicate domains (from Caddy's loaded
configuration) and projects (a routes file of that name) are refused before
any write. `remove` and a failed install's undo take a site's routes out
again. A site's backup holds a copy (`routes.caddy`), which a restore writes
back when the shared Caddy has none for the project.

`src/bundle/manifest.ts` defines the migration bundle schema and depends only on
zod so the exporter can share it. Import policy stays outside that schema.
`src/bundle/stage.ts` stages and validates input, `src/import.ts` sequences the
Expand Down
125 changes: 122 additions & 3 deletions docs/caddy.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

`./ghost-docker install --domain` writes this site's routes into
`caddy/sites/site.caddy`, once. From then on the file is yours, as the nginx
file Ghost-CLI generated was: the manager never rewrites it.
file Ghost-CLI generated was: the manager never rewrites it. On a server with
[the shared Caddy](#the-shared-caddy), the routes go into the shared Caddy's
directory instead, in a file named for the site.

## Layout

Expand Down Expand Up @@ -32,8 +34,8 @@ A site moved from `main` keeps its own Caddyfile as `caddy/sites/site.caddy`
main's snippets, which take no arguments, from `caddy/sites/legacy-snippets/`,
and proxies to bare service names (`ghost:2368`), which resolve on the site's
own network. Both keep working. Moving to this layout's snippets and aliases,
as `install` writes them, is optional, and is needed before the site joins a
shared Caddy.
as `install` writes them, is optional, and is needed before the site could
join a shared Caddy.

## Changing routes

Expand Down Expand Up @@ -92,3 +94,120 @@ ActivityPub routes are always present. With the `activitypub` profile they
point at this site's own service; otherwise at the hosted service,
`https://ap.ghost.org`. ActivityPub, its migration job, database grants,
storage and serving URL all belong to the site.

## The shared Caddy

Only one program can hold ports 80 and 443, so several production sites on
one server share one Caddy, as Ghost-CLI's sites shared one nginx. Everything
else stays each site's own: Ghost, MySQL, ActivityPub, analytics, backups,
updates and restores work exactly as they do for a site on its own. A local
site never uses the shared Caddy, and a server without one runs each
production site with its own Caddy, as before.

Set it up once, in a directory of its own, before the sites:

```bash
mkdir ~/caddy && cd ~/caddy
curl -fsSL https://docker.ghost.org/install.sh | bash -s -- install --infra
```

Then install each site as usual, each in its own directory. On a server with
the shared Caddy, `install --domain` joins it: the site has no Caddy of its
own, and its routes are written to the shared Caddy's
`caddy/sites/<project>.caddy`, which Caddy validates and loads before the
site starts. Its first line, `# ghost-docker install <token>`, names the
write, so a failed install removes the file only while it is still that
write's. A site installed `--no-caddy` does not join it. `<project>` is the site's `COMPOSE_PROJECT_NAME`. A domain the
shared Caddy already serves, or a project that already has routes there, is
refused before anything is written. An existing site cannot be moved behind
the shared Caddy yet; it keeps its own Caddy, which needs 80 and 443, so set
the shared Caddy up on a server before its first production site.

| Path, in the shared Caddy's directory | Owner |
| --- | --- |
| `caddy/sites/<project>.caddy` | one per site behind it: written by that site's `install`, then yours |
| `caddy/custom/*.caddy`, `caddy/global/*.caddy` | yours, as for a single site |

Edit a site's routes as described above, then reload the shared Caddy, in
its directory:

```bash
docker compose exec caddy caddy reload --config /etc/caddy/Caddyfile
```

A configuration Caddy does not load is not applied: every site keeps being
served as before, and Caddy says why. `./ghost-docker check` in a site's
directory reports the shared Caddy's health, the site's routes file, and
HTTPS through it. `./ghost-docker list` shows the shared Caddy as `infra`,
and each site behind it as `shared-caddy`.

`./ghost-docker remove`, in a site's directory, removes its routes file,
reloads the shared Caddy, and takes the site's containers down; no other site
is touched. The site's directory, data and backups stay until you delete them.
A site's backup holds its routes, read from the shared Caddy. Restoring it
keeps the routes the shared Caddy has for the project, since they may have
been edited since, and otherwise writes the backup's back: a site removed, or
moved to a new server with a shared Caddy of its own, comes back with them.
The shared Caddy's own directory has no backup; it holds no data, and
`install --infra` sets it up again, with your edits to its Caddyfile and
snippets yours to keep.

### How it is connected

The shared Caddy is its own Compose project (`ghost-infra` unless `--name`
says otherwise), with the `infra` profile. `install --infra` creates the
network `ghost-ingress`, which belongs to no Compose project, so taking the
shared Caddy down never takes it away from the sites. Each site behind it
selects the `shared-caddy` profile, and its Ghost, ActivityPub and analytics join
`ghost-ingress` under their unique aliases (`ghost-<project>:2368` and so
on), which is why routes always use them. MySQL and every one-shot job stay on
the site's own network.

Ghost is still published on the site's loopback port, so a site behind the
shared Caddy can be reached without it, for example from your own proxy.

### What it trusts

The shared network is not isolation between sites. Every container on it can
reach every other site's Ghost, ActivityPub and analytics, and a site's bare
service names (`ghost`, `activitypub`) answer for every site on it, which is
why nothing routes by them. MySQL is never on it. Run sites behind one Caddy
only when you would trust them on one server anyway, as with Ghost-CLI.

The shared Caddy's directory holds every site's routes, and its `caddy_data`
volume their certificates. Its `caddy/` is writable by whoever installed it, and the manager writes a site's
routes as the owner of `caddy/sites/`.

### Restarting and updating it

Restarting the shared Caddy (`docker compose restart`, or `down` then
`up -d`, in its directory) interrupts every site behind it for those seconds,
and brings each back with its routes; certificates are kept in its
`caddy_data` volume. A site's own operations (`update`, `self-update`,
`backup`, `restore`, `docker compose down`) never restart it.

Its Caddy image is pinned in its `compose.yml`, by the release it runs.
`./ghost-docker self-update`, in its directory, moves it to a newer release as
for a site: untouched stack files are replaced, edited ones kept beside the
release's `.new`, and the launcher re-pinned; then Caddy is recreated, which
restarts it as above, reloads its configuration (so a changed Caddyfile or
snippet is loaded even when the container was not recreated), and must pass
its health check. Its reloads and those of each site's `install` and `remove`
take turns, through a lock on `caddy/sites/` inside the container, so none
loads a configuration read before another site's routes were written. There is no backup,
because it holds no data. The release's images are pulled before any file is
written, so one that cannot be pulled changes nothing. Once files are written,
a site's reload can load them, so a failed update puts its files back and
reloads them into Caddy, or starts the previous Caddy again when it had been
recreated. It never updates the sites behind it, and their own
`self-update` never touches it.

### Capacity

Caddy itself costs little per site: a few routes, and a certificate it renews
in the background. What limits a server is each site's own Ghost and MySQL,
and ActivityPub and analytics where selected, which cost what they would on a
server of their own ([configuration.md](configuration.md#site-modes-and-profiles)
lists the optional services' costs). Size the server for the sum of them.
Let's Encrypt limits how many certificates one account obtains per hour;
installing many sites at once can reach it, and Caddy retries later.
Loading
Loading