Run your CMS in production
Run the same project in production mode on a Linux server behind Caddy, keep its content across restarts, and keep the developer tools, the secrets and the private files away from visitors.
run-what, systemd, Caddy and real HTTPS) has been checked only by reading its files and by running run-what in production mode on a development machine. Treat it as a reviewed recipe that has not yet run on a Linux server.Allow 30–45 minutes, plus the time to get a server and a domain. This is an editorial estimate. For the server steps you need a Linux server with systemd where you can use sudo, a domain whose DNS you control, and ports 80 and 443 open. Following those steps deploys your site to that server.
Read
Start at L20. So far you ran run-what dev on your computer: it watches your files, reloads the browser, shows the inspector at /w-inspector, and explains errors in detail. A public site must do none of that. --production is the switch: no file watching or live reload, no developer tools, no debug output and generic error pages. Session cookies keep Secure, so the site needs HTTPS.
The recipe has four parts. run-what runs as its own account and listens on 127.0.0.1, so only programs on the same machine reach it. Caddy, a web server, answers visitors over HTTPS, gets the certificate itself, and passes each request on. systemd starts run-what when the server boots and restarts it if it fails. A file outside the project holds the one secret.
Two folders hold everything the site writes: data/ (the store, with your accounts and pages, and the sessions database) and uploads/. They must persist across restarts and upgrades. Everything else, the binary and the site files, can be replaced. A backup (lesson 22) holds the store and uploads/; sessions are never backed up.
Files in uploads/ are served to anyone who has their address, whatever the publication state of the page that links them; publication and sign-in protect pages, not files.
The server starts with a fresh database. On its first start it imports data/pages.json, the starter pages, as your computer did in lesson 12. It does not bring your current pages, edits, publication state or accounts: your project on your computer stays exactly as it is. In this lesson you create a new editor and publish a new page on the server. Moving existing content to another place is the backup and restore of lesson 22.
The secret, WHAT_SERVER_FORM_SECRET, signs the rules of forms that use w-validate. With the same secret such a form, opened before a restart, still submits after it; with a new secret it must be reloaded. The course's forms do not use w-validate and do not depend on it. Set it anyway, so forms you add later keep working across restarts. Left empty, run-what refuses to start.
Change
1. Keep a copy. Stop the dev server with Ctrl+C. Copy the whole my-site folder to a new folder such as my-site-before-L21.
2. Add the recipe. Create a deploy/ folder in my-site with these five files. Your own what.toml does not change: deploy/what.toml is the server's copy, with the public HTTPS address and the one proxy in front.
# systemd unit for one What site: run-what in production mode, reachable
# only from this machine, behind Caddy. Install as
# /etc/systemd/system/what.service. Prerequisites and steps: README.txt.
# Replace /srv/my-site and /etc/my-site if your paths differ.
[Unit]
Description=What site (my-site)
After=network.target
[Service]
Type=simple
User=what
Group=what
WorkingDirectory=/srv/my-site
ExecStart=/usr/local/bin/run-what dev --production --path /srv/my-site --host 127.0.0.1 --port 8085
# The form secret, outside the project, readable only by root (systemd
# reads it before it starts the service as the account above).
EnvironmentFile=/etc/my-site/env
# run-what shuts down cleanly on SIGINT (Ctrl+C), not on systemd's default
# SIGTERM.
KillSignal=SIGINT
Restart=on-failure
RestartSec=2
# Logs: journalctl -u what
StandardOutput=journal
StandardError=journal
# Within the project the service can write only data/ (the store and
# sessions) and uploads/; PrivateTmp gives it its own temporary folder.
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
ReadWritePaths=/srv/my-site/data /srv/my-site/uploads
UMask=0077
[Install]
WantedBy=multi-user.target
# Caddy serves the site over HTTPS (it obtains and renews the certificate)
# and passes each request to run-what on this machine. Install as
# /etc/caddy/Caddyfile. Replace example.com with your domain: its DNS must
# point at this server, ports 80 and 443 must be open, and the same origin
# must be in allowed_origins in the server's what.toml.
example.com {
reverse_proxy 127.0.0.1:8085
}
# The service's environment. Copy to /etc/my-site/env on the server, outside
# the project, then:
# sudo chown root:root /etc/my-site/env
# sudo chmod 600 /etc/my-site/env
#
# WHAT_SERVER_FORM_SECRET signs the rules of every <form w-validate>, so
# such a form opened before a restart still submits after it (with a new
# secret it must be reloaded). Forms without w-validate do not depend on
# it. At least 32 bytes.
# Generate one on the server with
# openssl rand -hex 32
# and paste it after the = sign. Left empty, run-what refuses to start
# ("[server] form_secret must be at least 32 bytes"). Never put it in
# what.toml, in the project folder or in version control.
WHAT_SERVER_FORM_SECRET=
# What Project Configuration: the server's copy. It is the project's
# what.toml with the [server] settings for running behind Caddy (see
# README.txt); keep the project's own copy for running on your computer.
[server]
port = 8085
host = "127.0.0.1"
# The address visitors use. Forms and sign-in accept state-changing
# requests only from this origin. Replace example.com with your domain,
# as in the Caddyfile.
allowed_origins = ["https://example.com"]
# Caddy, on this machine, is the one proxy in front of the site, so the
# visitor's address is the X-Forwarded-For entry it adds and rate limits
# apply per visitor, not to the proxy. Correct only while run-what listens
# on 127.0.0.1 and nothing but Caddy can reach it.
trusted_proxy_hops = 1
[session]
enabled = true
cookie_name = "w_session"
max_age = 604800
database = "data/sessions.db"
# The database owns the pages now. On the first start after this change the
# server copies data/pages.json into the database once, keeps every id, and
# records that it did. It never reads the file again.
[import]
pages = "data/pages.json"
# Editors may create, change and delete pages. A published page is
# unpublished before it can be deleted. No two pages may share a slug: the
# database itself refuses the second one.
[collections.pages]
create = "editor"
update = "editor"
delete = "editor"
unique = ["slug"]
# Visitors read published copies of these fields only. A page created now
# stays private until it is published.
public = ["title", "slug", "summary", "body"]
# Editors read the working copies, drafts included, from the admin area.
working_read = "editor"
# Editors publish. Publish copies a page's last save (its public fields) into
# the published copy visitors read; Unpublish removes that copy. Nobody can
# publish until this line exists, and only signed-in accounts can hold it.
publish = "editor"
# The server checks every new or changed page itself, whatever form sent it.
fields.writable = ["title", "slug", "summary", "body"]
fields.required = ["title", "slug"]
fields.max_length = { title = 120, summary = 280, body = 20000 }
fields.slug = ["slug"]
fields.immutable = ["slug"]
# Sign-in. The site keeps its own editor accounts in data/app.db. You add one
# from the terminal with `run-what admin add`: there is no built-in account and
# no password in any file of the project.
[auth]
enabled = true
provider = "local"
login_path = "/login"
after_login = "/"
PRODUCTION RECIPE: one What site on a Linux server with systemd, Caddy in front.
Not run on a Linux server. These files are checked by parsing
(scripts/recipe-check.py) and the configuration by starting run-what on a
development machine. Starting the service under systemd on Linux, real
TLS and hosting are unverified.
FILES
what.service systemd unit, installed as /etc/systemd/system/what.service
Caddyfile Caddy site, installed as /etc/caddy/Caddyfile
env.example the form secret's file, copied to /etc/my-site/env
what.toml the server's what.toml: the project's own, with [server]
set for Caddy (the public HTTPS origin, one proxy hop)
PREREQUISITES (yours to provide; nothing here creates them)
1. A Linux server with systemd, a domain whose DNS points at it, ports 80
and 443 open, and Caddy installed (Caddy obtains the certificate).
2. A run-what binary built for that server. A binary built on macOS does
not run on Linux. Build it from the What source with
cargo build --release --bin run-what on the server, or on a Linux
machine with the same architecture and a C library no newer than the
server's. Install it as /usr/local/bin/run-what, owned by root, mode
0755.
3. A service account that cannot log in and owns only the data, with its
own group of the same name (Group=what in the unit):
sudo useradd --system --user-group --home-dir /srv/my-site --shell /usr/sbin/nologin what
4. The two persistent folders, owned by the account, private, created
before the service starts (systemd refuses a missing ReadWritePaths):
sudo install -d -o root -g root -m 0755 /srv/my-site
sudo install -d -o what -g what -m 0700 /srv/my-site/data /srv/my-site/uploads
data/ holds the store (data/app.db, with -wal and -shm while running)
and the sessions database (data/sessions.db); uploads/ holds uploaded
files. Both folders must persist across restarts and upgrades. A
backup (L22) holds the store and uploads/ only: sessions are never
backed up or restored, so after a restore, sign-ins through the site's
own accounts start again.
5. The site files in /srv/my-site, owned by root, directories 0755 and
files 0644, so the service reads them and cannot change them:
this folder's what.toml as /srv/my-site/what.toml (with your domain),
site/, static/ and components/. Put data/pages.json (read once, on the
first start) in on its own, so data/ and uploads/ keep the owner and
mode from step 4:
sudo install -o root -g root -m 0644 data/pages.json /srv/my-site/data/pages.json
Never copy a whole data/ or uploads/ folder over them, and never
chown or chmod the project recursively. Copy no data/app.db, no
sessions database and no .env: the server creates its own data, and a
.env in the project would be loaded and travel with every copy of the
project.
6. The secret, outside the project:
sudo install -d -o root -g root -m 0755 /etc/my-site
sudo install -o root -g root -m 0600 env.example /etc/my-site/env
openssl rand -hex 32
Paste the generated value after WHAT_SERVER_FORM_SECRET= in
/etc/my-site/env (sudoedit /etc/my-site/env). Left empty, run-what
refuses to start.
7. Your domain in two places, the same origin in both: the site address
in the Caddyfile and allowed_origins in /srv/my-site/what.toml
("https://" plus the domain, nothing after it).
START
The first editor account (there is no built-in one), as the service
account so its files belong to it, before the first start:
sudo -u what /usr/local/bin/run-what admin add editor --path /srv/my-site
Then:
sudo install -o root -g root -m 0644 what.service /etc/systemd/system/what.service
sudo systemctl daemon-reload
sudo systemctl enable --now what
sudo install -o root -g root -m 0644 Caddyfile /etc/caddy/Caddyfile
sudo systemctl reload caddy
Logs: journalctl -u what. For JSON log lines, add WHAT_LOG_FORMAT=json to
/etc/my-site/env and restart.
WHY EACH SETTING
--production no file watching, live reload, developer tools, debug
output or detailed error pages; session cookies keep
Secure, so the site needs HTTPS (Caddy).
127.0.0.1 only Caddy, on the same machine, reaches run-what; the
plain HTTP port is never public. trusted_proxy_hops = 1 is
correct only while this holds.
allowed_origins state-changing requests are accepted only from the
public HTTPS origin.
WHAT_SERVER_FORM_SECRET signs the rules of <form w-validate> forms; with
the same secret such a form opened before a restart still
submits after it, with a new one it must be reloaded. Forms
without w-validate (the course's edit forms) do not depend
on it; keep it stable for the w-validate forms you add.
KillSignal=SIGINT run-what shuts down cleanly on SIGINT; systemd's
default SIGTERM stops it without the clean shutdown.
ProtectSystem=strict, ReadWritePaths within the project the service can
write only data/ and uploads/; the site files and what.toml
stay as you installed them. (PrivateTmp gives it its own
writable temporary folder outside the project.)
3. Try production mode on your computer. In the my-site folder, stop any running server with Ctrl+C, then run run-what dev --production and open http://127.0.0.1:8085. The public pages work, /w-inspector answers “Page Not Found”, and an error page says only that an error occurred. Signing in and saving are refused here: production expects the site's HTTPS address, and your computer serves http://. Keep editing with run-what dev. Stop the server with Ctrl+C.
4. On your server, follow deploy/README.txt: the binary built for the server, the account, the two folders, the site files, the secret, your domain in two places, then the first editor and the start. Your computer's data/ stays where it is: the server gets only data/pages.json.
Check
- In
my-site, stop any running server with Ctrl+C and runrun-what dev --production. Athttp://127.0.0.1:8085,/w-inspectorand/w-data/infoanswer “Page Not Found”, and/what.tomland/data/app.dbare never served. Stop it with Ctrl+C. - Run
run-what dev, sign in athttp://127.0.0.1:8085/login, create a draft and publish it. Stop it with Ctrl+C. Runrun-what dev --production: the public page is at its address. Stop it with Ctrl+C and runrun-what dev --productionagain: the page has the same address and the same content. Stop it with Ctrl+C. - On your server:
systemctl status whatsays active,journalctl -u whatshows the start, and your site answers onhttps://your domain with the starter pages. Sign in there as the editor you created, create a new page and publish it. Aftersudo systemctl restart whatthe page has the same address and content, and you are still signed in.
Exercise
Explain in two sentences why run-what listens on 127.0.0.1 and not on 0.0.0.0, and what trusted_proxy_hops = 1 would get wrong if it did.
If something goes wrong
The server items below are anticipated from the recipe and the code; they have not yet been seen on a Linux server.
- The service does not start. Read
journalctl -u what. “form_secret must be at least 32 bytes”: fill in/etc/my-site/env. A missing folder: createdata/anduploads/as in step 4 of the README. “Exec format error”: the binary was built for another system. - Sign-in or saving says the request was refused.
allowed_originsin the server'swhat.tomlmust be exactlyhttps://plus the domain in the Caddyfile. - The certificate is missing. The domain's DNS must point at the server and ports 80 and 443 must be open; see
journalctl -u caddy. - Start again from a known state. Download the L21 checkpoint into a new, empty folder; never unpack it over your project. It has no database, no account and no secret: run
run-what admin add editorin the new folder, thenrun-what dev.
Next: back to the course.