frankenphp icon indicating copy to clipboard operation
frankenphp copied to clipboard

Modify Caddyfile for Laravel Octane

Open jduan00 opened this issue 1 year ago • 4 comments

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
    }
}
  1. 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

jduan00 avatar Jul 15 '24 06:07 jduan00

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.

withinboredom avatar Jul 15 '24 08:07 withinboredom

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

jduan00 avatar Jul 16 '24 04:07 jduan00

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

The backend.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
		}
	}
}

AlliBalliBaba avatar Jul 26 '24 08:07 AlliBalliBaba

Has anyone managed to modify the Caddyfile when using FrankenPHP with Octane?

I tried to make changes, but when I pass the Caddyfile using the --caddyfile=path/Caddyfile parameter, it starts throwing certificate errors. I even tried setting a fixed domain, but the error persists.

If anyone can help, I’d really appreciate it.

vresende avatar May 23 '25 13:05 vresende