dunglas / frankenphp

🧟 The modern PHP app server
https://frankenphp.dev
MIT License
6.8k stars 233 forks source link

Modify Caddyfile for Laravel Octane #923

Open jduan00 opened 3 months ago

jduan00 commented 3 months ago

Describe you feature request

Hi there,

I am trying to tweak the Caddyfile for Laravel Octane. What is the proper way to do it?

Here is what I have tried, but the server does not start. Any help will be greatly appreciated.

1) Make a copy for the Caddyfile cp <laravel path>/vendor/laravel/octane/src/Commands/stubs/Caddyfile /etc/frankenphp/Caddyfile-octane

2) Modify Caddyfile-octane

# Based on /app/www/sites/eid5/vendor/laravel/octane/src/Commands/stubs/Caddyfile

{
  {$CADDY_GLOBAL_OPTIONS}

  ## admin {$CADDY_SERVER_ADMIN_HOST}:{$CADDY_SERVER_ADMIN_PORT}
  admin off

  frankenphp {
    worker "{$APP_PUBLIC_PATH}/frankenphp-worker.php" {$CADDY_SERVER_WORKER_COUNT}
  }
}

{$CADDY_SERVER_SERVER_NAME} {
  log {
    level INFO
    output stdout
    format json

    # Redact the authorization query parameter that can be set by Mercure...
    format filter {
      wrap {$CADDY_SERVER_LOGGER}
      fields {
        uri query {
          replace authorization REDACTED
        }
      }
    }
  }

  route {
    root * "{$APP_PUBLIC_PATH}"

    ## encode zstd br gzip ## these will add 4ms latency

    # Mercure configuration is injected here...
    {$CADDY_SERVER_EXTRA_DIRECTIVES}

    php_server {
      index frankenphp-worker.php
      # Required for the public/storage/ directory...
      resolve_root_symlink
    }
  }
}

# Add docker health check
127.0.0.1:8080 {

  respond /http-ping "pong" 200

  log {
        output discard
    }
}

3) Start laravel octane + frankenphp php artisan octane:start --server=frankenphp --port=8100 --host=0.0.0.0 --caddyfile /etc/frankenphp/Caddyfile-octane

Error received:

Error: adapting config using caddyfile: server block without any key is global configuration, and if used, it must be first
withinboredom commented 3 months ago

I suspect CADDY_SERVER_SERVER_NAME is empty and it sees two global blocks. At least, that is what the error message you gave says is wrong.

jduan00 commented 3 months ago

Thanks for the pointer. It seems that using the --caddyfile option will lose CADDY_SERVER_SERVER_NAME and other variables.

AlliBalliBaba commented 2 months ago

I don't think $CADDY_SERVER_SERVER_NAME is necessarily dropped. This example configuration works for me, at least for laravel/octane v2.4.0 laravel/framework v11.10.0:

php artisan octane:start --server=frankenphp --host=0.0.0.0 --port=8000 --max-requests=10000 --caddyfile=/app/resources/caddy/backend.Caddyfile

Thebackend.Caddyfile content:

{
    {$CADDY_GLOBAL_OPTIONS}

    admin localhost:{$CADDY_SERVER_ADMIN_PORT}

    frankenphp {
        worker "{$APP_PUBLIC_PATH}/frankenphp-worker.php" {$CADDY_SERVER_WORKER_COUNT}
    }
}

{$CADDY_SERVER_SERVER_NAME} {
    log {
        level {$CADDY_SERVER_LOG_LEVEL}

        # Redact the authorization query parameter that can be set by Mercure...
        format filter {
            wrap {$CADDY_SERVER_LOGGER}
            fields {
                uri query {
                    replace authorization REDACTED
                }
            }
        }
    }

    route {
        root * "{$APP_PUBLIC_PATH}"
        encode zstd br gzip

        # Mercure configuration is injected here...
        {$CADDY_SERVER_EXTRA_DIRECTIVES}

        @static {
            file
                path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.woff2 *ttf *.svg
            }
            header @static Cache-Control max-age=5184000

        php_server {
            index frankenphp-worker.php
            # Required for the public/storage/ directory...
            # resolve_root_symlink
        }
    }
}