Documentation Frameworks and migration

Deploy a Laravel app

FrankenPHP from a Dockerfile on $PORT, APP_KEY, PostgreSQL, migrations, queues and scheduled tasks.

~5 min read
On this page

Gregale builds Node, Python and Go from source, but not PHP. A Laravel app deploys from a Dockerfile. The one on this page runs it on FrankenPHP, a PHP application server built on Caddy, so one process serves both PHP and the files in public/, with no separate nginx or PHP-FPM. Symfony and other PHP frameworks deploy the same way. What every framework shares is on Deploy a web framework.

Prepare the app

Laravel needs no code changes to listen on the right port: the web server does that. Two settings matter:

  • Health check. Laravel 11 and later answer /up without touching the database. Point Gregale at it:

    yaml
    # gregale.yaml
    hosting:
      health: /up
    
  • Logs. Send them to standard error so gregale logs shows them: LOG_CHANNEL=stderr, which the Dockerfile below sets.

Caddyfile and Dockerfile

Add a Caddyfile at the project root. Gregale sets PORT; 8080 is the fallback for local runs:

text
{
	frankenphp
	admin off
	auto_https off
}

:{$PORT:8080} {
	root * /app/public
	encode zstd gzip
	php_server
}

auto_https off is right here: Gregale terminates TLS at its edge and forwards plain HTTP to the app.

Then a Dockerfile:

dockerfile
FROM dunglas/frankenphp:1-php8.5-alpine

RUN install-php-extensions pdo_pgsql opcache zip
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Run as an unprivileged user that owns Caddy's state and Laravel's writable
# directories. Gregale runs Dockerfile apps as uid 1000 when USER is unset.
RUN adduser -D -u 1000 app \
    && mkdir -p /app \
    && chown -R app:app /app /data/caddy /config/caddy
USER app

WORKDIR /app
COPY --chown=app:app composer.json composer.lock ./
RUN composer install --no-dev --no-scripts --no-autoloader --prefer-dist --no-interaction

COPY --chown=app:app . .
RUN composer dump-autoload --optimize --no-dev \
    && php artisan package:discover \
    && php artisan view:cache \
    && php artisan event:cache

ENV APP_ENV=production \
    APP_DEBUG=false \
    DB_CONNECTION=pgsql \
    LOG_CHANNEL=stderr

# The base image checks Caddy's admin API, which the Caddyfile turns off.
# Gregale checks /up instead (hosting.health in gregale.yaml).
HEALTHCHECK NONE
EXPOSE 8080
CMD ["frankenphp", "run", "--config", "/app/Caddyfile"]
  • Alpine keeps it small. This image unpacks to about 290 MB, which fits the Hobby plan's app filesystem. The Debian-based dunglas/frankenphp:1-php8.5 image works the same way but unpacks to about 670 MB, which needs the Pro plan or higher; see Plans and pricing.
  • The user. Gregale runs an app built from a Dockerfile as uid 1000 when the image sets no USER. Laravel writes to storage/ and bootstrap/cache/, and Caddy to /data/caddy and /config/caddy, so the image creates that user and gives it those directories. Without this, pages fail with touch(): Utime failed: Operation not permitted and Caddy logs permission denied.
  • HEALTHCHECK NONE. The FrankenPHP image declares a health check against Caddy's admin API, which the Caddyfile turns off. Gregale waits for an image's HEALTHCHECK before serving, so without this line the deployment fails with app_startup_timeout.
  • zip. Composer needs the zip extension (or unzip) to install packages from their archives.
  • Commit composer.lock. If you develop on a newer PHP than the image runs, set config.platform.php in composer.json (for example "8.4.0") so the lock file only picks packages the image can run.
  • Add a .dockerignore with vendor/, node_modules/, .env and storage/logs/*, so local files don't end up in the image.
  • Don't run php artisan config:cache in the Dockerfile: it would bake in the build's environment, and Gregale passes configuration and secrets when the app starts.
  • Each build gets 2 vCPU, 2048 MB of memory and 15 minutes; see Builds and framework detection.

Set APP_KEY

Laravel encrypts cookies and sessions with APP_KEY, so the app needs one before it serves pages. Reserve the app, then set the key as a secret:

bash
gregale deploy --create-only --name my-app
gregale env set --app my-app APP_KEY="base64:$(openssl rand -base64 32)"

Never commit the key or put it in the Dockerfile. See Secrets.

Deploy

bash
gregale deploy --dry-run
gregale deploy --name my-app

If the deployment fails:

bash
gregale app health my-app --errors
gregale logs my-app

Add PostgreSQL

bash
gregale add postgres --app my-app

The connection URL arrives as DATABASE_URL. Laravel reads a database URL from the connection's url key; point the pgsql connection in config/database.php at it:

php
'pgsql' => [
    'driver' => 'pgsql',
    'url' => env('MIGRATION_DATABASE_URL', env('DATABASE_URL', env('DB_URL'))),
    // ...the rest of the defaults
],

DB_CONNECTION=pgsql (set in the Dockerfile) makes it the default connection. DATABASE_URL goes through a connection pooler in transaction mode; see Managed PostgreSQL.

With the default .env settings, sessions, the cache and queued jobs use the database too. That works, but it means a page view queries PostgreSQL.

Run migrations as a release command

The serving app's credential can't change the schema. Give the release command its own credential:

bash
gregale postgres attach my-app-postgres my-app --access migration --env MIGRATION_DATABASE_URL

Only the release command receives MIGRATION_DATABASE_URL, and the url line above prefers it when it is set. Then run migrations before each new version takes traffic:

yaml
# gregale.yaml
release:
  command: php artisan migrate --force

If a migration fails, the deploy stops and the previous version keeps serving.

Things to know

  • Queues and the scheduler. php artisan queue:work and schedule:work are long-running processes that stop while the app is parked. Run scheduled commands as a cron and background work as a job or a worker.
  • Files. The filesystem is ephemeral. Store uploads in object storage with Laravel's s3 disk, not in storage/app.
  • Octane. FrankenPHP can also run Laravel Octane in worker mode, which keeps the app booted between requests. The setup on this page boots Laravel per request, which is simpler and needs no changes to your code.

Example repository

gregale-dev/laravel-postgres is the app on this page: Laravel 13 on FrankenPHP with a small notes API, migrations as the release command and the settings above. Its gregale.yaml creates the database; set APP_KEY first, then deploy:

bash
git clone https://github.com/gregale-dev/laravel-postgres
cd laravel-postgres
gregale deploy --create-only --name my-laravel-app
gregale env set --app my-laravel-app APP_KEY="base64:$(openssl rand -base64 32)"
gregale deploy --name my-laravel-app

Where to host a Laravel app with a queue worker and a database compares how Laravel Cloud, Forge, Railway, Fly.io, Render, Vercel and Gregale run the app, its queue worker and scheduler, and what each costs.

Spotted a problem on this page? Tell us