google app engine/phpやってみた

google app engine

## Standard environmentとFlexible environment
サービスが動く環境が2種類用意されている。
Standard 

```
The PHP 7.2 runtime is capable of running any framework, library, or binary.
The PHP 5.5 runtime does not allow native code, filesystem access, or arbitrary network connections, and has proprietary APIs.

Optimized to scale nearly instantaneously to handle huge traffic spikes.
```

Flexible

```
Open source runtimes capable of running any framework, library, or binary.

Greater CPU and memory instance types.

Can access resources in the same Compute Engine network.

PHP 5.6, 7.0, 7.1, 7.2
```

これを読むと違いがわかる。
> フレキシブル環境は、スタンダード環境を補完することを目的としています

https://cloud.google.com/appengine/docs/flexible/java/flexible-for-standard-users?hl=ja

## 展開
app.yamlに展開環境やURLパターン等を書き、以下のコマンドで展開できる

```
$ gcloud app deploy
```

本当に展開しますか?的なことを聞かれれるのが面倒なので-qオプションをつける

```
$ gcloud app deploy -q
```

エイリアス組んでおく。terminal-notifierを使って終わったら通知してくれるように。

```.bashrc
alias gad='gcloud app deploy ; terminal-notifier -title "gcloud app deploy" -message done -sound a'
alias gadq='gcloud app deploy -q ; terminal-notifier -title "gcloud app deploy" -message done -sound a'
alias gab='gcloud app browse'
```

## app.yamlメモ
ここを見ながらやっていくが書かれていない挙動がちょいちょいあるので注意
https://cloud.google.com/appengine/docs/standard/php/config/appref?hl=ja

#### 最初構成チェック
app.yamlというファイルに書かれた情報を元に展開される。
展開するだけならapp.yamlファイルさえあれば良い? と思ったけど空のapp.yamlだと展開時にエラー
ちなみにapp.ymlはダメ。エラーがでる

```
ERROR: An app.yaml (or appengine-web.xml) file is required to deploy this directory as an App Engine application. Create an app.yaml file using the directions at https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml (App Engine Flexible Environment) or https://cloud.google.com/appengine/docs/standard/python/config/appref (App Engine Standard Environment) under the tab for your language.
```

```app.yaml
何もかかない
```

```結果.yaml
ERROR: (gcloud.app.deploy) An error occurred while parsing file: [path/app.yaml]
```

適当なruntimeを書くとINVALID_ARGUMENTと怒られるがGoogle Cloud Storageにupはされる。

```app.yaml
runtime: piyo
```

```結果.yaml
Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 1 file to Google Cloud Storage                 ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
ERROR: (gcloud.app.deploy) INVALID_ARGUMENT: Invalid runtime 'piyo' specified. Accepted runtimes are: [go, php, php55, python27, java, java7, java8, go111, go112, nodejs8, nodejs10, php72, python37]
```

ならばphpと書くと、、、エラー。phpと書くとデフォルトでphp54になるが、php54は使用できないよということかな

```app.yaml
runtime: php
```

```結果.yaml
Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 0 files to Google Cloud Storage                ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
ERROR: (gcloud.app.deploy) INVALID_ARGUMENT: PHP 5.4 applications are prevented from being deployed to Google App Engine from any version of the SDK, including older ones. If you need to continue to deploy PHP 5.4 applications for compatibility reasons, you can request that your application be whitelisted for PHP 5.4 deployment by visiting http://goo.gl/qjKEuk.
```

```翻訳.yaml
PHP 5.4アプリケーションは、古いバージョンを含むSDKのどのバージョンからもGoogle App Engineにデプロイされません。互換性の理由から引き続きPHP 5.4アプリケーションをデプロイする必要がある場合は、http：//goo.gl/qjKEukにアクセスして、アプリケーションをPHP 5.4デプロイ用にホワイトリストに登録するように要求できます。
```

php54でもデプロイできるのかな -> ダメでした。

```app.yaml
runtime: php54
```

```結果.yaml
Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 1 file to Google Cloud Storage                 ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
ERROR: (gcloud.app.deploy) INVALID_ARGUMENT: Invalid runtime 'php54' specified. Accepted runtimes are: [go, php, php55, python27, java, java7, java8, go111, go112, nodejs8, nodejs10, php72, python37]
```

php55にしてみる。handler書けって言われた。

```app.yaml
runtime: php55
```

```結果.yaml
Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 1 file to Google Cloud Storage                 ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
ERROR: (gcloud.app.deploy) INVALID_ARGUMENT: At least one handler must be provided.
```

php55 + handler書くと展開成功。しかしブラウザでアクセスすると404エラー

```app.yaml
runtime: php55

handlers:
- url: /(.+\.php)$
  script: \1
```

```結果.yaml
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://cool-component-234904.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
```

stackdriverのログみると↓のエラーが出てた。index.phpが必要なのか

```
ERROR: serve handler failed: inferring default front controller: front controller not found at any of these paths: [public/index.php index.php]
```

index.phpを用意し展開すると正常にアクセスできた。

```index.php
<?php
echo "PHP " . phpversion();
```
<img width="142" alt="スクリーンショット 2019-03-22 1.19.22.png" src="https://qiita-image-store.s3.amazonaws.com/0/278474/b2643909-f6db-df43-e771-5c0bcfdc5e44.png">


一旦app.yamlをruntimeだけにし、index.phpを削除しruntime php72を試す。
-> 展開は成功した。
-> .gcloudignoreファイルが自動的に作られた。
-> handlerは書かなくても展開される

```app.yaml
runtime: php72
```

```結果.yaml
Beginning deployment of service [default]...
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
╔════════════════════════════════════════════════════════════╗
╠═ 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://cool-component-234904.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
```

```自動生成された.gcloudignore
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
#   $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore

# PHP Composer dependencies:
/vendor/
```

index.phpを作って展開するとphp72になっていることが確認できた。
<img width="113" alt="スクリーンショット 2019-03-22 1.34.35.png" src="https://qiita-image-store.s3.amazonaws.com/0/278474/1d7e4035-8ffa-53da-ce41-34cfbc3a46f7.png">