LoginSignup
0
0

More than 1 year has passed since last update.

GAE+Cloud SQLでphpMyAdminを使う

Last updated at Posted at 2022-04-12

はじめに

GoogleAppsScript+Cloud SQLを利用してDB操作をしていたのですが、使い慣れているphpMyAdminなども利用したいと考えていたところ、GoogleAppEngineを使えば出来ることに気が付きました。
このページではそのやり方やつまづいたところ、対応方法を書いておきます。

php5.5+phpMyAdmin4.9.xの場合

GoogleAppEngineでphpMyAdminを利用したい場合、下記に大まかな作成方法が示されています。
App Engine スタンダード環境の Cloud SQL で phpMyAdmin を使用する

ただこの通りに実行した場合、ログイン時に「connection refused」と出てログインできませんでした。IAM設定などを見直しても同様です。
色々調べた結果、app.yamlにIAMで権限付与を行ったサービスアカウントを明記した後デプロイすることでログインできるようになりました。

app.yaml
service: phpmyadmin
runtime: php55
api_version: 1

handlers:

- url: /(.+\.(ico|jpg|png|gif))$
  static_files: \1
  upload: (.+\.(ico|jpg|png|gif))$
  application_readable: true

- url: /(.+\.(htm|html|css|js))$
  static_files: \1
  upload: (.+\.(htm|html|css|js))$
  application_readable: true

- url: /(.+\.php)$
  script: \1
  login: admin

- url: /.*
  script: index.php
  login: admin

service_account: XXXXXXXXXXX@appspot.gserviceaccount.com

ちなみにphp5.5+phpmyadmin4.9.xの環境では、JSON型のフィールドがある場合エラーで表示できなくなってしまうようです(JSON型に対応しているのはphp5.6以上とのこと)。
また/tmpもphp5では利用できません。参考:一時ファイルの読み取りと書き込み

その場合はphp7.4+phpmyadmin5.1.x系で動かすのですが、そのままではログインできない&画面遷移が起きない等エラーが出て動かないため、下記に対応方法を記載します。

php7.4+phpMyAdmin5.1.xの場合

下記のスレッドの途中にて動作させる方法が記載されています。
config.inc.phpの設定は4.9.xのインストール方法とほぼ同じですので、そちらも見比べながら進めるのがおすすめです。

GoogleAppEngine V5.1.0 - Failed to set session cookie. Maybe you are using HTTP instead of HTTPS.

4.9.xの方で記載のあるconfig.inc.phpに下記を更に追加

config.inc.php
$cfg['TempDir'] = '/tmp';
$cfg['PmaAbsoluteUri'] = 'https://xxxxx.appspot.com/'; //←デプロイ後のURL

カスタムエントリポイントとしてpublic.phpを新規作成(後でapp.yamlで指定する)

public.php
<?php

declare(strict_types=1);

$path = @parse_url($_SERVER['REQUEST_URI'])['path'];

switch ($path) {
    case '/':
    case '/index.php':
        // Use the right uri or session set will fail (set PmaAbsoluteUri to be sure it's fixed)
        $_SERVER['REQUEST_URI'] = '/index.php';
        require __DIR__ . '/index.php';
        break;
    case '/url.php':
        // Use the right uri just in case (set PmaAbsoluteUri to be sure it's fixed)
        $_SERVER['REQUEST_URI'] = '/url.php';
        require __DIR__ . '/url.php';
        break;
    case '/show_config_errors.php':
        require __DIR__ . '/show_config_errors.php';
        break;
    case '/js/messages.php':
        // Use the right uri just in case (set PmaAbsoluteUri to be sure it's fixed)
        $_SERVER['REQUEST_URI'] = '/js/messages.php';
        require __DIR__ . '/js/messages.php';
        break;
    default:
        http_response_code(404);
        exit('Not Found: ' . json_encode([
            'path' => $path,
            'uri' => $_SERVER['REQUEST_URI'],
        ]));
}

デプロイ用app.yamlを作成(エントリポイントの指定)

app.yaml
service: phpmyadmin
runtime: php74

entrypoint: serve public.php

handlers:

- url: /(.+\.(ico|jpg|png|gif))$
  static_files: \1
  upload: (.+\.(ico|jpg|png|gif))$

- url: /(.+\.(htm|html|css|js))$
  static_files: \1
  upload: (.+\.(htm|html|css|js))$

- url: .*
  script: auto
  secure: always

service_account: XXXXXXXXXXX@appspot.gserviceaccount.com

これでphpMyAdmin5.1.xがエラーなく開けるようになりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0