GAE+CloudeSQL+GCSでWordPressを構築する④(GAE,GCSリソースの作成)

この記事は約16分で読めます。
前回の記事でWordPressに必要なDBの作成を行いました。
今回の記事では、
  • Composerよりgoogle/cloud-toolsをダウンロード
  • ダウンロードファイルのスクリプトからGAE版WordPressのリソースを作成
  • app.yamlのカスタマイズ
  • GAEへデプロイ
  • WordPressの動作確認
  • GCSの確認

までを行います。

完成図は下記イメージとなります。

※赤枠が今回の作成箇所

スポンサーリンク

前提条件

参考手順と作業に必要な事前準備は下記記事となっておりますので、ご覧になっていない方は参照頂けると幸いです。

GAE+CloudeSQL+GCSでWordPressを構築する①(参考手順と事前準備)
前回の記事でGAE+CloudSQLの構成でWordPressを構築する事としました。※構築の記事を投稿後にAMIMOTO AMIのWordPressを移行記事を書こうと思います。今回は参考にした手順と事前準備についてのお話となります...

また、クライアント環境はCloudShellではなく、macOSのターミナルとなっている点ご承知置き下さい。

注意事項
GAEは作成後にリージョンの変更が出来ません。
リージョンを変更するには新たにプロジェクトを作成し直す必要がありますので十分注意して下さい。
スポンサーリンク

Composerからgoogle/cloud-toolsをダウンロード

作業用ディレクトリから以下のコマンドを実行します。

composer require google/cloud-tools
スポンサーリンク

ダウンロードファイルのスクリプトからGAE版WordPressのリソースを作成

作業用ディレクトリから以下のコマンドを実行します。
php vendor/bin/wp-gae create
実行時に拡張機能に関するエラーが表示された場合はphar、zipなどのPHP拡張機能をインストールし再度スクリプトを実行して下さい。
私の環境(macOSローカル)では「Fatal error: Uncaught Error: Class ‘ZipArchive’ not found 〜」と出力されましたので、下記対応を行いました。

実行後にWordPressのダウンロードと設定ファイルの作成のスクリプトが行われます。

まずターミナルに「Please enter a directory path for the new project (defaults to my-wordpress-project)」と表示されます。

GAE版WordPressの出力ディレクトリ名をお好みのものにしたい場合はディレクトリ名を入力しEnterを押下して下さい。

未入力でEnterを押下するとmy-wordpress-projectというディレクトリが作成されます。

当手順では未入力のままEnterを押下しました。

Enter押下後いくつかターミナルで入力を求められます。

Please select the region of your Cloud SQL instance (defaults to us-central1)
  [0 ] northamerica-northeast1
  [1 ] us-central
  [2 ] us-central1
  [3 ] us-east1
  [4 ] us-east4
  [5 ] us-west1
  [6 ] us-west2
  [7 ] southamerica-east1
  [8 ] europe-north1
  [9 ] europe-west1
  [10] europe-west2
  [11] europe-west3
  [12] europe-west4
  [13] asia-east1
  [14] asia-northeast1
  [15] asia-south1
  [16] asia-southeast1
  [17] australia-southeast1

DBインスタンスはどこのリージョンの物を使うかという内容です。

前回の手順で東京リージョン(asia-northeast1)にDBインスタンスを作成しているので「14」を入力しEnterを押下します。

その後ターミナルで入力が求められる内容は次の通りとなります。

Please enter project_id (required):プロジェクトIDを入力します
Please enter db_instance (required):DBインスタンス名を入力します
Please enter db_name (required):スキーマ名を入力します
Please enter db_user (defaults to 'root'):スキーマへの操作権限があるDBユーザー名を入力します
Please enter db_password (required):上記ユーザーのパスワードを入力します
Do you want to use the same db user and password for local run? (Y/n) Yを入力します

以下入力例です。

Please enter project_id (required): test-wordpress1
Please enter db_instance (required): tutorial-sql-instance
Please enter db_name (required): tutorialdb
Please enter db_user (defaults to 'root'): tutorial-user
Please enter db_password (required):(上記ユーザーのパスワードを入力)
Do you want to use the same db user and password for local run? (Y/n)Y

スクリプトの処理が完了するとターミナルに下記内容が表示されます。

Copied necessary files with parameters.
Your WordPress project is ready at 作業用ディレクトリ/my-wordpress-project

スクリプトで作成されたWordPressディレクトリに移動します。

cd my-wordpress-project

ディレクトリの中身をみると下記リソースが作成されています。

  • WordPressのファイル・ディレクトリ群
  • app.yaml:ランタイムおよび静的アセットハンドラーを指定するApp Engine構成ファイル
  • cron.yaml:App Engine構成ファイル。wp-cron.phpを15分ごとに実行する内容
  • php.ini:WordPress固有のApp Engine PHP構成設定ファイル
  • gae-app.php:フロントコントローラー。すべてのApp Engineアプリケーションに必要
  • スクリプトで編集済のwp-config.php:前述のスクリプト実行時にターミナルで入力したDB関連の情報が反映されています。
スポンサーリンク

app.yamlのカスタマイズ

GAEにデプロイする前にapp.yamlをカスタマイズしました。

内容は下記の通りです。

# App Engine runtime configuration
runtime: php72

# Defaults to "serve index.php" and "serve public/index.php". Can be used to
# serve a custom PHP front controller (e.g. "serve backend/index.php") or to
# run a long-running PHP script as a worker process (e.g. "php worker.php").
entrypoint: serve gae-app.php

# Defines static handlers to serve WordPress assets
handlers:
- url: /(.*.(htm|html|css|js))
  static_files: 1
  upload: .*.(htm|html|css|js)$

- url: /wp-content/(.*.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
  static_files: wp-content/1
  upload: wp-content/.*.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$

- url: /(.*.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
  static_files: 1
  upload: .*.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$

- url: /wp-includes/images/media/(.*.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg))
  static_files: wp-includes/images/media/1
  upload: wp-includes/images/media/.*.(ico|jpg|jpeg|png|gif|woff|ttf|otf|eot|svg)$

- url: /.*
  redirect_http_response_code: 301
  script: auto
  secure: always

instance_class: F1

automatic_scaling:
  min_instances: 0
  max_instances: 3

  min_idle_instances: 0
  max_idle_instances: 1

  min_pending_latency: automatic
  max_pending_latency: automatic

27行目以降がカスタマイズの内容となります。

(26行目より上はスクリプトが自動生成した内容となります)

  • 27〜30行目 : httpでリクエストが来た場合、httpsでリダイレクトさせるための記述です。
  • 32行目 : 自動スケーリングの際、最小スペックのインスタンスで自動スケーリングさせるという記述になります。
  • 34〜42行目 : 最小/最大インスタンス数、最小/最大の待機インスタンス数、自動スケール時の最小/最大レイテンシ時間の定義

特に注意して頂きたいのが最大インスタンス数、最大待機インスタンス数の定義です。

この定義がない場合、GAEのインスタンスが際限なく自動スケーリングする可能性があり高額な課金が発生する可能性がありますので必ず設定した方がいいです。

また、デプロイ後過去のバージョンが累積されるので、バージョン数のインスタンスが常に起動している状態となります。

デプロイ後、不要な過去バージョンは忘れずに削除して下さい。

※CI/CDでパイプライン化している場合、四六時中GAEに対しデプロイが発生するため、過去バージョンのインスタンスが累積しまくり○○万円溶かしちゃいましたという怖い話を聞いた事があります。

最小インスタンス数、最小待機インスタンス数を0にすることでリクエストが全く発生してない時は稼動インスタンス数0となる(筈、調べて追記予定、、、)ので、GAEの課金を抑制する事が出来ます。

スケールアウトするタイミングについて、レイテンシやCPU稼働率の定義で細く指定する事が出来ますが、今回のカスタマイズではレイテンシを自動判断するという内容にしました。

今後稼動状況を分析し調整するかもしれません。

app.yamlのリファレンスはこちらを参考にして下さい。

app.yaml 構成ファイル  |  App Engine スタンダード環境での PHP に関するドキュメント  |  Google Cloud
スポンサーリンク

GAEへデプロイ

スクリプトで作成されたWordPressディレクトリ(この記事の例ではmy-wordpress-project)から下記コマンドを実行し、GAEにWrodPressをデプロイします。

gcloud app deploy app.yaml cron.yaml

初回デプロイの場合、どのリージョンにGAEインスタンを作成するかの入力を求められます。

ここではDBインスタンスがある東京リージョン(asia-northest1)に作成するので「2」を入力しEnterを押下します。

Please choose the region where you want your App Engine application
located:

 [1] asia-east2    (supports standard and flexible)
 [2] asia-northeast1 (supports standard and flexible)
 [3] asia-northeast2 (supports standard and flexible)
 [4] asia-northeast3 (supports standard and flexible)
 [5] asia-south1   (supports standard and flexible)
 [6] australia-southeast1 (supports standard and flexible)
 [7] europe-west   (supports standard and flexible)
 [8] europe-west2  (supports standard and flexible)
 [9] europe-west3  (supports standard and flexible)
 [10] europe-west6  (supports standard and flexible)
 [11] northamerica-northeast1 (supports standard and flexible)
 [12] southamerica-east1 (supports standard and flexible)
 [13] us-central    (supports standard and flexible)
 [14] us-east1      (supports standard and flexible)
 [15] us-east4      (supports standard and flexible)
 [16] us-west2      (supports standard and flexible)
 [17] us-west3      (supports standard and flexible)
 [18] us-west4      (supports standard and flexible)
 [19] cancel
Please enter your numeric choice:

確認を求められますので「Y」を入力しEnterを押下します。

Creating App Engine application in project [test-wordpress1] and region [asia-northeast1]....done.
Services to deploy:

descriptor:      [/Users/xxx/wordpress/my-wordpress-project/app.yaml]
source:          [/Users/xxx/wordpress/my-wordpress-project]
target project:  [test-wordpress1]
target service:  [default]
target version:  [20200506t150411]
target url:      
404 Page not found
Configurations to update: descriptor: [/Users/xxx/wordpress/my-wordpress-project/cron.yaml] type: [cron jobs] target project: [test-wordpress1] Do you want to continue (Y/n)?

デプロイが完了するとターミナルに下記メッセージが表示されます。

Visit the Cloud Platform Console Task Queues page to view your queues and cron jobs.
Google Cloud Platform
Google Cloud Platform lets you build, deploy, and scale applications, websites, and services on the same infrastructure as Google.
You can stream logs from the command line by running: $ gcloud app logs tail -s default To view your application in the web browser run: $ gcloud app browse
スポンサーリンク

WordPressの動作確認

WordPressの動作確認を行います。
ターミナルから下記コマンドを入力します。

gcloud app browse

ブラウザ画面が立ち上がり、WordPressの初期設定画面が立ち上がります。

スポンサーリンク

GCSの確認

GAEに初回デプロイする際、GCSにいくつかバゲットが作成されています。
GCPコンソールから確認すると下記の通りとなっています。

赤枠のバゲットにはGAEインスタンス上で動くコンテナイメージが格納されます。

青枠のバゲットにはWordPressの記事で挿入する画像やプラグインが生成する公開可能リソース(CSSやjs)が格納されるバゲットとなります。

青枠のバゲットについて、

  • 参照専用
  • インターネットからどのユーザーもアクセス可

にする必要がありますので、ターミナルより下記コマンドを実行します。

gsutil defacl ch -u AllUsers:R gs://[YOUR_PROJECT_ID].appspot.com

[YOUR_PROJECT_ID]は今回作成したリソースが属するプロジェクIDの値にします。

今回のデプロイリソースに「Google Cloud Strage Plugin」が含まれていますので、WordPressの初期設定後、このプラグインを有効化、設定することで記事に挿入する画像がアップロード可能となります。

スポンサーリンク

補足:テーマやプラグインの追加や削除、更新について

GAEインスタンス上のリソースについて、外部から追加や変更、削除が出来きない制約があります。

このためローカル環境で対応し、再度GAEにデプロイする必要があります。

  • テーマの追加や削除

WordPressが格納されているディレクトリ内にwp-content/themesというディレクトリがあります。

テーマを追加する際は追加テーマをディレクトリごとこのディレクトリに格納すればOKです。

テーマを削除する場合はこのディレクトリ配下にあるテーマをディレクトリごと削除すればOKです。

上記対応を行った後にGAEへデプロイします。

  • プラグインの追加や削除

WordPressが格納されているディレクトリ内にwp-content/pluginsというディレクトリがあります。

プラグインを追加する際は追加テーマをディレクトリごとこのディレクトリに格納すればOKです。

プラグインの注意点として、ファイルを作成・変更などを行うプラグインはGAEの制約上利用出来ません。

ローカル環境でGAEリソースを動かし、プラグインからの出力を反映させる必要があるようです。

プラグインを削除する場合はこのディレクトリ配下にあるテーマをディレクトリごと削除すればOKです。

上記対応を行ったローカルリソースをGAEへデプロイします。

  • WordPress本体、テーマ、プラグインの更新
GAEで構築した当ブログの各種アップデート方法について(Wordpress,Theme,Plugin)
当ブログのWordPressはこちらの手順を元に構築しておます。構築に利用したgoogle/cloud-toolsパッケージの中に、各種アップデートスクリプトが同梱されているので、こちらを利用しアップデートを実施します。...
スポンサーリンク

WordPressの更新について

テーマ、プラグインの更新と同様に更新用スクリプトを利用しローカルリソースの更新を行い、GAEへデプロイします。

以上でGAE環境でWordPressが利用可能となりました。

初めてWordPressをお使いになられる方は、WordPressの初期設定を行った後にお好みのテーマやPlugを準備しご利用頂ければと思います。

公開されているComposerパッケージが秀逸だったので、簡単に環境構築出来たなと思いました。

スポンサーリンク
GCP
ヤマログ
タイトルとURLをコピーしました