From ba28e1dbf8c977fe8da540ba5f79bee45f39eff3 Mon Sep 17 00:00:00 2001 From: Exynox Date: Tue, 31 Dec 2024 20:37:45 +0200 Subject: [PATCH] Added Octane/FrankenPHP support for prod Docker image --- .gitignore | 4 + composer.json | 7 +- composer.lock | 263 +++++++++++++++++- config/octane.php | 224 +++++++++++++++ .../prod/{Dockerfile => apache.Dockerfile} | 4 +- docker/image/prod/frankenphp.Dockerfile | 36 +++ docker/{init.sh => init-apache.sh} | 0 docker/init-frankenphp.sh | 50 ++++ 8 files changed, 582 insertions(+), 6 deletions(-) create mode 100644 config/octane.php rename docker/image/prod/{Dockerfile => apache.Dockerfile} (96%) create mode 100644 docker/image/prod/frankenphp.Dockerfile rename docker/{init.sh => init-apache.sh} (100%) create mode 100644 docker/init-frankenphp.sh diff --git a/.gitignore b/.gitignore index 3356ef0..33e4f23 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,7 @@ yarn-error.log /.fleet /.idea /.vscode + +**/caddy +frankenphp +frankenphp-worker.php diff --git a/composer.json b/composer.json index e148c9a..ba126c0 100644 --- a/composer.json +++ b/composer.json @@ -6,12 +6,13 @@ "license": "MIT", "require": { "php": "^8.2", + "ext-gd": "*", + "ext-simplexml": "*", "guzzlehttp/guzzle": "^7.2", "laravel/framework": "^11.31", + "laravel/octane": "^2.6", "laravel/sanctum": "^4.0", - "laravel/tinker": "^2.8", - "ext-gd": "*", - "ext-simplexml": "*" + "laravel/tinker": "^2.8" }, "require-dev": { "fakerphp/faker": "^1.9.1", diff --git a/composer.lock b/composer.lock index fcfe4e5..59c8e57 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a560395d060e8def1be287fc4798d9b3", + "content-hash": "304103575d8c6cd1c07a2d3f5a226f77", "packages": [ { "name": "brick/math", @@ -1054,6 +1054,94 @@ ], "time": "2023-12-03T19:50:20+00:00" }, + { + "name": "laminas/laminas-diactoros", + "version": "3.5.0", + "source": { + "type": "git", + "url": "https://github.com/laminas/laminas-diactoros.git", + "reference": "143a16306602ce56b8b092a7914fef03c37f9ed2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laminas/laminas-diactoros/zipball/143a16306602ce56b8b092a7914fef03c37f9ed2", + "reference": "143a16306602ce56b8b092a7914fef03c37f9ed2", + "shasum": "" + }, + "require": { + "php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0", + "psr/http-factory": "^1.1", + "psr/http-message": "^1.1 || ^2.0" + }, + "conflict": { + "amphp/amp": "<2.6.4" + }, + "provide": { + "psr/http-factory-implementation": "^1.0", + "psr/http-message-implementation": "^1.1 || ^2.0" + }, + "require-dev": { + "ext-curl": "*", + "ext-dom": "*", + "ext-gd": "*", + "ext-libxml": "*", + "http-interop/http-factory-tests": "^2.2.0", + "laminas/laminas-coding-standard": "~2.5.0", + "php-http/psr7-integration-tests": "^1.4.0", + "phpunit/phpunit": "^10.5.36", + "psalm/plugin-phpunit": "^0.19.0", + "vimeo/psalm": "^5.26.1" + }, + "type": "library", + "extra": { + "laminas": { + "module": "Laminas\\Diactoros", + "config-provider": "Laminas\\Diactoros\\ConfigProvider" + } + }, + "autoload": { + "files": [ + "src/functions/create_uploaded_file.php", + "src/functions/marshal_headers_from_sapi.php", + "src/functions/marshal_method_from_sapi.php", + "src/functions/marshal_protocol_version_from_sapi.php", + "src/functions/normalize_server.php", + "src/functions/normalize_uploaded_files.php", + "src/functions/parse_cookie_header.php" + ], + "psr-4": { + "Laminas\\Diactoros\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "PSR HTTP Message implementations", + "homepage": "https://laminas.dev", + "keywords": [ + "http", + "laminas", + "psr", + "psr-17", + "psr-7" + ], + "support": { + "chat": "https://laminas.dev/chat", + "docs": "https://docs.laminas.dev/laminas-diactoros/", + "forum": "https://discourse.laminas.dev", + "issues": "https://github.com/laminas/laminas-diactoros/issues", + "rss": "https://github.com/laminas/laminas-diactoros/releases.atom", + "source": "https://github.com/laminas/laminas-diactoros" + }, + "funding": [ + { + "url": "https://funding.communitybridge.org/projects/laminas-project", + "type": "community_bridge" + } + ], + "time": "2024-10-14T11:59:49+00:00" + }, { "name": "laravel/framework", "version": "v11.34.2", @@ -1267,6 +1355,96 @@ }, "time": "2024-11-27T15:43:57+00:00" }, + { + "name": "laravel/octane", + "version": "v2.6.0", + "source": { + "type": "git", + "url": "https://github.com/laravel/octane.git", + "reference": "b8b11ef25600baa835d364e724f2e948dc1eb88b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/octane/zipball/b8b11ef25600baa835d364e724f2e948dc1eb88b", + "reference": "b8b11ef25600baa835d364e724f2e948dc1eb88b", + "shasum": "" + }, + "require": { + "laminas/laminas-diactoros": "^3.0", + "laravel/framework": "^10.10.1|^11.0", + "laravel/prompts": "^0.1.24|^0.2.0|^0.3.0", + "laravel/serializable-closure": "^1.3|^2.0", + "nesbot/carbon": "^2.66.0|^3.0", + "php": "^8.1.0", + "symfony/console": "^6.0|^7.0", + "symfony/psr-http-message-bridge": "^2.2.0|^6.4|^7.0" + }, + "conflict": { + "spiral/roadrunner": "<2023.1.0", + "spiral/roadrunner-cli": "<2.6.0", + "spiral/roadrunner-http": "<3.3.0" + }, + "require-dev": { + "guzzlehttp/guzzle": "^7.6.1", + "inertiajs/inertia-laravel": "^0.6.9|^1.0", + "laravel/scout": "^10.2.1", + "laravel/socialite": "^5.6.1", + "livewire/livewire": "^2.12.3|^3.0", + "mockery/mockery": "^1.5.1", + "nunomaduro/collision": "^6.4.0|^7.5.2|^8.0", + "orchestra/testbench": "^8.21|^9.0", + "phpstan/phpstan": "^1.10.15", + "phpunit/phpunit": "^10.4", + "spiral/roadrunner-cli": "^2.6.0", + "spiral/roadrunner-http": "^3.3.0" + }, + "bin": [ + "bin/roadrunner-worker", + "bin/swoole-server" + ], + "type": "library", + "extra": { + "laravel": { + "aliases": { + "Octane": "Laravel\\Octane\\Facades\\Octane" + }, + "providers": [ + "Laravel\\Octane\\OctaneServiceProvider" + ] + }, + "branch-alias": { + "dev-master": "2.x-dev" + } + }, + "autoload": { + "psr-4": { + "Laravel\\Octane\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Supercharge your Laravel application's performance.", + "keywords": [ + "frankenphp", + "laravel", + "octane", + "roadrunner", + "swoole" + ], + "support": { + "issues": "https://github.com/laravel/octane/issues", + "source": "https://github.com/laravel/octane" + }, + "time": "2024-11-25T21:47:18+00:00" + }, { "name": "laravel/prompts", "version": "v0.3.2", @@ -4834,6 +5012,89 @@ ], "time": "2024-11-06T14:24:19+00:00" }, + { + "name": "symfony/psr-http-message-bridge", + "version": "v7.2.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/psr-http-message-bridge.git", + "reference": "03f2f72319e7acaf2a9f6fcbe30ef17eec51594f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/psr-http-message-bridge/zipball/03f2f72319e7acaf2a9f6fcbe30ef17eec51594f", + "reference": "03f2f72319e7acaf2a9f6fcbe30ef17eec51594f", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "psr/http-message": "^1.0|^2.0", + "symfony/http-foundation": "^6.4|^7.0" + }, + "conflict": { + "php-http/discovery": "<1.15", + "symfony/http-kernel": "<6.4" + }, + "require-dev": { + "nyholm/psr7": "^1.1", + "php-http/discovery": "^1.15", + "psr/log": "^1.1.4|^2|^3", + "symfony/browser-kit": "^6.4|^7.0", + "symfony/config": "^6.4|^7.0", + "symfony/event-dispatcher": "^6.4|^7.0", + "symfony/framework-bundle": "^6.4|^7.0", + "symfony/http-kernel": "^6.4|^7.0" + }, + "type": "symfony-bridge", + "autoload": { + "psr-4": { + "Symfony\\Bridge\\PsrHttpMessage\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "PSR HTTP message bridge", + "homepage": "https://symfony.com", + "keywords": [ + "http", + "http-message", + "psr-17", + "psr-7" + ], + "support": { + "source": "https://github.com/symfony/psr-http-message-bridge/tree/v7.2.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-26T08:57:56+00:00" + }, { "name": "symfony/routing", "version": "v7.2.0", diff --git a/config/octane.php b/config/octane.php new file mode 100644 index 0000000..8cfba01 --- /dev/null +++ b/config/octane.php @@ -0,0 +1,224 @@ + env('OCTANE_SERVER', 'roadrunner'), + + /* + |-------------------------------------------------------------------------- + | Force HTTPS + |-------------------------------------------------------------------------- + | + | When this configuration value is set to "true", Octane will inform the + | framework that all absolute links must be generated using the HTTPS + | protocol. Otherwise your links may be generated using plain HTTP. + | + */ + + 'https' => env('OCTANE_HTTPS', false), + + /* + |-------------------------------------------------------------------------- + | Octane Listeners + |-------------------------------------------------------------------------- + | + | All of the event listeners for Octane's events are defined below. These + | listeners are responsible for resetting your application's state for + | the next request. You may even add your own listeners to the list. + | + */ + + 'listeners' => [ + WorkerStarting::class => [ + EnsureUploadedFilesAreValid::class, + EnsureUploadedFilesCanBeMoved::class, + ], + + RequestReceived::class => [ + ...Octane::prepareApplicationForNextOperation(), + ...Octane::prepareApplicationForNextRequest(), + // + ], + + RequestHandled::class => [ + // + ], + + RequestTerminated::class => [ + // FlushUploadedFiles::class, + ], + + TaskReceived::class => [ + ...Octane::prepareApplicationForNextOperation(), + // + ], + + TaskTerminated::class => [ + // + ], + + TickReceived::class => [ + ...Octane::prepareApplicationForNextOperation(), + // + ], + + TickTerminated::class => [ + // + ], + + OperationTerminated::class => [ + FlushOnce::class, + FlushTemporaryContainerInstances::class, + // DisconnectFromDatabases::class, + // CollectGarbage::class, + ], + + WorkerErrorOccurred::class => [ + ReportException::class, + StopWorkerIfNecessary::class, + ], + + WorkerStopping::class => [ + CloseMonologHandlers::class, + ], + ], + + /* + |-------------------------------------------------------------------------- + | Warm / Flush Bindings + |-------------------------------------------------------------------------- + | + | The bindings listed below will either be pre-warmed when a worker boots + | or they will be flushed before every new request. Flushing a binding + | will force the container to resolve that binding again when asked. + | + */ + + 'warm' => [ + ...Octane::defaultServicesToWarm(), + ], + + 'flush' => [ + // + ], + + /* + |-------------------------------------------------------------------------- + | Octane Swoole Tables + |-------------------------------------------------------------------------- + | + | While using Swoole, you may define additional tables as required by the + | application. These tables can be used to store data that needs to be + | quickly accessed by other workers on the particular Swoole server. + | + */ + + 'tables' => [ + 'example:1000' => [ + 'name' => 'string:1000', + 'votes' => 'int', + ], + ], + + /* + |-------------------------------------------------------------------------- + | Octane Swoole Cache Table + |-------------------------------------------------------------------------- + | + | While using Swoole, you may leverage the Octane cache, which is powered + | by a Swoole table. You may set the maximum number of rows as well as + | the number of bytes per row using the configuration options below. + | + */ + + 'cache' => [ + 'rows' => 1000, + 'bytes' => 10000, + ], + + /* + |-------------------------------------------------------------------------- + | File Watching + |-------------------------------------------------------------------------- + | + | The following list of files and directories will be watched when using + | the --watch option offered by Octane. If any of the directories and + | files are changed, Octane will automatically reload your workers. + | + */ + + 'watch' => [ + 'app', + 'bootstrap', + 'config/**/*.php', + 'database/**/*.php', + 'public/**/*.php', + 'resources/**/*.php', + 'routes', + 'composer.lock', + '.env', + ], + + /* + |-------------------------------------------------------------------------- + | Garbage Collection Threshold + |-------------------------------------------------------------------------- + | + | When executing long-lived PHP scripts such as Octane, memory can build + | up before being cleared by PHP. You can force Octane to run garbage + | collection if your application consumes this amount of megabytes. + | + */ + + 'garbage' => 50, + + /* + |-------------------------------------------------------------------------- + | Maximum Execution Time + |-------------------------------------------------------------------------- + | + | The following setting configures the maximum execution time for requests + | being handled by Octane. You may set this value to 0 to indicate that + | there isn't a specific time limit on Octane request execution time. + | + */ + + 'max_execution_time' => 30, + +]; diff --git a/docker/image/prod/Dockerfile b/docker/image/prod/apache.Dockerfile similarity index 96% rename from docker/image/prod/Dockerfile rename to docker/image/prod/apache.Dockerfile index 8281740..1e4936e 100644 --- a/docker/image/prod/Dockerfile +++ b/docker/image/prod/apache.Dockerfile @@ -64,11 +64,11 @@ RUN composer install --no-ansi --no-interaction --no-plugins --no-progress --no- RUN npm ci && npm run build # Make the init script executable -RUN chmod +x /app/docker/init.sh +RUN chmod +x /app/docker/init-apache.sh # Expose the API on port 80 EXPOSE 80 # Run supervisord for handling the container services ENTRYPOINT ["/bin/sh", "-c"] -CMD ["/app/docker/init.sh"] +CMD ["/app/docker/init-apache.sh"] diff --git a/docker/image/prod/frankenphp.Dockerfile b/docker/image/prod/frankenphp.Dockerfile new file mode 100644 index 0000000..19c6408 --- /dev/null +++ b/docker/image/prod/frankenphp.Dockerfile @@ -0,0 +1,36 @@ +# syntax=docker/dockerfile:1 +FROM dunglas/frankenphp:php8.2-alpine + +# +# Install system packages & dependencies +# + +RUN apk update && apk add wget npm + +# Composer +RUN wget -O composer-setup.php https://getcomposer.org/installer \ + && php composer-setup.php --install-dir=/usr/local/bin --filename=composer \ + && rm ./composer-setup.php + +# +# PHP extensions +# +RUN install-php-extensions pcntl gd zip intl exif mysqli pdo pdo_mysql + +# Copy the source code +WORKDIR /app +COPY . /app + +# Install the dependencies +RUN composer install --no-ansi --no-interaction --no-plugins --no-progress --no-scripts --optimize-autoloader +RUN npm ci && npm run build + +# Make the init script executable +RUN chmod +x /app/docker/init-frankenphp.sh + +# Expose the API on port 80 +EXPOSE 80 + +# Run supervisord for handling the container services +ENTRYPOINT ["/bin/sh", "-c"] +CMD ["/app/docker/init-frankenphp.sh"] diff --git a/docker/init.sh b/docker/init-apache.sh similarity index 100% rename from docker/init.sh rename to docker/init-apache.sh diff --git a/docker/init-frankenphp.sh b/docker/init-frankenphp.sh new file mode 100644 index 0000000..fed92d0 --- /dev/null +++ b/docker/init-frankenphp.sh @@ -0,0 +1,50 @@ +#!/bin/sh +set -e + +KERNEL_VERSION=$(uname -r) +KERNEL_ARCH=$(uname -m) +FRANKENPHP_VERSION=$(frankenphp -v | cut -d " " -f 2) +PHP_VERSION=$(php -r "echo PHP_VERSION;") + +echo "The Old Metin2 Project - Web management system" +echo "Kernel ${KERNEL_VERSION}, architecture: ${KERNEL_ARCH}, FrankenPHP: ${FRANKENPHP_VERSION}, PHP: ${PHP_VERSION}" + +# Create storage directories if they don't exist +if [ ! -d /app/storage/app/public/ ]; then + mkdir -p /app/storage/app/public/; +fi +if [ ! -d /app/storage/app/public/patch-data/ ]; then + mkdir -p /app/storage/app/public/patch-data/; +fi + +if [ ! -d /app/storage/framework/cache/data/ ]; then + mkdir -p /app/storage/framework/cache/data/; +fi + +if [ ! -d /app/storage/framework/sessions/ ]; then + mkdir -p /app/storage/framework/sessions/; +fi + +if [ ! -d /app/storage/framework/testing/ ]; then + mkdir -p /app/storage/framework/sessions/; +fi + +if [ ! -d /app/storage/framework/views/ ]; then + mkdir -p /app/storage/framework/views/; +fi + +if [ ! -d /app/storage/logs/ ]; then + mkdir -p /app/storage/logs/; +fi + +# Set folder permissions +chown -R www-data:www-data /app/storage + +# Link filesystem paths +/usr/local/bin/php artisan storage:link + +# Run database migrations +/usr/local/bin/php artisan migrate --force --no-interaction + +# Run Laravel Octane with FrankenPHP +/usr/local/bin/php artisan octane:frankenphp --port=80