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 LiteBucket S3/R2 SQLite.
1. Choosing Your Production Architecture
| Platform | Recommended Storage | Config Template | Notes |
|---|---|---|---|
| Single VPS / Hetzner / Kamal | Local SQLite on NVMe + litebucket replicate or Litestream 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 / Managed Postgres (Schema-per-Tenant) | PostgreSQL (default-features = false, features = ["postgres"]) | postgres://user:pass@host/myapp?options=-c%20search_path%3D%22tenant_{tenant}%22 | One Postgres server & one DB; no CREATEDB needed (or .../tenant_{tenant} for logical DB-per-tenant on one server) |
| Laravel Cloud / Ephemeral Containers (SQLite) | LiteBucket S3/R2 SQLite VFS | laravel-litebucket (t.litebucket()) | 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. Laravel Cloud Auto-Setup with LiteBucket (Zero Postgres, Zero Persistent Disk)
If you deploy on Laravel Cloud and want database-per-tenant SQLite without paying for a Postgres cluster or mounting a disk:
- Attach a Laravel Cloud Object Storage (R2) bucket to your environment. Laravel Cloud automatically injects
FILESYSTEM_ENDPOINT,FILESYSTEM_BUCKET,FILESYSTEM_ACCESS_KEY_ID,FILESYSTEM_SECRET_ACCESS_KEY, andFILESYSTEM_REGION.laravel-litebucketdiscovers all of them automatically — zero S3 credentials to copy-paste. - Set two environment variables in Laravel Cloud:
.env LITEBUCKET_VFS=1 LITEBUCKET_ENCRYPTION_KEY=pass:your-strong-secret-passphrase - Wire
.with_litebucket()+t.litebucket_from_env():Rust use laravel_litebucket::prelude::*; use laravel_tenanted::WithTenancy; Application::configure() .with_litebucket() .with_migrations(migrations()) .with_tenancy_using(|t| { t.litebucket_from_env() .central_connection("central") .auto_create(false); }) - Use strict durability for central auth/org tables in
src/config/database.rs:Rust let central_url = if std::env::var("LITEBUCKET_VFS").as_deref() == Ok("1") { laravel_litebucket::database_url_strict("central/main") } else { "sqlite://storage/database/central.sqlite3?mode=rwc".to_owned() };
When Laravel Cloud stops, hibernates, or redeploys your container, the fresh cold pod boots in ~1 second from zero local disk and hydrates both central/main and every tenants/<slug>/main database directly from your R2 bucket with per-tenant HKDF AES-256-GCM encryption. See the Configuration Reference and litebucket.dev for full details.
5. 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-saas6. 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.litebucket 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/litebucket shred).
- For local SQLite, back up