shinke1987.net
雑多な備忘録等のはず。
他のカテゴリ・タブ
目次
PR

GAE(PHP)の動作確認

2024-08-08 2024-08-08
カテゴリ: GCP PHP

目的

phpinfo()関数の結果をWebブラウザで表示する。

前提

OS:macOS 14.5 sonoma

GoogleCloudConsoleにてプロジェクトを作成し、GAEアプリケーション(PHP)を作成済み。

GAEのインスタンスクラスはF1。

Google Cloud CLI のセットアップ

クイックスタート: Google Cloud CLI をインストールする | Google Cloud CLI Documentation

上記ページから「macOS 64ビット (ARM64, AppleM1 silicon) 」用のtar.gzファイルをダウンロードし、

ダウンロードしたファイルを任意の場所へ移動し、展開するために下記コマンドを実行する。

% mkdir ~/Development/GoogleAppEngine
% mv ~/Downloads/google-cloud-cli-VERSION-darwin-arm.tar ~/Development/GoogleAppEngine
% cd ~/Development/GoogleAppEngine
% tar xvf google-cloud-cli-VERSION-darwin-arm.tar

下記コマンドでインストールする。

% ./google-cloud-sdk/install.sh

下記のように聞かれるので、それぞれ回答する。

(途中でsudo用のパスワードの入力を求められたりする)

Welcome to the Google Cloud CLI!

To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. This data is
handled in accordance with our privacy policy
<https://cloud.google.com/terms/cloud-privacy-notice>. You may choose to opt in this
collection now (by choosing 'Y' at the below prompt), or at any time in the
future by running the following command:

    gcloud config set disable_usage_reporting false

Do you want to help improve the Google Cloud CLI (y/N)?  y


Your current Google Cloud CLI version is: 477.0.0
The latest available version is: 487.0.0

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                     Components                                                     │
├──────────────────┬──────────────────────────────────────────────────────┬──────────────────────────────┬───────────┤
│      Status      │                         Name                         │              ID              │    Size   │
├──────────────────┼──────────────────────────────────────────────────────┼──────────────────────────────┼───────────┤
│ Update Available │ BigQuery Command Line Tool                           │ bq                           │   1.7 MiB │
│ Update Available │ Cloud Storage Command Line Tool                      │ gsutil                       │  11.3 MiB │
│ Update Available │ Google Cloud CLI Core Libraries                      │ core                         │  19.3 MiB │
│ Not Installed    │ App Engine Go Extensions                             │ app-engine-go                │   4.5 MiB │
│ Not Installed    │ Appctl                                               │ appctl                       │  18.5 MiB │
│ Not Installed    │ Artifact Registry Go Module Package Helper           │ package-go-module            │   < 1 MiB │
│ Not Installed    │ Cloud Bigtable Command Line Tool                     │ cbt                          │  17.0 MiB │
│ Not Installed    │ Cloud Bigtable Emulator                              │ bigtable                     │   7.0 MiB │
│ Not Installed    │ Cloud Datastore Emulator                             │ cloud-datastore-emulator     │  36.2 MiB │
│ Not Installed    │ Cloud Firestore Emulator                             │ cloud-firestore-emulator     │  45.2 MiB │
│ Not Installed    │ Cloud Pub/Sub Emulator                               │ pubsub-emulator              │  63.7 MiB │
│ Not Installed    │ Cloud Run Proxy                                      │ cloud-run-proxy              │  11.3 MiB │
│ Not Installed    │ Cloud SQL Proxy v2                                   │ cloud-sql-proxy              │  13.2 MiB │
│ Not Installed    │ Google Container Registry's Docker credential helper │ docker-credential-gcr        │           │
│ Not Installed    │ Kustomize                                            │ kustomize                    │   7.4 MiB │
│ Not Installed    │ Log Streaming                                        │ log-streaming                │  11.9 MiB │
│ Not Installed    │ Minikube                                             │ minikube                     │  34.8 MiB │
│ Not Installed    │ Nomos CLI                                            │ nomos                        │  30.1 MiB │
│ Not Installed    │ On-Demand Scanning API extraction helper             │ local-extract                │  19.9 MiB │
│ Not Installed    │ Skaffold                                             │ skaffold                     │  22.9 MiB │
│ Not Installed    │ Terraform Tools                                      │ terraform-tools              │  63.6 MiB │
│ Not Installed    │ anthos-auth                                          │ anthos-auth                  │  20.9 MiB │
│ Not Installed    │ config-connector                                     │ config-connector             │  88.7 MiB │
│ Not Installed    │ enterprise-certificate-proxy                         │ enterprise-certificate-proxy │   8.3 MiB │
│ Not Installed    │ gcloud Alpha Commands                                │ alpha                        │   < 1 MiB │
│ Not Installed    │ gcloud Beta Commands                                 │ beta                         │   < 1 MiB │
│ Not Installed    │ gcloud app Java Extensions                           │ app-engine-java              │ 127.8 MiB │
│ Not Installed    │ gcloud app Python Extensions                         │ app-engine-python            │   5.0 MiB │
│ Not Installed    │ gcloud app Python Extensions (Extra Libraries)       │ app-engine-python-extras     │   < 1 MiB │
│ Not Installed    │ gke-gcloud-auth-plugin                               │ gke-gcloud-auth-plugin       │   4.0 MiB │
│ Not Installed    │ istioctl                                             │ istioctl                     │  24.8 MiB │
│ Not Installed    │ kpt                                                  │ kpt                          │  13.8 MiB │
│ Not Installed    │ kubectl                                              │ kubectl                      │   < 1 MiB │
│ Not Installed    │ kubectl-oidc                                         │ kubectl-oidc                 │  20.9 MiB │
│ Not Installed    │ pkg                                                  │ pkg                          │           │
│ Installed        │ Google Cloud CRC32C Hash Tool                        │ gcloud-crc32c                │   1.2 MiB │
└──────────────────┴──────────────────────────────────────────────────────┴──────────────────────────────┴───────────┘
To install or remove components at your current SDK version [477.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [487.0.0], run:
  $ gcloud components update


Modify profile to update your $PATH and enable shell command completion?

Do you want to continue (Y/n)?  Y

The Google Cloud SDK installer will now prompt you to update an rc file to bring the Google Cloud CLIs into your environment.

Enter a path to an rc file to update, or leave blank to use [/Users/ユーザ名/.zshrc]:  
Backing up [/Users/maehara_shinsuke/.zshrc] to [/Users/maehara_shinsuke/.zshrc.backup].
[/Users/maehara_shinsuke/.zshrc] has been updated.

==> Start a new shell for the changes to take effect.


Google Cloud CLI works best with Python 3.11 and certain modules.

Download and run Python 3.11 installer? (Y/n)?  Y

Running Python 3.11 installer, you may be prompted for sudo password...
Password:
installer: Package name is Python
installer: Installing at base path /
installer: The install was successful.
Setting up virtual environment
Creating virtualenv...
Installing modules...
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 89.7/89.7 kB 1.7 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 59.0/59.0 kB 5.2 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.4/10.4 MB 21.1 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 163.0/163.0 kB 14.8 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.3/5.3 MB 19.8 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 178.3/178.3 kB 11.6 MB/s eta 0:00:00
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 117.6/117.6 kB 12.3 MB/s eta 0:00:00
  Building wheel for crcmod (pyproject.toml) ... done


Updates are available for some Google Cloud CLI components.  To install them,
please run:
  $ gcloud components update

Virtual env enabled.

For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts

これでとりあえずインストールは完了したが、次はアップデートを実行する。

アップデートを実行する前に.zshrcが編集されたので、ターミナルを新たに開き直してから、次のコマンドを実行する。

% gcloud components update

途中入力を求められることもあるが(Yを1回押下する程度)続行するとズラズラと長い文字列が表示され、最終的に次のように表示される。

Performing post processing steps...done.                                      

Update done!

To revert your CLI to the previously installed version, you may run:
  $ gcloud components update --version 477.0.0

これでGoogle Cloud CLI のセットアップは完了。

SDKの初期化

SDKを初期化するために次のコマンドを実行する。

% gcloud init

すると下記のように聞かれるので、適宜入力する。

(サインインするためのGoogleアカウントを選択したり、プロジェクトを選択する)

Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.                                                                                                
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).

You must sign in to continue. Would you like to sign in (Y/n)?  Y

Your browser has been opened to visit:

    https://accounts.google.com/o...URLが表示される...

You are signed in as: [Googleアカウントのメールアドレス].

Pick cloud project to use: 
 [1] gae-test-431804
 [2] GCPのプロジェクト名1
 [3] GCPのプロジェクト名1
 [4] Enter a project ID
 [5] Create a new project
Please enter numeric choice or text value (must exactly match list item):  1

Your current project has been set to: [gae-test-431804].

Not setting default zone/region (this feature makes it easier to use
[gcloud compute] by setting an appropriate default value for the
--zone and --region flag).
See https://cloud.google.com/compute/docs/gcloud-compute section on how to set
default compute region and zone manually. If you would like [gcloud init] to be
able to do this for you the next time you run it, make sure the
Compute Engine API is enabled for your project on the
https://console.developers.google.com/apis page.

Created a default .boto configuration file at [/Users/Macのユーザ名/.boto]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
The Google Cloud CLI is configured and ready to use!

* Commands that require authentication will use Googleアカウントのメールアドレス by default
* Commands will reference project `gae-test-431804` by default
Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the CLI like arg files and output formatting
* Run `gcloud cheat-sheet` to see a roster of go-to `gcloud` commands.

デプロイの準備

PHP 7 / 8 ランタイム環境 | Google App Engine standard environment docs | Google Cloud

上記ページを参考にする。

作業フォルダは ~/Development/GoogleAppEngine/GAE-Test というフォルダで行う。

下記2つのファイルを作成する。

GAE-Test/app.yaml の内容

runtime: php83

GAE-Test/index.php の内容

<?php
phpinfo();

デプロイ

app.yaml が存在する、~/Development/GoogleAppEngine/GAE-Test フォルダにて、次のコマンドを実行する。

% gcloud app deploy

すると下記のように表示され、最終的にエラーが表示された。

Services to deploy:

descriptor:                  [/Users/Macのユーザ名/Development/GoogleAppEngine/GAE-Test/app.yaml]
source:                      [/Users/Macのユーザ名/Development/GoogleAppEngine/GAE-Test]
target project:              [gae-test-431804]
target service:              [default]
target version:              [20240807t165719]
target url:                  [https://gae-test-431804.dt.r.appspot.com]
target service account:      [gae-test-431804@appspot.gserviceaccount.com]


Do you want to continue (Y/n)?  Y

Beginning deployment of service [default]...
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 6 files to Google Cloud Storage                ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
Updating service [default]...failed.                                                                                               
ERROR: (gcloud.app.deploy) Error Response: [13] Failed to create cloud build: com.google.net.rpc3.client.RpcClientException: <eye3 title='/ArgoAdminNoCloudAudit.CreateBuild, FAILED_PRECONDITION'/> APPLICATION_ERROR;google.devtools.cloudbuild.v1/ArgoAdminNoCloudAudit.CreateBuild;invalid bucket "staging.gae-test-431804.appspot.com"; service account gae-test-431804@appspot.gserviceaccount.com does not have access to the bucket;AppErrorCode=9;StartTimeMs=1723017487652;unknown;ResFormat=uncompressed;ServerTimeSec=2.239562515;LogBytes=256;Non-FailFast;EndUserCredsRequested;EffSecLevel=none;ReqFormat=uncompressed;ReqID=2bf5e22602762e49;GlobalID=0;Server=[2002:a05:630c:969:b0:6e:903a:5e33]:4001.

新しいプロジェクトのデプロイが失敗する

上記ページを参照すると、デフォルトのサービスアカウントにストレージ管理者ロールが付与されていることを確認してね、とのことなので確認する。

Google Cloud Console → IAM と進み、次の画面を表示する。

名前がApp Engine default service account の行の右側の編集アイコンをクリックし、

下記画像を参考にストレージ管理者ロールを付与する。

デフォルトサービスアカウントにストレージ管理者ロールを付与後、再度コマンドを実行すると、下記のように表示される。

Services to deploy:

descriptor:                  [/Users/Macのユーザ名/Development/GoogleAppEngine/GAE-Test/app.yaml]
source:                      [/Users/Macのユーザ名/Development/GoogleAppEngine/GAE-Test]
target project:              [gae-test-431804]
target service:              [default]
target version:              [20240807t172051]
target url:                  [https://gae-test-431804.dt.r.appspot.com]
target service account:      [gae-test-431804@appspot.gserviceaccount.com]


Do you want to continue (Y/n)?  Y

Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 1 file to Google Cloud Storage                 ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
Updating service [default]...done.                                                                                                 
Setting traffic split for service [default]...done.                                                                                
Deployed service [default] to [https://gae-test-431804.dt.r.appspot.com]

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

次にWebブラウザで上記のhttpsから始まるアドレスへアクセスするか、gcloud app browseコマンドをターミナルから実行すると、Webブラウザが起動されてindex.php の実行結果が表示される。

下記コマンドを実行しておくことでログが発生するたびにターミナルへ表示することができる。

% gcloud app logs tail -s default

下記コマンドを実行すると過去のログを表示することができる。

% gcloud app logs read -s default
同一カテゴリの記事