Yii3 sur OxPHP

Yii3 est la recette la plus légère de cette série. Le modèle yiisoft/app possède un contrôleur frontal public/index.php, ne nécessite aucune base de données et, comme la plupart du traitement des chaînes est assuré par des polyfills, ne requiert pratiquement aucune extension PHP supplémentaire. Il correspond au mode de routage framework d'OxPHP et s'exécute comme un service unique.

Vue d'ensemble de la pile

  • Image OxPHP : ghcr.io/oxphp/oxphp:0.10.0 (PHP 8.5)
  • Mode de routage : Framework (ENTRY_FILE=index.php ; DOCUMENT_ROOT laissé à sa valeur par défaut /var/www/html/public)
  • Extensions ajoutées : mbstring (native, pour la rapidité — le runtime la remplace sinon par un polyfill)
  • Services : OxPHP uniquement
  • URL : http://localhost:8094 · interne http://localhost:9095/health
Note

yiisoft/app déclare "php": "8.2 - 8.5", l'image PHP 8.5 par défaut convient donc.

Structure du projet

bash
mkdir -p yii3-oxphp/src docker run --rm -v "$PWD/yii3-oxphp/src":/app -w /app \ composer:2 create-project yiisoft/app . --prefer-dist cp yii3-oxphp/src/.env.example yii3-oxphp/src/.env

Dockerfile et Compose

src/Dockerfile
ARG OXPHP_VERSION=0.10.0 ARG PHP_VERSION=8.5 ARG ALPINE_VERSION=3.23 # ── PHP base: native mbstring ───────────────────────────────── FROM php:${PHP_VERSION}-zts-alpine${ALPINE_VERSION} AS php-base RUN apk add --no-cache oniguruma \ && apk add --no-cache --virtual .build-deps oniguruma-dev \ && docker-php-ext-install -j"$(nproc)" mbstring \ && apk del .build-deps # ── Composer ────────────────────────────────────────────────── FROM composer:2 AS composer # ── OxPHP artifacts (PHP 8.5 default image) ─────────────────── FROM ghcr.io/oxphp/oxphp:${OXPHP_VERSION} AS oxphp # ── dev: OxPHP server + PHP CLI + Composer ──────────────────── FROM php-base AS dev RUN apk add --no-cache libgcc git COPY --from=composer /usr/bin/composer /usr/local/bin/composer COPY --from=oxphp /usr/local/bin/oxphp /usr/local/bin/oxphp COPY --from=oxphp /usr/local/lib/liboxphp_bridge.so /usr/local/lib/ COPY --from=oxphp /usr/local/lib/php/extensions/ /tmp/oxphp-ext/ RUN cp /tmp/oxphp-ext/*/oxphp_sapi.so "$(php -r 'echo ini_get("extension_dir");')/" \ && rm -rf /tmp/oxphp-ext \ && echo "extension=oxphp_sapi.so" > /usr/local/etc/php/conf.d/oxphp-ext.ini RUN { \ echo "[opcache]"; echo "opcache.enable=1"; echo "opcache.enable_cli=1"; \ echo "opcache.validate_timestamps=1"; echo "opcache.revalidate_freq=0"; \ } > /usr/local/etc/php/conf.d/opcache-dev.ini RUN getent passwd www-data >/dev/null \ || adduser -D -H -u 82 -G www-data -s /sbin/nologin www-data RUN mkdir -p /var/www/html/public && chown -R www-data:www-data /var/www/html ENV LD_LIBRARY_PATH=/usr/local/lib WORKDIR /var/www/html COPY --chown=www-data:www-data . /var/www/html EXPOSE 80 9090 CMD ["oxphp"]
Note

Le modèle yiisoft/app fournit son propre .dockerignore de type liste blanche ; conservez-le. Avec un src/ monté en bind, le COPY n'est qu'un mécanisme de repli.

Installation et premier lancement

bash
docker compose up -d --build docker compose exec app php yii # the Yii console

Notes OxPHP

Note
  • Aucune base de données, aucun service supplémentaire. Le déploiement OxPHP le plus minimal possible : une image, un processus.
  • yiisoft/assets publie les fichiers statiques dans public/assets/<hash>/. De vrais fichiers sur disque, servis directement par le mode framework. Aucune acrobatie de réécriture d'URL n'est nécessaire (contrairement à certains pipelines d'assets qui intègrent un segment de version dans l'URL).

Vérification

bash
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8094/ # 200 curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8094/nonexistent # 404 (Yii router) curl -s -o /dev/null -w "%{http_code}\n" http://localhost:9095/health # 200

Voir aussi