在 App Engine 彈性環境中託管 WordPress

本教程介紹如何將小型 WordPress 網站部署到 App Engine 彈性環境。

目標

  • 建立 Cloud SQL 第二代執行個體。
  • 設定 WordPress 網站範例。
  • 將 WordPress 網站範例部署至 App Engine 彈性環境。

費用

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the required APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. 安裝 Google Cloud CLI。

  6. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  7. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  8. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the required APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. 安裝 Google Cloud CLI。

  12. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  13. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  14. 依照下列步驟建立憑證:
    • 前往 Google Cloud 控制台的「憑證」頁面。

      開啟 API 憑證頁面

    • 按一下 [Create credentials] (建立憑證),然後選取 [Service account key] (服務帳戶金鑰)
    • 選取 [Service account] (服務帳戶) > [App Engine default service account] (App Engine 預設服務帳戶)
    • 按一下 [Create] (建立).
    • 將下載的金鑰妥善儲存於安全的位置。
  15. 安裝 PHPComposer
  16. 下載 Cloud SQL Proxy 並予以執行。同時將 Cloud SQL Proxy 執行檔的位置新增至 PATH 環境變數。
  17. 安裝 MySQL 用戶端,並確認 mysql 執行檔的位置在您的 PATH 環境變數中。

建立及設定 Cloud SQL 第二代執行個體

  1. 建立 Cloud SQL 第二代執行個體:

    gcloud sql instances create tutorial-sql-instance \
        --activation-policy=ALWAYS \
        --tier=db-n1-standard-1 \
        --region=us-central1
    
  2. 設定執行個體的根密碼:

    gcloud sql users set-password root --instance tutorial-sql-instance \
        --password [YOUR_SQL_ROOT_PASSWORD] \
        --host %
    

    其中 [YOUR_SQL_ROOT_PASSWORD] 是您選擇的安全密碼。

  3. 下載並執行 Cloud SQL 代理

    ./cloud-sql-proxy \
        -dir /tmp/cloudsql \
        -instances=[YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance \
        -credential_file=[PATH_TO_YOUR_SERVICE_ACCOUNT_JSON]

    其中

    • [YOUR_PROJECT_ID] 是您的 Google Cloud 專案 ID。

    • [PATH_TO_YOUR_SERVICE_ACCOUNT_JSON] 是您先前下載之服務帳戶 JSON 檔案的路徑。

    以下輸出表示 Proxy 已準備好進行新的連線:

    Listening on 127.0.0.1:3306 for [YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance
    Ready for new connections
    
  4. 在另一個終端機視窗中,建立新資料庫與使用者:

    mysql -h 127.0.0.1 -u root --password=[YOUR_SQL_ROOT_PASSWORD]
    mysql> create database tutorialdb;
    mysql> create user 'tutorial-user'@'%' identified by '[YOUR_DATABASE_PASSWORD]';
    mysql> grant all on tutorialdb.* to 'tutorial-user'@'%';
    mysql> exit

    其中:

    • [YOUR_SQL_ROOT_PASSWORD] 是您 Cloud SQL 執行個體的根密碼。
    • [YOUR_DATABASE_PASSWORD] 是您選擇的安全密碼。

設定 WordPress 專案

  1. 複製範例存放區:

    git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
    
  2. 前往包含程式碼範例的目錄:

    cd php-docs-samples/appengine/flexible/wordpress
    
  3. 安裝依附元件:

    composer install
    
  4. 執行輔助指令碼:

    php wordpress.php setup -n \
        --dir=./wordpress-project \
        --db_instance=tutorial-sql-instance \
        --db_name=tutorialdb \
        --db_user=tutorial-user \
        --project_id=[YOUR_PROJECT_ID] \
        --db_password=[YOUR_DATABASE_PASSWORD]

    其中:

    • [YOUR_PROJECT_ID] 是您的專案 ID。
    • [YOUR_DATABASE_PASSWORD] 是資料庫密碼。

    -dir 參數指定您 WordPress 專案的位置。

  5. 輔助指令碼會將資訊寫入 wordpress-project/wordpress/wp-config.php。檢查 wp-config.php 的內容,確認您的名稱、專案 ID 和資料庫密碼正確無誤。

    if ($onGae) {
        /** Production environment */
        define('DB_HOST', ':/cloudsql/[YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance');
        /** The name of the database for WordPress */
        define('DB_NAME', 'tutorialdb');
        /** MySQL database username */
        define('DB_USER', 'tutorial-user');
        /** MySQL database password */
        define('DB_PASSWORD', '[YOUR_DATABASE_PASSWORD]');
    } else {
        /** Local environment */
        define('DB_HOST', '127.0.0.1');
        /** The name of the database for WordPress */
        define('DB_NAME', 'tutorialdb');
        /** MySQL database username */
        define('DB_USER', 'tutorial-user');
        /** MySQL database password */
        define('DB_PASSWORD', '[YOUR_DATABASE_PASSWORD]');
    }

將 WordPress 專案部署至 App Engine 彈性環境

  1. 前往 WordPress 專案目錄:

    cd wordpress-project
    
  2. 部署 WordPress 專案:

    gcloud app deploy \
        --promote --stop-previous-version app.yaml cron.yaml
    
  3. 在瀏覽器中輸入下列網址:

    https://PROJECT_ID.REGION_ID.r.appspot.com

    更改下列內容:

更新 WordPress、外掛程式與主題

請務必為 WordPress、外掛程式與主題保持最新狀態。您可以使用 wp 工具來更新這些項目。更新之後,您需要重新部署 WordPress 專案。

  1. 更新 WordPress 本身:

    vendor/bin/wp core update --path=wordpress
    
  2. 更新外掛程式:

    vendor/bin/wp plugin update --all --path=wordpress
    # Just in case it updates any of the dropins, copy the files:
    cp wordpress/wp-content/plugins/batcache/advanced-cache.php \
        wordpress/wp-content/plugins/memcached/object-cache.php \
        wordpress/wp-content
    
  3. 更新主題:

    vendor/bin/wp theme update --all --path=wordpress
    
  4. 再次部署專案:

    gcloud app deploy \
        --promote --stop-previous-version app.yaml cron.yaml
    

清除所用資源

為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。

刪除專案

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

刪除專案的方法如下:

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

刪除應用程式的非預設版本

如果您不想刪除專案,可以透過刪除應用程式的非預設版本來降低費用。

若要刪除應用程式版本:

  1. 前往 Google Cloud 控制台的 App Engine「Versions」(版本) 頁面。

    前往「版本」

  2. 勾選您要刪除的非預設應用程式版本的核取方塊。
  3. 如要刪除應用程式版本,請按一下 「刪除」

刪除 Cloud SQL 執行個體

如要刪除 Cloud SQL 執行個體:

  1. 前往 Google Cloud 控制台的「Instances」(執行個體) 頁面。

    前往「Instances」(執行個體)

  2. 按一下要刪除的 SQL 執行個體名稱。
  3. 如要刪除執行個體,請依序點選 「Delete」(刪除),然後按照指示操作。

後續步驟