Skip to content
Darin Larimore
All posts

How I run every site on Forge

6 min read
On this page
  1. The cutover
  2. The deploy script
  3. Zero-downtime, where it earns its keep
  4. Daemons
  5. Env
  6. What Forge does not get to do
  7. The short version

In October I deleted four servers. Marketing sites and small client sites that had each been given their own small DigitalOcean box over the years, the smallest with 512 MB of RAM and a 10 GB disk that was 86% full, moved onto one 4 GB, 2 vCPU VPS provisioned through Forge. The four cost about $34 a month; the one that replaced them costs $24. The apps with databases and queues kept their own servers, and client apps stay separate from each other on purpose. Everything is provisioned, deployed and restarted through Forge.

The shape of this is in the fifteen-years post; this is the detail.

The cutover

What made the consolidation boring was ordering. Nearly every zone sits behind Cloudflare's proxy with strict SSL, so pointing an A record at a server with no certificate gives visitors a 525 until one exists, and a failed validation means rolling back. The order that worked: final sync of storage and database; copy the old server's certificate and key onto the new site as an existing certificate; flip the proxied A record; only then request Let's Encrypt, which replaces the copied cert; push-to-deploy on for the new site, off for the old. Nobody noticed.

The deploy script

Forge's default Laravel script does not build the front end, and the minimal script for a plain PHP site is git pull with no PHP-FPM reload. On a server where opcache has timestamp validation off, that omission means a deploy reports complete, the commit is right, and the site serves last week's code until something restarts FPM.

This is the script I paste into every flat-file site now, from this site's own repo:

set -euo pipefail

cd $FORGE_SITE_PATH
git pull origin $FORGE_SITE_BRANCH

$FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader

# Stale route/config caches make Wayfinder skip new routes during the Vite build.
$FORGE_PHP artisan optimize:clear

npm ci
npm run build

$FORGE_PHP artisan optimize

( flock -w 10 9 || exit 1
    echo 'Reloading PHP FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock

set -e is the important line. Without it a failed npm run build still ends in "Deployment complete" with the old assets serving. With it the deploy is red and the old release keeps serving, which is the same outcome with an honest label.

Statamic sites get two changes. The first line is a guard:

if [[ $FORGE_DEPLOY_MESSAGE =~ "[BOT]" ]]; then exit 0; fi

The control panel commits every content save through git automation with [BOT] in the message, and an editor fixing a typo should not trigger a build. Second, php artisan optimize is replaced with optimize:clear, view:cache and a stache warm, because config:cache cannot serialise the closure in the search config, and the static cache has to be cleared after the warm so cached HTML stops pointing at asset hashes the build just deleted. Leaving that step out cost me a site that deployed clean and lost its styles.

Zero-downtime, where it earns its keep

Flat-file sites deploy in place. Anything with a database and a queue (the billing portal, the client review tool, a game, an ops app) uses Forge's zero-downtime releases: each deploy is a fresh clone into releases/, current is a symlink, and the web directory is current/public. Shared paths like storage can only be declared when the site is created, and anything edited on the server dies on the next deploy, which is the point and also the trap.

Horizon has one gotcha on releases. horizon:terminate in the deploy script waits for in-flight jobs; with five-minute watermark jobs running two at a time, that blocked the pipeline. Setting fast_termination to true lets the new Horizon start immediately while the old workers finish out.

Daemons

Forge's scheduler integration runs schedule:run every minute. Queue workers are Forge daemons: Horizon for the apps on Redis, a plain queue:work for the Craft sites and the small apps on the database driver. I added Horizon to one app after a report job sat in the queue indefinitely, because the scheduler was running and nothing else was. Inertia SSR is a daemon too, with npm run build producing both bundles. Commands run through Forge's command runner are non-interactive, so any artisan command with a confirm() needs --force or it takes the default and cancels itself; I added that flag so a site announcement could be sent from Forge at all.

One thing I tried and undid: building assets in GitHub Actions, shipping a per-commit tarball to the server over SSH and then hitting the Forge deploy hooks, so a small server never ran Vite. It worked. I reverted it the same afternoon: a deploy key in Actions secrets, a tarball directory to prune and two webhook URLs, for a build the server was handling fine.

Env

Forge's environment tab is the only copy of production env. The repo carries deploy/env.production.example with the secrets blank, and a one-shot script creates the site through the API, fills that file with a generated APP_KEY, PUTs the environment and the deploy script, and triggers the first deploy. The mail and Turnstile keys get pasted in by hand. CI never sees any of it: the workflow copies .env.example, runs key:generate and composer ci:check, with dummy values in phpunit.xml for anything a test needs.

The trap with hand-edited env: after changing production .env on the box you need config:cache and an FPM reload, or opcache keeps serving the old config.

What Forge does not get to do

  • Mail. Outbound SMTP ports are blocked on the VPS tier anyway; everything goes through an API provider with a scoped key per domain.
  • DNS, TLS at the edge, bot checks. That is Cloudflare. Forge only ever sees an HTTP-01 challenge.
  • Be the gate. None of the repos I checked have branch protection, and push-to-deploy ships whatever lands on main, red CI included. The gate is composer ci:check before the push, on my machine.
  • Be believed. A green deploy is not a working deploy; the log is.

The short version

One VPS for the marketing sites and separate servers for client apps. One script with set -e and an FPM reload. Releases only where there is a database. Env in one place. Cloudflare and the mail provider doing the jobs the box should not. The deploy is boring, which is the whole point.