# Deploying Laravel (a Laravel Forge replacement) ts-cloud can provision and deploy a Laravel (or any PHP) app on your own box — a drop-in replacement for Laravel Forge. It provisions **nginx + php-fpm + Composer**, on-box **MySQL/MariaDB/Postgres + Redis/Memcached/Meilisearch**, deploys from **git into zero-downtime atomic releases** (Envoyer-style), issues **Let's Encrypt** certificates, runs **queue workers + the scheduler + daemons**, and adds **UFW**, **automatic security updates**, **monitoring**, **scheduled backups**, and **Slack/Discord/Telegram/email/webhook notifications**. ## Quick start (preset) ```ts // cloud.config.ts import { createLaravelPreset } from '@stacksjs/ts-cloud' export default createLaravelPreset({ name: 'Acme', slug: 'acme', domain: 'acme.com', repository: { url: 'git@github.com:acme/app.git', branch: 'main' }, phpVersion: '8.3', sslEmail: 'ops@acme.com', databasePassword: process.env.DB_PASSWORD, }) ``` ```sh cloud deploy --env production ``` This provisions the server (first run) and deploys the app: clone, then `composer install`, `artisan migrate --force`, config/route/view/event cache, `storage:link`, an atomic flip of the `current` symlink, and a queue restart. SSL is issued and the queue worker + scheduler are started. ## Full configuration ```ts import type { CloudConfig } from '@stacksjs/ts-cloud' export default { project: { name: 'Acme', slug: 'acme', region: 'us-east-1' }, cloud: { provider: 'hetzner' }, environments: { production: { type: 'production', domain: 'acme.com' } }, notifications: { slack: { webhookUrl: process.env.SLACK_WEBHOOK! }, }, infrastructure: { compute: { runtime: 'php', webServer: 'nginx', // or 'rpx' php: { versions: ['8.3', '8.2'], default: '8.3' }, managedServices: { mysql: true, redis: true, meilisearch: true }, firewall: { enabled: true, allowedPorts: [8080] }, autoUpdates: true, // unattended-upgrades (the OS) appUpdates: [ // self-updating tools you deploy { service: 'mail', binary: '/opt/mail/mail-server' }, ], monitoring: true, backups: { enabled: true, schedule: '0 2 * * *', retentionCount: 7, bucket: 'acme-backups' }, }, appDatabase: { engine: 'mysql', name: 'acme', username: 'acme', password: process.env.DB_PASSWORD }, }, sites: { main: { root: '.', type: 'laravel', // laravel | php | statamic | wordpress | static | spa domain: 'acme.com', aliases: ['www.acme.com'], phpVersion: '8.3', repository: { url: 'git@github.com:acme/app.git', branch: 'main', strategy: 'push' }, ssl: { provider: 'letsencrypt', email: 'ops@acme.com' }, scheduler: true, queues: [ { connection: 'redis', queue: 'default,emails', processes: 2, tries: 3, timeout: 90 }, ], daemons: [ { command: 'php artisan reverb:start', name: 'reverb' }, ], sharedPaths: ['storage', '.env'], keepReleases: 5, }, }, } satisfies CloudConfig ``` ## Deploy strategies - **Push to deploy** (`strategy: 'push'`, default) — deploys the tip of `branch`. - **Tag deploy** (`strategy: 'tag'`) — deploys a specific `tag`, or the highest tag matching `tagPattern`. Useful for promoting tagged releases. ```ts repository: { url: '…', strategy: 'tag', tagPattern: 'v*' } ``` ### Push-to-deploy (CI pipeline) ts-cloud deploys from your machine or CI, so Forge's deploy webhook becomes a provider-native pipeline. `cloud quick-deploy` detects GitHub, GitLab, or Bitbucket from the `origin` remote (with `site.repository.provider` as a fallback) and scaffolds a workflow that runs `cloud deploy` on every push to the target environment's `deployBranch`: ```ts environments: { production: { type: 'production', deployBranch: 'main' }, } ``` ```sh cloud quick-deploy # writes the CI file for the configured provider cloud quick-deploy --force # overwrite an existing file cloud quick-deploy --site api --skip-dns-verification ``` Use `--provider github|gitlab|bitbucket` when the origin remote is unavailable in the current checkout. `--site` limits the generated deploy command to one site; without it, the environment's configured sites deploy together. GitHub workflows use the Pantry setup action when `pantry.lock` exists and Bun otherwise. Hetzner compute workflows also install the `SSH_PRIVATE_KEY` repository secret as the deployment key (and derive its public key for first-time provisioning); customize the secret name with `--ssh-key-secret `. | `provider` | Generated file | | --- | --- | | `github` | `.github/workflows/deploy.yml` | | `gitlab` | `.gitlab-ci.yml` | | `bitbucket` | `bitbucket-pipelines.yml` | `custom` (or no repository) skips generation — wire your own pipeline to call `cloud deploy`. ## Site types `type` selects the deploy script + nginx template: | `type` | Web root | Deploy script | | --- | --- | --- | | `laravel` | `public/` | composer + `artisan migrate`/cache/queue restart | | `statamic` | `public/` | full Laravel deploy (composer + artisan) | | `wordpress` | release root | optional composer, no artisan; WP hardening | | `php` | release root | optional composer, no artisan | | `static` | release root | files only (clone + activate) | | `spa` | release root | files only, SPA fallback to `index.html` | **WordPress** vhosts add hardening automatically: `xmlrpc.php` is denied, `wp-config.php`/`readme.html`/`license.txt` are blocked, and PHP execution inside `wp-content/uploads` is refused. (Bedrock serves from `web/` — set `webDirectory: 'web'`.) **Statamic** is treated as a Laravel app. ## Zero-downtime releases Every deploy clones into `releases/`, links the shared `storage`/`.env`, runs the deploy script, then atomically flips `current`. A failed step leaves the previous release serving. The last `keepReleases` (default 4) are retained for rollback. ## SSL certificates By default every site with a `domain` gets a free **Let's Encrypt** certificate issued by certbot (HTTP-01), with auto-renewal and an nginx reload on renewal. You can also bring your own certificate, or issue a **wildcard** via DNS-01. ```ts sites: { main: { // Let's Encrypt (default when a domain is set) ssl: { provider: 'letsencrypt', email: 'ops@acme.com' }, }, } ``` **Custom certificate** — install an operator-provided cert/key (Forge's "Install Existing Certificate"): ```ts ssl: { provider: 'custom', certPath: '/etc/ssl/acme.crt', keyPath: '/etc/ssl/acme.key' } ``` **Wildcard / DNS-01** — `*.acme.com` requires DNS-01 validation through your DNS provider (cloudflare, route53, digitalocean, google). The credentials are written to a root-only INI; route53 can use the instance role instead. ```ts ssl: { provider: 'letsencrypt', email: 'ops@acme.com', wildcard: true, dns: { provider: 'cloudflare', credentials: { dns_cloudflare_api_token: process.env.CLOUDFLARE_DNS_TOKEN! }, propagationSeconds: 30, }, } ``` ### HSTS & TLS hardening Force HTTPS in browsers with an HSTS header, and pin the TLS protocol versions nginx serves. Both live under the site's `ssl`: ```ts ssl: { provider: 'letsencrypt', email: 'ops@acme.com', // true ⇒ `max-age=31536000; includeSubDomains`; or customize: hsts: { maxAge: 63072000, includeSubDomains: true, preload: true }, // Restrict to modern TLS (applies to the custom-cert :443 block). tlsProtocols: ['TLSv1.2', 'TLSv1.3'], } ``` `hsts: true` emits a one-year `Strict-Transport-Security` header with `includeSubDomains`. Only enable `preload` once every subdomain is HTTPS-only. ## Database users Beyond the application user, provision extra database logins — read-only reporting accounts, per-service users, etc. (Forge's "Database Users"). Each can be granted full or read-only access to one or more databases, and re-provisioning resets the password. ```ts infrastructure: { appDatabase: { engine: 'mysql', name: 'acme', username: 'acme', password: process.env.DB_PASSWORD, users: [ { username: 'reporter', password: process.env.RO_PASSWORD, access: 'readonly' }, { username: 'analytics', password: process.env.AN_PASSWORD, databases: ['acme', 'metrics'] }, ], }, } ``` `access: 'readonly'` grants `SELECT` only on MySQL/MariaDB (and `CONNECT` + `USAGE` + `SELECT`, including future tables, on Postgres). The default is full read/write. ## PHP settings & OPcache PHP boxes are tuned for production by default (Forge's "Optimize for Production"): OPcache is enabled with timestamp validation **off** (every deploy restarts php-fpm, so a fresh release always recompiles), larger interned-string and accelerated-file buffers, and a bigger realpath cache. Override or extend with custom `php.ini` directives. ```ts infrastructure: { compute: { php: { versions: ['8.3'], optimizeForProduction: true, // default ini: { memory_limit: '512M', upload_max_filesize: '128M', post_max_size: '128M', }, }, }, } ``` Set `optimizeForProduction: false` to skip the OPcache tuning (your `ini` overrides still apply). The directives are written as a managed `php.ini` drop-in discovered at runtime, so they work regardless of where PHP is installed. The pantry `php.net` build already bundles the full Laravel extension matrix (mbstring, xml, curl, pdo_mysql, pdo_pgsql, redis, gd, bcmath, zip, intl, openssl, sodium, …). `php.extensions` is accepted for documentation/intent but that build is fixed — extra extensions (e.g. `imagick`, `swoole`) require a custom PHP build in pantry, not a runtime flag. ## Nginx configuration Inject custom directives into a site's generated `server { … }` block (Forge's "Edit Nginx Configuration"), and define **reusable templates** shared across sites. ```ts infrastructure: { compute: { // Reusable directive blocks, referenced by name from a site. nginxTemplates: { hardening: [ 'add_header X-Robots-Tag "noindex, nofollow";', 'location ~* \\.(env|git) { deny all; }', ], }, }, }, sites: { main: { nginx: { template: 'hardening', // inject the shared block clientMaxBodySize: '256M', // per-vhost upload ceiling serverSnippet: ['gzip on;', 'location /health { return 200; }'], }, }, } ``` The resolved template lines plus the per-site `serverSnippet` are inserted after the managed root/locations, so the framework defaults still apply. ## Deployment history Every deploy tees its full output to a per-release log and appends a record to a history log on the box (Forge's deployment log) — both successful and failed deploys are captured. Inspect it with the CLI: ```sh cloud deploy:history # most recent deploys for the first site cloud deploy:history main --limit 50 ``` Logs live under `/.ts-cloud/` (outside `releases/`, so they survive pruning); per-deploy logs are capped to the release keep-count. ## Scheduler & heartbeat monitoring Enable the Laravel scheduler (a per-minute `php artisan schedule:run` cron) and, optionally, attach a **heartbeat monitor** (healthchecks.io / Oh Dear style) that is pinged only after a successful run — so you're alerted if the scheduler stops. ```ts sites: { main: { scheduler: true, // simplest form // …or with heartbeat monitoring: scheduler: { heartbeatUrl: 'https://hc-ping.com/your-uuid', heartbeatMethod: 'GET', // GET (default) | POST | HEAD }, }, } ``` ## Rollback & recipes (CLI) Roll a site back to a previous release, or run a reusable script across every server (Forge's "Recipes"): ```sh # Roll back to the previous release (atomic flip + php-fpm/queue restart) cloud deploy:rollback main # Roll back to a specific release id cloud deploy:rollback main --to 20240601120000 # Run a local bash script on every server, as a chosen user cloud deploy:recipe clear-opcache ./recipes/clear-opcache.sh --user www-data ``` ### CLI reference (Forge-parity commands) | Command | What it does | | --- | --- | | `cloud deploy --env production` | Provision (first run) + deploy all sites | | `cloud deploy:rollback [site] [--to ]` | Roll a site back to a previous release | | `cloud deploy:history [site] [--limit ]` | Show on-box deployment history | | `cloud deploy:recipe