在 App Engine 標準環境中搭配使用 phpMyAdmin 與 Cloud SQL


瞭如何在 App Engine 標準環境中安裝 phpMyAdmin。您可以使用 phpMyAdmin 來透過網路管理 Cloud SQL。

如果您執行下列操作,會發現這份教學課程非常有幫助:

  • 在 App Engine 上執行應用程式。
  • 使用 Cloud SQL 做為您的資料庫。
  • 使用 phpMyAdmin 做為 MySQL 的介面,或者您偏好使用網路介面執行資料庫管理。

如果您使用 Compute Engine,請考慮使用透過「點擊部署」提供的其中一種開發堆疊或產品。部署包括 MySQL 的堆疊 (例如 LAMPLEMP) 或產品 (例如 Drupal),可讓您選擇在部署中安裝 phpMyAdmin。

目標

  • 在 App Engine 標準環境中部署 phpMyAdmin。

費用

本教學課程使用 Cloud Platform 可計費元件,包括:

  • App Engine
  • Cloud SQL

使用 Pricing Calculator 根據預測使用量產生預估費用。

初次使用 Cloud Platform 的使用者可能符合免費試用的資格。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Install the gcloud CLI.

  5. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  6. To initialize the gcloud CLI, run the following command:

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  8. Make sure that billing is enabled for your Google Cloud project.

  9. Install the gcloud CLI.

  10. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  11. To initialize the gcloud CLI, run the following command:

    gcloud init
  12. 建立 Cloud SQL 第二代執行個體
  13. (選用) 部署使用 Cloud SQL 執行個體的 App Engine 應用程式,或選取現有應用程式。

    舉例來說,您可以建立及部署留言簿範例。雖然您可以單獨部署 phpMyAdmin,但在實際情況中,您可能想將 phpMyAdmin 與 App Engine 應用程式搭配使用。

下載 phpMyAdmin 原始碼

您會將 phpMyAdmin 部署為 App Engine 應用程式的服務,因此必須下載 phpMyAdmin 的原始碼。步驟如下:

  1. 在 Cloud Shell 終端機中,輸入下列指令來下載 phpMyAdmin 4.9.5 版的原始碼:

    wget https://files.phpmyadmin.net/phpMyAdmin/4.9.5/phpMyAdmin-4.9.5-all-languages.tar.gz
    

    如要使用不同版本的 phpMyAdmin,請使用 phpMyAdmin 下載頁面中可用版本的連結。

  2. 建立新目錄。您會將檔案解壓縮到這個目錄中。

    mkdir phpMyAdmin
    
  3. 將檔案從封存檔案解壓縮到新目錄中。

    tar -xzvf phpMyAdmin-4.9.5-all-languages.tar.gz -C phpMyAdmin --strip-components=1
    

準備檔案進行部署

如要部署 phpMyAdmin,您必須建立三個檔案:app.yaml (包含 App Engine 的設定資訊)、config.inc.php (包含 phpMyAdmin 的設定資訊),以及 php.ini (包含 PHP 的應用程式專屬設定)。

建立「app.yaml

App Engine 設定檔可指定網址路徑對應於要求處理常式和靜態檔案的方式。它同時包含應用程式程式碼的相關資訊,例如應用程式 ID 及最新版的識別碼。請按照下列步驟建立檔案:

  1. 在您建立的 phpMyAdmin 目錄中,建立名為 app.yaml 的新檔案。

    cd phpMyAdmin
    touch app.yaml
    
  2. 使用您慣用的編輯器,將下列文字貼到 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
    
  3. 如果您將 phpMyAdmin 部署為 App Engine 中的第一個也是唯一一個應用程式,請將 service 的值從 phpmyadmin 變更為 default

    通常,您會將 phpMyAdmin 部署為現有應用程式的服務,並為服務提供名稱。但是,如果您尚未部署應用程式,則系統會要求您使用服務名稱「default」。如果您要在 App Engine 上試用 phpMyAdmin,這樣做在本教學課程中是可行的。

    這份教學課程僅適用於 App Engine 標準環境。

  4. 儲存檔案。

建立「config.inc.php

請按照下列步驟建立 phpMyAdmin 設定檔。

  1. 建立名為 config.inc.php 的新檔案。

    touch config.inc.php
    
  2. 使用您慣用的編輯器,將下列文字貼到 config.inc.php 中。

    <?php
    /**
     * Copyright 2016 Google Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    /*
     * This is needed for cookie based authentication to encrypt password in
     * cookie
     * http://www.question-defense.com/tools/phpmyadmin-blowfish-secret-generator
     */
    $cfg['blowfish_secret'] = '{{your_secret}}'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
    
    /*
     * Servers configuration
     */
    $i = 0;
    
    // Change this to use the project and instance that you've created.
    $host = '/cloudsql/{{your_connection_string}}';
    $type = 'socket';
    
    /*
    * First server
    */
    $i++;
    /* Authentication type */
    $cfg['Servers'][$i]['auth_type'] = 'cookie';
    /* Server parameters */
    $cfg['Servers'][$i]['socket'] = $host;
    $cfg['Servers'][$i]['connect_type'] = $type;
    $cfg['Servers'][$i]['compress'] = false;
    /* Select mysql if your server does not have mysqli */
    $cfg['Servers'][$i]['extension'] = 'mysqli';
    $cfg['Servers'][$i]['AllowNoPassword'] = true;
    /*
     * End of servers configuration
     */
    
    /*
     * Directories for saving/loading files from server
     */
    $cfg['UploadDir'] = '';
    $cfg['SaveDir'] = '';
    
    /*
    * Other settings
    */
    $cfg['PmaNoRelation_DisableWarning'] = true;
    $cfg['ExecTimeLimit'] = 60;
    $cfg['CheckConfigurationPermissions'] = false;
    
  3. 開啟 Google Cloud Shell 並執行下列指令,取得 Blowfish 的隨機字串:

    php -r "echo password_hash(uniqid(), PASSWORD_BCRYPT).PHP_EOL;"
    
  4. 將新密鑰貼到 config.inc.php{{your_secret}} 處。

  5. 前往 Google Cloud 控制台的 Cloud SQL 執行個體頁面

  6. 按一下 Cloud SQL 執行個體,顯示「Instance details」(執行個體詳細資料) 頁面。

  7. {{your_connection_string}} 的值 (在 $host 變數內) 取代為「執行個體連線名稱」屬性。

  8. 儲存檔案。

建立「php.ini

phpMyAdmin 的程式碼會使用 App Engine 預設停用的函式。請按照下列步驟新增 php.ini 檔案,讓 App Engine 重新啟用函式:

  1. phpMyAdmin 目錄中建立檔案。

    touch php.ini
    
  2. 編輯檔案並新增下行:

    google_app_engine.enable_functions = "php_uname, getmypid"
    
  3. 儲存檔案。

部署應用程式

使用下列指令,將您的應用程式部署至 App Engine。

  1. 檢查 gcloud 元件的更新。

    gcloud components update
    
  2. app.yaml 檔案所在的 phpMyAdmin 目錄中執行下列指令,來部署應用程式:

    gcloud app deploy
    

    這個指令會像指定 app.yaml 檔案一樣,將應用程式部署至 phpMyAdmin 服務。部署至單獨的服務有助於確保 phpMyAdmin 在與主要應用程式相同的資料中心執行,進而改善效能。如要進一步瞭解如何透過指令列部署應用程式,請參閱部署 PHP 應用程式

登入 phpMyAdmin

現在可以登入 phpMyAdmin。

  1. 在網路瀏覽器中,輸入 phpMyAdmin 的網址來開啟歡迎頁面,進而將網址變更為使用您的應用程式 ID。

        https://phpmyadmin-dot-[YOUR_APP_ID].appspot.com
    
  2. 針對「Username」(使用者名稱),輸入 root

  3. 輸入在您設定 root 帳戶時提供的 root 密碼。

  4. 按一下 [Go] (前往)

在您部署 App Engine 應用程式時,請記得以密碼保護您建立來存取 Cloud SQL 中資料庫的任何使用者帳戶。

疑難排解

App Engine 會使用 Cloud SQL 驗證 Proxy 連線至第二代 Cloud SQL 執行個體。如要進一步瞭解 Cloud SQL 驗證 Proxy 的運作方式,請參閱關於 Cloud SQL 驗證 Proxy 一文。

Google Cloud 主控台中的 App Engine 記錄可提供關於 App Engine 錯誤的資訊。

清除所用資源

完成教學課程後,您可以清除所建立的資源,這樣資源就不會繼續使用配額,也不會產生費用。下列各節將說明如何刪除或關閉這些資源。

刪除專案

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

如要刪除專案:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

刪除執行個體

如要刪除 Cloud SQL 執行個體:

  1. In the Google Cloud console, go to the Instances page.

    Go to Instances

  2. Click the name of the SQL instance you that want to delete.
  3. To delete the instance, click Delete, and then follow the instructions.

後續步驟