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

GAE(PHP)のローカル開発用サーバを構築

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

目的

GAE(PHP)のローカル開発用サーバを構築する。

前提

既にデプロイまではできることを確認済みであること。

ローカル環境 → OS:macOS 14.5 sonoma

前回の内容はこちら。

アカウントのアクセス認証情報を取得

下記コマンドを実行する。

% gcloud auth login

すると、Googleのアカウントを選択する画面が表示されるので、アカウントを選択する。

APIアクセス用の認証情報の使用を一時的に許可する

下記コマンドを実行する。

% gcloud auth application-default login

すると、Googleのアカウントを選択する画面が表示されるので、アカウントを選択する。

ローカル開発用サーバを起動

下記コマンドを実行する。

% python3 google-cloud-sdkのパス/bin/dev_appserver.py --application=プロジェクトID app.yaml

すると下記のようなエラーが表示された。

This action requires the installation of components: [app-engine-
python, cloud-datastore-emulator]
  

Your current Google Cloud CLI version is: 487.0.0
Installing components from version: 487.0.0

┌──────────────────────────────────────────────────────────────┐
│             These components will be installed.              │
├─────────────────────────────────────────┬─────────┬──────────┤
│                   Name                  │ Version │   Size   │
├─────────────────────────────────────────┼─────────┼──────────┤
│ Cloud Datastore Emulator                │   2.3.1 │ 36.2 MiB │
│ gRPC Python library (Platform Specific) │  1.20.0 │  1.9 MiB │
│ gcloud app Python Extensions            │ 1.9.113 │  5.0 MiB │
└─────────────────────────────────────────┴─────────┴──────────┘
 
For the latest full release notes, please visit:
  https://cloud.google.com/sdk/release_notes

Once started, canceling this operation may leave your SDK installation in an inconsistent state.

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

Performing in place update...

╔════════════════════════════════════════════════════════════╗
╠═ Downloading: Cloud Datastore Emulator                    ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Downloading: gRPC Python library                         ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Downloading: gRPC Python library (Platform Specific)     ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Downloading: gcloud app Python Extensions                ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Datastore Emulator                     ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gRPC Python library                          ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gRPC Python library (Platform Specific)      ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gcloud app Python Extensions                 ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...done.

Update done!

Restarting command:
  $ dev_appserver.py --application=gae-test-431804 app.yaml

/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/google/appengine/api/datastore.py:1307: SyntaxWarning: invalid escape sequence '\s'
  '^\s*([^\s]+)(\s+(%s)\s*)?$' % '|'.join(OPERATORS),
/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/google/appengine/_internal/antlr3/recognizers.py:511: SyntaxWarning: invalid escape sequence '\<'
  """
/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/lib/webapp2/webapp2/__init__.py:897: SyntaxWarning: invalid escape sequence '\d'
  """Initializes this route.
/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/lib/cherrypy/cherrypy/lib/httputil.py:30: SyntaxWarning: invalid escape sequence '\*'
  """Return the given path \*atoms, joined into a single URL.
/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/lib/cherrypy/cherrypy/_cpreqbody.py:317: SyntaxWarning: invalid escape sequence '\*'
  """A list of strings, each of which should be a known encoding.
/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/lib/cherrypy/cherrypy/_cpreqbody.py:531: SyntaxWarning: invalid escape sequence '\*'
  """A list of strings, each of which should be a known encoding.
/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/lib/cherrypy/cherrypy/process/wspbus.py:1: SyntaxWarning: invalid escape sequence '\_'
  """An implementation of the Web Site Process Bus.
/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/lib/cherrypy/cherrypy/process/servers.py:1: SyntaxWarning: invalid escape sequence '\ '
  """
/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/google/appengine/ext/db/__init__.py:281: SyntaxWarning: invalid escape sequence '\s'
  '^\s*([^\s]+)(\s+(%s)\s*)?$' % '|'.join(_OPERATORS),
/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/lib/py3_antlr3/antlr3/recognizers.py:516: SyntaxWarning: invalid escape sequence '\<'
  """
INFO     2024-08-08 03:47:12,993 <string>:316] Skipping SDK update check.
WARNING  2024-08-08 03:47:12,993 <string>:325] The default encoding of your local Python interpreter is set to 'utf-8' while App Engine's production environment uses 'ascii'; as a result your code may behave differently when deployed.
Traceback (most recent call last):
  File "/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/dev_appserver.py", line 103, in <module>
    _run_file(__file__, globals())
  File "/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/dev_appserver.py", line 99, in _run_file
    _execfile(_PATHS.script_file(script_name), globals_)
  File "/Users/Macのユーザ名/Development/GoogleAppEngine/google-cloud-sdk/platform/google_appengine/dev_appserver.py", line 81, in _execfile
    exec(open(fn).read(), scope)
  File "<string>", line 638, in <module>
  File "<string>", line 626, in main
  File "<string>", line 339, in start
PhpPathError: For php83, --php_executable_path must be specified.

–php_executable_path を設定してね、って内容なので設定する。

ここで設定するのはローカルのPHPの実行ファイルなので、下記コマンドで場所を確認する。

% whereis php

確認したPHPの実行ファイルのパスを指定して再度実行する。

% python3 google-cloud-sdkのパス/bin/dev_appserver.py --php_executable_path=/opt/homebrew/bin/php --application=プロジェクトID app.yaml

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

    raise RuntimeError(
RuntimeError: Unknown runtime 'php83'; supported runtimes are 'go119', 'go120', 'go121', 'go122', 'php81', 'php82', 'python310', 'python311', 'python312', 'python38', 'python39'.

app.yaml でphp83をruntimeに指定しているからだと思うので、最新版を使えないのは残念だけどPHP8.2を使うようapp.yaml を修正して再度実行する。

下記のように表示された。

INFO     2024-08-08 03:59:50,438 <string>:316] Skipping SDK update check.
WARNING  2024-08-08 03:59:50,439 <string>:325] The default encoding of your local Python interpreter is set to 'utf-8' while App Engine's production environment uses 'ascii'; as a result your code may behave differently when deployed.
INFO     2024-08-08 03:59:50,915 <string>:391] Starting API server at: http://localhost:53848
INFO     2024-08-08 03:59:51,041 dispatcher.py:267] Starting module "default" running at: http://localhost:8080
INFO     2024-08-08 03:59:51,044 admin_server.py:67] Starting admin server at: http://localhost:8000
[Thu Aug  8 12:59:53 2024] PHP 8.3.7 Development Server (http://localhost:53851) started
INFO     2024-08-08 03:59:54,074 instance.py:293] Instance PID: 12827
[Thu Aug  8 12:59:54 2024] 127.0.0.1:53856 Accepted
[Thu Aug  8 12:59:54 2024] 127.0.0.1:53856 Closed without sending a request; it was probably just an unused speculative preconnection
[Thu Aug  8 12:59:54 2024] 127.0.0.1:53856 Closing

Webブラウザで確認すると、http://localhost:8000 は表示可能だが、

http://localhost:8080 は繋がらなかったので、ポート番号を変更する。

ポート番号を9999に変更予定だが、使われていないことを確認するには下記コマンドを実行する。

% lsof -i:9999

実行後、何も表示されなければ使われていない。

ポート番号を変更して、再度開発用サーバを起動するために下記コマンドを実行する。

% python3 google-cloud-sdkのパス/bin/dev_appserver.py --php_executable_path=/opt/homebrew/bin/php --application=プロジェクトID --port=9999 app.yaml

下記のように表示された。

INFO     2024-08-08 04:18:55,258 <string>:316] Skipping SDK update check.
WARNING  2024-08-08 04:18:55,258 <string>:325] The default encoding of your local Python interpreter is set to 'utf-8' while App Engine's production environment uses 'ascii'; as a result your code may behave differently when deployed.
INFO     2024-08-08 04:18:55,664 <string>:391] Starting API server at: http://localhost:54142
INFO     2024-08-08 04:18:55,757 dispatcher.py:267] Starting module "default" running at: http://localhost:9999
INFO     2024-08-08 04:18:55,760 admin_server.py:67] Starting admin server at: http://localhost:8000
[Thu Aug  8 13:18:56 2024] PHP 8.3.7 Development Server (http://localhost:54145) started
INFO     2024-08-08 04:18:57,787 instance.py:293] Instance PID: 13171
[Thu Aug  8 13:18:57 2024] [::1]:54148 Accepted
[Thu Aug  8 13:18:57 2024] [::1]:54148 Closed without sending a request; it was probably just an unused speculative preconnection
[Thu Aug  8 13:18:57 2024] [::1]:54148 Closing

この状態で下記のアドレスへChromeからアクセスしてみると、下記3カ所全てアクセスできた。
http://localhost:54142
http://localhost:9999
http://localhost:8000

試しにindex.phpの内容を次のように変更する。

<?php
echo <<<EOF
<html>
  <head>
    <title>index.php</title>
  </head>
  <body>
    aiueo
  </body>
</html>
EOF;

それからhttp://localhost:9999 へアクセスし変更が反映されていれば、
ひとまず開発用サーバは起動できたことになる。

同一カテゴリの記事