Deploy Mishka CMS to production

v0.0.3

A real Mishka CMS install is your own copy of the mishka_cms repository, built into a Docker image with the Dockerfile in its root. This guide builds that image, sets its environment, runs the migrations before the server and shows how to upgrade. You finish with a CMS that serves your sites from a container you control.

Before you begin

  • Your own copy of the mishka_cms repository, such as a fork.
  • Elixir 1.20 or later, Erlang/OTP and Bun on the machine where you generate the migrations. The .tool-versions file of the repository pins the versions the image is built with.
  • Docker, to build the image, and a PostgreSQL database the container can reach.
  • A way to put HTTPS in front of the container, such as a reverse proxy.

The demo image is not for this. Its migrations belong to one release and a newer demo needs a fresh database. See Try Mishka CMS with the demo image.

Generate and commit the migrations

The migrations follow the extensions your copy loads (ash_domains in config/config.exs), so the repository does not keep them. You generate them once and commit them to your copy:

bash
mix deps.get
mix oban.install --yes
mix ash.codegen first_install
git add priv/repo/migrations priv/resource_snapshots
git commit -m "Add the migrations"

The build fails when priv/repo/migrations holds no migration, and when mix ash.codegen --check finds that the resources describe a schema the committed migrations and snapshots do not. The message names the command to run. Neither check needs a database.

Build the image

Build the image from the root of your copy:

bash
docker build -t my-cms .

The build fetches the Mishka packages from Hex at the versions mix.lock pins, and it needs no GitHub token. The image runs the release as the user nobody and exposes port 4000.

Set the environment variables

The release reads its settings from environment variables when it starts. These are required:

VariableWhat it holds
DATABASE_URLThe database, such as ecto://USER:PASSWORD@HOST/DATABASE. The release stops at boot without it.
SECRET_KEY_BASEThe key that signs and encrypts cookies. Make one with mix phx.gen.secret. The release stops at boot without it.
TOKEN_SIGNING_SECRETThe secret that signs the tokens of the sign-in system. Make one with openssl rand -base64 32. The release stops at boot without it.
SESSION_SIGNING_SALTThe salt of the session cookie. When it is unset, the release uses a fixed default, so set your own.
MISHKA_SETUP_TOKENThe key to the install wizard, for the first run. In production /install refuses everyone while it is unset.

Keep the same values on every deploy. A new SECRET_KEY_BASE invalidates the cookies the old one signed. Keep the values out of version control.

These are optional:

VariableWhat it does
MISHKA_DATA_DIRWhere the release keeps its data. The image sets it to /app/mishka_data.
PORTThe port the server listens on. The default is 4000.
POOL_SIZEThe size of the database pool. The default is 10.
ECTO_IPV6Set it to true or 1 to connect to the database over IPv6.
PAGE_CODESet it to off to stop authors writing Elixir in pages, layouts, components and error pages for the whole deployment.
CODE_SIGNING_KEYWith a key set, Elixir that did not come through the CMS does not run. Protect page code with a signing key turns it on, and "Restore a database from another environment" below covers a restore.

Mount a volume for the data

Mount a volume at MISHKA_DATA_DIR (/app/mishka_data). It holds Mnesia, the store of Mishka Installer, the caches and every uploaded file. Without a volume this data is lost on each redeploy.

The image creates the folder owned by nobody, so a named volume is writable. A bind mount needs the same owner on the host, user ID 65534.

Mnesia keeps its schema under this folder for the node that created it. The image fixes the node name to mishka_cms@localhost, so a new container finds the schema of the old one.

Run the migrations, then the server

Run /app/bin/migrate before /app/bin/server, on every deploy. The image does not run migrations itself, and its default command starts the server. With plain Docker, run the migration as a one-off container, then start the server:

bash
docker run --rm --env-file production.env my-cms /app/bin/migrate

docker run -d --name mishka-cms -p 4000:4000 \
  --env-file production.env \
  -v mishka_data:/app/mishka_data \
  my-cms

In Docker Compose or a deploy platform, make the migration a service or a pre-start command that must succeed before the app starts. The examples/docker/demo-docker-compose.yml file shows this with depends_on.

Check that the node is ready

GET /_health tells a proxy or a deploy platform whether a container is ready to serve. It answers on any host, over plain HTTP too, before the CMS looks for a site or a session. The copy of the repository already excludes the path from the HTTPS redirect in config/prod.exs.

It answers 200 with the text ok only when all of this holds:

  1. The boot is done: every site is loaded and every precompiled page, layout, component and error page is built.
  2. Every active site has its stylesheet and its script bundle built.
  3. Every plugin on the runtime:health:ready event answers ready. See Plugin events.
  4. The database answers a SELECT 1.

Otherwise it answers 503 with the first reason as plain text, such as booting, boot failed, building assets: acme-docs css or database unavailable. A site whose stylesheet cannot be built keeps the node at 503, so a release that cannot build it never takes visitors. The answer is never cached. From inside the container:

bash
curl -fsS http://localhost:4000/_health

Point the health check of your platform at it, and send visitors to a new container only after it answers 200.

Put HTTPS in front and install

In production the endpoint redirects plain HTTP to HTTPS and trusts the X-Forwarded-Proto header of your proxy. Have the proxy end TLS, forward the requests to port 4000 with the original Host header and set X-Forwarded-Proto: https. A site is found by the host name of the request, so the domain you give the first site in the wizard must point at the proxy.

Then open https://your-domain/install?token= followed by your MISHKA_SETUP_TOKEN, and follow Install Mishka CMS and sign in from the setup wizard. Once the CMS is installed, /install answers as a page that does not exist.

Upgrade

An upgrade is a new build of your copy:

  1. Raise the version requirements of the Mishka packages in mix.exs if the new version is outside them.
  2. Update them: mix deps.update mishka_cms_core mishka_cms_builder mishka_blog mishka_document mishka_gervaz, and the other Mishka packages you load.
  3. Generate the migration the new versions need: mix ash.codegen upgrade_<version>, with the new version in the name.
  4. Commit mix.lock, priv/repo/migrations and priv/resource_snapshots.
  5. Build the image and deploy it. The new container runs /app/bin/migrate before the server.

If you forget step 3, the build stops at the migrations check and names the command.

Restore a database from another environment

With CODE_SIGNING_KEY set, the Elixir a page stores carries a signature made with that key. A database restored from an environment with another key holds signatures this deployment cannot verify, so those handlers compile to code that does nothing. The boot log then warns that the stored snippets carry no signature it can verify.

Read the warnings, which name each snippet. If the database is yours and the code is what you wrote, sign it again with this deployment's key. A release has no Mix, so call it on the running node:

bash
/app/bin/mishka_cms rpc 'MishkaCmsCore.Runtime.Authoring.CodeAudit.resign()'

It answers {:ok, count} with the number of rows it wrote, or {:error, :signing_off} when no key is set. It signs whatever is stored, so never run it on a database somebody edited by hand. Restart the node afterwards so the pages compile again. In development, mix mishka.resign_code does the same.

Never run mix dev_reset on a deployed database

The mix dev_reset alias is for throwaway development databases. It deletes priv/resource_snapshots and every migration, drops the database of the environment it runs in and the test database, and writes the migrations again with new timestamps.

Next steps