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_ROOTlaissé à 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· internehttp://localhost:9095/health
yiisoft/app déclare "php": "8.2 - 8.5", l'image PHP 8.5 par défaut convient donc.
Structure du projet
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/.envDockerfile et Compose
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"]services:
app:
build:
context: ./src
target: dev
image: yii3-oxphp/app:dev
container_name: yii3-oxphp
ports:
- "8094:80"
- "9095:9090"
volumes:
- ./src:/var/www/html
environment:
# LISTEN_ADDR (0.0.0.0:80) and DOCUMENT_ROOT (/var/www/html/public) are
# the OxPHP defaults, so both are omitted.
ENTRY_FILE: index.php # framework mode
INTERNAL_ADDR: 0.0.0.0:9090
ACCESS_LOG: all
restart: unless-stopped
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://0.0.0.0:9090/health"]
interval: 10s
timeout: 3s
retries: 5
start_period: 5sLe 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
docker compose up -d --build
docker compose exec app php yii # the Yii consoleNotes OxPHP
- Aucune base de données, aucun service supplémentaire. Le déploiement OxPHP le plus minimal possible : une image, un processus.
yiisoft/assetspublie les fichiers statiques danspublic/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
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 # 200Voir aussi
- Routage · Guide Docker
- Laravel et Symfony, les autres recettes en mode framework