Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Commit

Permalink
Adds wordpress community article (#687)
Browse files Browse the repository at this point in the history
* adds wordpress community article

* editorial and production pass

I did a light copy-edit and fixed up a few production issues, including converting codeblocks to use indentation, since our publishing system deals badly with codefences in lists.
  • Loading branch information
bshaffer authored and ToddKopriva committed Jan 29, 2019
1 parent adfb340 commit b815082
Showing 1 changed file with 212 additions and 0 deletions.
212 changes: 212 additions & 0 deletions tutorials/run-wordpress-on-appengine-standard/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
---
title: Run WordPress on Google App Engine standard environment
description: Learn how to deploy a WordPress app to Google App Engine standard environment.
author: bshaffer
tags: App Engine, WordPress, PHP
date_published: 2019-01-31
---

[WordPress][wordpress] is an open source web framework for PHP developers that encourages the use of the model-view-controller (MVC) pattern.

This tutorial illustrates how to use a simple command-line tool for downloading
and configuring WordPress on App Engine standard environment for PHP 7.2.

You can check out [PHP on Google Cloud Platform (GCP)][php-gcp] to get an
overview of PHP and learn ways to run PHP apps on GCP.

## Prerequisites

1. Create a project in the [Google Cloud Platform Console][cloud-console].
1. Enable billing for your project.
1. Install the [Google Cloud SDK][cloud_sdk].
1. [Enable Cloud SQL API][cloud-sql-api-enable].
1. Install [Composer][composer].

### Create and configure a Cloud SQL for MySQL instance

> **Note**: In this guide, we use `wordpress` for the instance name and the database
name. We use `root` for the database user name.

1. Create a new Cloud SQL for MySQL Second Generation instance with the following
command:

gcloud sql instances create wordpress \
--activation-policy=ALWAYS \
--tier=db-n1-standard-1

> **Note**: you can choose `db-f1-micro` or `db-g1-small` instead of
`db-n1-standard-1` for the Cloud SQL machine type, especially for
development or testing purposes. However, those machine types are not
recommended for production use and are not eligible for Cloud SQL SLA
coverage. See the [Cloud SQL SLA](https://cloud.google.com/sql/sla)
for more details.

1. Create the database you want your WordPress site to use:

gcloud sql databases create wordpress --instance wordpress

1. Change the root password for your instance:

gcloud sql users set-password root \
--host=% \
--instance wordpress \
--password=YOUR_INSTANCE_ROOT_PASSWORD # Don't use this password!

## Create or update a WordPress project for App Engine

The `wp-gae` command provides a convenient way for you to either create
a new WordPress project or add the required configuration to an existing one.

### Setup

1. Download the `google/cloud-tools` package:

$ composer require google/cloud-tools

> **Note** If you receive an error about extensions, install `phar` and `zip` PHP
extensions and retry.

1. Now you can run the `wp-gae` command which is included in that package

$ php vendor/bin/wp-gae

> **Note** You can also install `google/cloud-tools` [globally][composer-global],
which will allow you to execute the command `wp-gae` anywhere.

### Create a new WordPress project

To download WordPress and set it up for GCP, run the `create` command:

$ php vendor/bin/wp-gae create

The command asks you several questions. After you answer them, you'll have a
new WordPress project. By default, it will create `my-wordpress-project` in the
current directory.

> **Note**: To determine the region your database is in, use the
`gcloud sql instances describe wordpress` command.

### Update an existing WordPress Project

If you are migrating an existing project to Google Cloud, you can use the
`update` command:

$ php vendor/bin/php update path/to/your-wordpress-site

The command asks you several questions; answer them. This will copy the
files in the [`files`](files/) directory and write the proper configuration.
Then your WordPress project will be ready to deploy to Google Cloud!

## Deploy to Google Cloud

`cd` into the root of your WordPress project.

$ cd my-wordpress-project

Run the following command to deploy your project to App Engine:

$ gcloud app deploy app.yaml cron.yaml

Now you can access your site, and continue the installation step! The URL is
`https://YOUR_PROJECT_ID.appspot.com/`.

### Enable the Google Cloud Storage plugin

To use the [Google Cloud Storage plugin][gcs-plugin] for media uploads, follow
these steps:

1. Configure the App Engine default GCS bucket for later use. The default App
Engine bucket is named YOUR_PROJECT_ID.appspot.com. Change the default Access
Control List (ACL) of that bucket as follows:

$ gsutil defacl ch -u AllUsers:R gs://YOUR_PROJECT_ID.appspot.com

1. Go to the Dashboard at `https://YOUR_PROJECT_ID.appspot.com/wp-admin`. On the
Plugins page, activate the `Google Cloud Storage plugin`.
1. In the plugins Settings page, set your Bucket name to the bucket you
configured in Step 1.

After activating the plugin, try uploading a media object in a new post
and confirm the image is uploaded to the GCS bucket by visiting the
[Google Cloud Console Storage page][cloud-storage-console].

## Local development

To access this MySQL instance, use Cloud SQL Proxy. [Download][cloud-sql-proxy-download]
it to your local computer and make it executable.

Go to the [the Credentials section][credentials-section] of your project in the
Console. Click **Create credentials** and then click **Service account key**. For
the Service account, select **App Engine app default service account**. Then
click **Create** to create and download the JSON service account key to your
local machine. Save it to a safe place.

Run the proxy by the following command:

$ cloud_sql_proxy \
-dir /cloudsql \
-instances=YOUR_PROJECT_ID:us-central1:wordpress \
-credential_file=/path/to/YOUR_SERVICE_ACCOUNT_JSON_FILE.json

> **Note**: Make sure to run the command under a user with write access in the `/cloudsql` directory. This proxy will use this folder to create a unix socket.
Now you can access the Cloud SQL instance with the MySQL client in a separate
command-line tab.

$ mysql --socket /cloudsql/YOUR_PROJECT_ID:us-central1:wordpress -u root -p
mysql> use database wordpress;
mysql> show tables;
mysql> exit

## Various workflows

### Install and update WordPress, plugins, and themes

Because the `wp-content` directory on the server is read-only, you have
to perform all code updates locally. Run WordPress locally and update the
plugins and themes in the local Dashboard, deploy the code to production, then
activate them in the production Dashboard. You can also use the `wp-cli` utility
as follows (be sure to keep the Cloud SQL proxy running):

# Install the wp-cli utility
$ composer require wp-cli/wp-cli-bundle
# Now you can run the "wp" command to update Wordpress itself
$ vendor/bin/wp core update --path=wordpress
# You can also update all the plugins and themes
$ vendor/bin/wp plugin update --all
$ vendor/bin/wp theme update --all

The following error may occur:

Failed opening required 'google/appengine/api/urlfetch_service_pb.php'

If you get this error, you can set a `WP_CLI_PHP_ARGS` environment variable to add
`include_path` PHP configuration for wp-cli:

$ export WP_CLI_PHP_ARGS='-d include_path=vendor/google/appengine-php-sdk'

Then try the update commands again.

### Remove plugins and themes

To remove plugins and themes, first deactivate them in the production Dashboard, and then
remove them completely locally. The next deployment will remove those files from
the production environment.

[php-gcp]: https://cloud.google.com/php
[wordpress]: https://wordpress.org/
[cloud_sdk]: https://cloud.google.com/sdk/

[cloudsql-create]: https://cloud.google.com/sql/docs/mysql/create-instance
[cloudsql-install]: https://cloud.google.com/sql/docs/mysql/connect-external-app#install

[sql-settings]: https://console.cloud.google.com/sql/instances
[mysql-client]: https://dev.mysql.com/doc/refman/5.7/en/mysql.html
[composer]: https://getcomposer.org/
[composer-global]: https://getcomposer.org/doc/03-cli.md#global
[cloud-console]: https://console.cloud.google.com/
[cloud-storage-console]: https://console.cloud.google.com/storage
[cloud-sql-api-enable]: https://console.cloud.google.com/flows/enableapi?apiid=sqladmin
[cloud-sql-proxy-download]: https://cloud.google.com/sql/docs/mysql/connect-external-app#install
[credentials-section]: https://console.cloud.google.com/apis/credentials/
[gcs-plugin]: https://wordpress.org/plugins/gcs/

0 comments on commit b815082

Please sign in to comment.