Guide 06 of 6
Deployment Guide
laravel-tenanted is designed to deploy cleanly across every Rust hosting topology — from a single $5 VPS running SQLite-per-tenant, to Fly.io / Kamal containers with persistent volumes, to stateless Laravel Cloud / Kubernetes pods running PostgreSQL or LiteVault S3/R2 SQLite.
1. Choosing Your Production Architecture
| Platform | Recommended Storage | Config Template | Notes |
|---|---|---|---|
| Single VPS / Hetzner / Kamal | Local SQLite on NVMe + Litestream or LiteVault Gear 1 WAL backup | sqlite:///data/tenants/{tenant}/main.sqlite3?mode=rwc | Zero DB server ops, sub-millisecond reads, mount /data volume |
| Fly.io / Render / Railway | Local SQLite on persistent volume | sqlite:///data/tenants/{tenant}/main.sqlite3?mode=rwc | Single writer region or Fly Replay for multi-region |
| Laravel Cloud / Stateless Containers | PostgreSQL (default-features = false, features = ["postgres"]) | postgres://user:pass@host/tenant_{tenant} | Horizontal scaling out of the box |
| Laravel Cloud / Ephemeral Containers (SQLite) | LiteVault S3/R2 SQLite VFS | laravel_rust_litevault::tenanted_database_url("tenants/{tenant}/main") | Zero local disk required + per-tenant HKDF AES-256-GCM encryption |
2. Environment-Driven Configuration
.with_tenancy() and .with_tenancy_using(...) read your environment variables automatically — no manual std::env::var(...) parsing required — so local dev, staging, and production run the exact same binary:
APP_ENV=production
APP_URL=https://myapp.com
TENANT_BASE_DOMAIN=myapp.com
TENANT_DATABASE_URL=sqlite:///data/tenants/{tenant}/main.sqlite3?mode=rwc
TENANT_MAX_OPEN=64
# TENANT_AUTO_CREATE defaults to false automatically when APP_ENV=production3. Wildcard Subdomains, Reverse Proxies & TrustHosts
When serving <tenant>.myapp.com:
- DNS: Add a wildcard
A/AAAA/CNAMErecord for*.myapp.compointing to your load balancer or server. - TLS: Issue a wildcard certificate (
*.myapp.com+myapp.com) via Caddy, Cloudflare, Fly.io, or Let's Encrypt DNS-01. - Preserve the
Hostheader: Ensure your reverse proxy forwards the originalHostheader (proxy_set_header Host $host;in Nginx; automatic in Caddy and Kamal Proxy). TrustHosts:.with_tenancy()/.with_tenancy_using(|t| { t.subdomain("myapp.com"); })(andTenantedServiceProvider::resolve_from_subdomain) automatically configureslaravel-rust'sTrustHostsmiddleware to allowmyapp.comand*.myapp.com. If you pass a customTrustHostsconfig in.with_middleware(...), remember to include"*.myapp.com".
4. Docker Deployment (Multi-Stage Rust Binary + Persistent Volume)
A complete working Dockerfile and fly.toml live in examples/deployable-saas/. The core pattern:
FROM rust:1.99-bookworm AS builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y ca-certificates curl && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=builder /app/target/release/my-saas /usr/local/bin/my-saas
RUN mkdir -p /data/tenants
ENV APP_ENV=production
ENV APP_URL=https://myapp.com
ENV TENANT_BASE_DOMAIN=myapp.com
ENV TENANT_DATABASE_URL="sqlite:///data/tenants/{tenant}/main.sqlite3?mode=rwc"
EXPOSE 8000
HEALTHCHECK --interval=15s --timeout=3s CMD curl -fsS http://127.0.0.1:8000/health || exit 1
CMD ["my-saas", "serve", "--host", "0.0.0.0", "--port", "8000"]Run with a persistent volume mounted at /data:
docker build -t my-saas .
docker run -d --name my-saas \
-p 8000:8000 \
-v my-saas-data:/data \
-e TENANT_BASE_DOMAIN=myapp.com \
my-saas5. Production Checklist
- Keep fail-closed mode enabled —
.with_tenancy()enablesrequire_tenantby default (or callTenantConfig::require_tenant()if registering manually), guaranteeing no controller, job, or command can accidentally query a tenanted model outside a tenant scope. - Keep
.auto_create(false)in production —.with_tenancy()setsauto_create(false)automatically whenAPP_ENV=production, preventing random bots hittingspam1234.myapp.comfrom creating thousands of empty tenant databases on disk! Provision customers explicitly during onboarding vialaravel_tenanted::create(&slug).await?ortenant:create <slug>. - Size
max_open_tenants— Each open SQLite pool keeps a handful of file descriptors (max_connections_per_tenant) and SQLite page cache in RAM.64–256works smoothly on 512 MB–2 GB containers; raiseulimit -nif you run higher. - Run eager migrations on deploy — Execute
my-saas tenant:migrate --allas a release/pre-start hook so every tenant schema is upgraded before traffic hits. - Isolate your cache —
.with_tenancy()automatically registers the in-memory"tenanted"default cache store (tenant:{id}:*). For multi-instance deployments sharing Redis, wrap it withlaravel_tenanted::cache_store("redis")?(orTenantedCacheStore::new(redis_store)). - Backups & offboarding:
- For local SQLite, back up
/data/tenants/continuously (e.g.litevault replicate --tenants-dir /data/tenants --per-tenant-keyor Litestream) — every tenant is an independent.sqlite3file you can restore or export individually! - For GDPR / customer offboarding, run
my-saas tenant:delete <tenant> --force(or wireTenantConfig::on_deletetoDROP DATABASE/litevault shred).
- For local SQLite, back up