如果您執行下列操作,會發現這份教學課程非常有幫助:
- 在 App Engine 上執行應用程式。
- 使用 Cloud SQL 做為您的資料庫。
- 使用 phpMyAdmin 做為 MySQL 的介面,或偏好使用網頁介面管理資料庫。
如果您使用 Compute Engine,建議使用 Click to Deploy 提供的開發堆疊或產品。部署包含 MySQL 的堆疊 (例如 LAMP 和 LEMP) 或 Drupal 等產品時,可以選擇在部署過程中安裝 phpMyAdmin。
目標
- 在 App Engine 標準環境中部署 phpMyAdmin。
費用
本教學課程使用 Cloud Platform 可計費元件,包括:
- App Engine
- Cloud SQL
使用 Pricing Calculator,根據預測用量估算費用。
事前準備
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Install the gcloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init - 建立 Cloud SQL 第二代執行個體。
- (選用) 部署使用 Cloud SQL 執行個體的 App Engine 應用程式,或選取現有應用程式。
例如,建立及部署留言板範例。雖然您可以單獨部署 phpMyAdmin,但在實際情境中,您可能需要搭配 App Engine 應用程式使用。
下載 phpMyAdmin 原始碼
您將部署 phpMyAdmin 做為 App Engine 應用程式的服務,因此必須下載 phpMyAdmin 的原始碼。步驟如下:
在 Cloud Shell 終端機中,輸入下列指令,下載 phpMyAdmin 5.2.3 版的原始碼:
wget https://files.phpmyadmin.net/phpMyAdmin/5.2.3/phpMyAdmin-5.2.3-all-languages.tar.gz如要使用不同版本的 phpMyAdmin,請使用 phpMyAdmin 下載頁面中可用版本的連結。
建立新目錄。您會將檔案解壓縮到這個目錄中。
mkdir phpMyAdmin將檔案從封存檔案解壓縮到新目錄中。
tar -xzvf phpMyAdmin-5.2.3-all-languages.tar.gz -C phpMyAdmin --strip-components=1
準備檔案進行部署
如要部署 phpMyAdmin,您必須建立兩個檔案:app.yaml (包含 App Engine 的設定資訊) 和 config.inc.php (包含 phpMyAdmin 的設定資訊)。
建立「app.yaml」
App Engine 設定檔可指定網址路徑對應於要求處理常式和靜態檔案的方式。此外,也包含應用程式程式碼的相關資訊,例如應用程式 ID 和最新版本 ID。請按照下列步驟建立檔案:
在您建立的
phpMyAdmin目錄中,建立名為app.yaml的新檔案。cd phpMyAdmin touch app.yaml使用慣用的編輯器,將下列文字貼到
app.yaml。service: phpmyadmin runtime: php83 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如果您要將 phpMyAdmin 部署為 App Engine 中的第一個也是唯一的應用程式,請將
service的值從phpmyadmin變更為default。一般來說,您會將 phpMyAdmin 部署為現有應用程式的服務,並為服務提供名稱。不過,如果您尚未部署應用程式,則必須使用服務名稱「default」。如果您是在 App Engine 上試用 phpMyAdmin,這對本教學課程的目的來說沒有問題。
這份教學課程僅適用於 App Engine 標準環境。
儲存檔案。
建立「config.inc.php」
請按照下列步驟建立 phpMyAdmin 設定檔。
建立名為
config.inc.php的新檔案。touch config.inc.php使用慣用的編輯器,將下列文字貼到
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'] = ''; $cfg['TempDir'] = '/tmp'; /* * Other settings */ $cfg['PmaNoRelation_DisableWarning'] = true; $cfg['ExecTimeLimit'] = 60; $cfg['CheckConfigurationPermissions'] = false;開啟 Cloud Shell 並執行下列指令,為 Blowfish 取得隨機字串:
php -r "echo password_hash(uniqid(), PASSWORD_BCRYPT).PHP_EOL;"將新的密鑰貼到
config.inc.php中的{{your_secret}}。前往 Google Cloud 控制台的 Cloud SQL 執行個體頁面。
按一下 Cloud SQL 執行個體,顯示「Instance details」(執行個體詳細資料) 頁面。
將
{{your_connection_string}}(位於$host變數中) 的值替換為「執行個體連線名稱」屬性。儲存檔案。
部署應用程式
使用下列指令,將您的應用程式部署至 App Engine。
檢查
gcloud元件是否有更新。gcloud components update在
app.yaml檔案所在的phpMyAdmin目錄中執行下列指令,部署應用程式:gcloud app deploy這個指令會將應用程式部署至
phpMyAdmin服務,如app.yaml檔案中所指定。部署至單獨的服務有助於確保 phpMyAdmin 在與主要應用程式相同的資料中心執行,進而改善效能。如要進一步瞭解如何透過指令列部署應用程式,請參閱「部署 PHP 應用程式」。
登入 phpMyAdmin
現在可以登入 phpMyAdmin。
在網路瀏覽器中,輸入 phpMyAdmin 的網址來開啟歡迎頁面,進而將網址變更為使用您的應用程式 ID。
https://phpmyadmin-dot-[YOUR_APP_ID].appspot.com針對「Username」(使用者名稱),輸入 root。
輸入在您設定 root 帳戶時提供的 root 密碼。
按一下 [Go] (前往)。
在您部署 App Engine 應用程式時,請記得以密碼保護您建立來存取 Cloud SQL 中資料庫的任何使用者帳戶。
疑難排解
App Engine 會使用 Cloud SQL Auth Proxy 連線至 Cloud SQL 第 2 代執行個體。如要進一步瞭解 Cloud SQL Auth Proxy 的運作方式,請參閱「關於 Cloud SQL Auth Proxy」。
Google Cloud 控制台中的 App Engine 記錄可提供 App Engine 錯誤的相關資訊。
清除所用資源
完成教學課程後,您可以清除所建立的資源,這樣資源就不會繼續使用配額,也不會產生費用。下列各節將說明如何刪除或關閉這些資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
刪除專案的方法如下:
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除執行個體
如要刪除 Cloud SQL 執行個體:
- 前往 Google Cloud 控制台的「Instances」(執行個體) 頁面。
- 按一下要刪除的 SQL 執行個體名稱。
- 如要刪除執行個體,請依序點選 >「Delete」(刪除),然後按照指示操作。
後續步驟
- 進一步瞭解 phpMyAdmin。
- 查看 Google Cloud 的參考架構、圖表和最佳做法。歡迎瀏覽我們的 Cloud Architecture Center。