こんにちは、MLBお兄さんこと松村です。
前回の記事で30-60 (30HR&60盗塁)を記録したと紹介したRonald Acuña Jr 選手ですが、なんと40-70 (40HR&70盗塁)を達成しました。本当に素晴らしい成績です。
2023年9月10日に CakePHP 5 がリリースされました。約4年ぶりのメジャーバージョンアップとなります。
CakePHP は好きなフレームワークの1つでもあり、これを Azure Web App にデプロイする手順を整理したので記事にまとめます。
サンプルアプリの準備
まずはデプロイするための CakePHP アプリケーションを準備します。
今回は WSL2 + Ubuntu + anyenv + phpenv の環境で準備を行います。(環境のセットアップ手順は割愛します)
PHP のインストール
今回は PHP 8.2.10 を使用します。
$ phpenv install 8.2.10 [Info]: Loaded extension plugin [Info]: Loaded apc Plugin. [Info]: Loaded composer Plugin. [Info]: Loaded github Plugin. [Info]: Loaded uprofiler Plugin. [Info]: Loaded xdebug Plugin. [Info]: Loaded xhprof Plugin. [Info]: Loaded zendopcache Plugin. [Info]: php.ini-production gets used as php.ini [Info]: Building 8.2.10 into /home/yuta/.anyenv/envs/phpenv/versions/8.2.10 [Downloading]: https://www.php.net/distributions/php-8.2.10.tar.bz2 [Preparing]: /tmp/php-build/source/8.2.10 [Compiling]: /tmp/php-build/source/8.2.10 [xdebug]: Installing version 3.2.2 [Downloading]: http://xdebug.org/files/xdebug-3.2.2.tgz [xdebug]: Compiling xdebug in /tmp/php-build/source/xdebug-3.2.2 [xdebug]: Installing xdebug configuration in /home/yuta/.anyenv/envs/phpenv/versions/8.2.10/etc/conf.d/xdebug.ini [xdebug]: Cleaning up. Makefile:245: warning: overriding recipe for target 'test' Makefile:136: warning: ignoring old recipe for target 'test' [Info]: Enabling Opcache... [Info]: Done [Info]: The Log File is not empty, but the Build did not fail. Maybe just warnings got logged. You can review the log in /tmp/php-build.8.2.10.20230922094103.log or rebuild with '--verbose' option [Success]: Built 8.2.10 successfully.
CakePHP 5アプリの作成
CakePHP のクイックスタートガイドに沿って、composer でアプリケーションを作成します。
$ composer self-update && composer create-project --prefer-dist cakephp/app:5.* cms Download composer.phar ... All settings correct for using Composer Downloading... Composer (version 2.6.3) successfully installed to: /tmp/composer.phar Use it: php /tmp/composer.phar Move composer.phar to /home/yuta/.anyenv/envs/phpenv/versions/8.2.10/composer You are already using the latest available Composer version 2.6.3 (stable channel). Creating a "cakephp/app:5.*" project at "./cms" Info from https://repo.packagist.org: #StandWithUkraine Installing cakephp/app (5.0.0) As there is no 'unzip' nor '7z' command installed zip files are being unpacked using the PHP zip extension. This may cause invalid reports of corrupted archives. Besides, any UNIX permissions (e.g. executable) defined in the archives will be lost. Installing 'unzip' or '7z' (21.01+) may remediate them. - Downloading cakephp/app (5.0.0) - Installing cakephp/app (5.0.0): Extracting archive Created project in /home/yuta/source/php/cms Loading composer repositories with package information Updating dependencies Your requirements could not be resolved to an installable set of packages. Problem 1 - cakephp/migrations 4.0.0 requires robmorgan/phinx 1.x-dev -> found robmorgan/phinx[dev-improve-create-tests-v2, v0.1.2, ..., 0.x-dev] but it does not match the constraint. - cakephp/migrations 4.0.1 requires robmorgan/phinx ^2.0 -> found robmorgan/phinx[dev-improve-create-tests-v2, v0.1.2, ..., 0.x-dev] but it does not match the constraint. - Root composer.json requires cakephp/migrations ^4.0.0 -> satisfiable by cakephp/migrations[4.0.0, 4.0.1].
まずはローカルで動かします。
$ cd cms $ chmod +x bin/cake $ bin/cake server
Warning: You should set
zend.assertions
to1
in your php.ini for your development environment. in /home/yuta/source/php/cms/vendor/cakephp/cakephp/src/Core/Configure.php on line 102
画面を確認すると zend.assertions
に関しての警告が出ていました。
php.ini を編集し zend.assertions=1
を設定し、アプリケーションを再起動します。。
[Assertion] ;zend.assertions = -1 **初期設定はこちら** zend.assertions = 1
無事に CakePHP を実行することができました。
Dockerfile の作成
続いて Azure Web App にデプロイする準備を行います。今回は Web App for Containers にデプロイするので、アプリケーションと同じディレクトリに下記の Dockerfile を準備します。
# ベースイメージの指定 FROM php:8.2.10-apache-bullseye # 必要なパッケージのインストール RUN apt-get update && \ apt-get install -y \ libicu-dev \ libpng-dev \ libzip-dev \ zip \ unzip \ git \ && \ rm -rf /var/lib/apt/lists/* # PHP拡張のインストール RUN docker-php-ext-install \ intl \ pdo_mysql \ gd \ zip # Composerのインストール COPY --from=composer:latest /usr/bin/composer /usr/bin/composer # Apacheの設定 RUN a2enmod rewrite # アプリケーションのコードをコンテナにコピー COPY . /var/www/html # Composerで依存関係をインストール RUN composer install --no-interaction --no-plugins # tmpディレクトリとlogsディレクトリのパーミッションを設定 RUN chmod -R 777 /var/www/html/tmp RUN chmod -R 777 /var/www/html/logs # コンテナのポートを公開 EXPOSE 80 # Apacheを起動 CMD ["apache2-foreground"]
ローカルでコンテナーを実行できることを確認しましょう。
$ docker build -t cakephp5 . $ docker run --rm -p 80:80 cakephp5
GitHub Actionsのワークフローを準備する
続いて Web App for Containers にデプロイするための、GitHub Actions のワークフローを準備します。
今回はリポジトリ (ghcr.io) にコンテナーイメージを登録します。ワークフローの内容は下記のようになります。
name: PHP CakePHP5 Container on: push: branches: [ main ] workflow_dispatch: env: REGISTRY: ghcr.io IMAGE_NAME: ${{ github.repository }}-cakephp5 permissions: id-token: write contents: read packages: write jobs: build-cakephp5: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Log in to the Container registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Extract metadata (tags, labels) for Docker id: meta uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - name: Build and push Docker image uses: docker/build-push-action@v5 with: context: . push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - name: Az CLI Login uses: azure/login@v1 with: client-id: ${{ vars.AZURE_CLIENT_ID }} tenant-id: ${{ vars.AZURE_TENANT_ID }} subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }} - name: 'Deploy to Azure Web App' id: deploy-to-webapp uses: azure/webapps-deploy@v2 with: app-name: ${{ vars.AZURE_WEBAPP_CONTAINER_NAME }} slot-name: 'Production' images: ${{ steps.meta.outputs.tags }} - name: Az CLI Logout run: az logout
このワークフローを実行すると Web App for Containers へのデプロイが行われます。
サイトにアクセスすると、無事にデプロイできたことが分かります。
今回の記事で解説した内容をまとめたリポジトリはこちらです。参考にしてください。
PHP のフレームワークは、近年は Laravel 人気が高まっていますが、個人的には CakePHP も好きなので機会があれば使っていきたいですね。