In this codelab, you'll learn how to run WordPress on App Engine Flexible Environment.

What you'll learn

What you'll need

How will you use use this tutorial?

Read it through only Read it and complete the exercises

How would you rate your experience with PHP?

Novice Intermediate Proficient

How would you rate your experience with using Google Cloud Platform services?

Novice Intermediate Proficient

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and enable billing in the Developers Console in order to use Google Cloud resources like Cloud SQL and Cloud Storage. Then create a new project:

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document). Prices are documented here: App Engine Flexible Environment, Cloud SQL & Cloud Storage

New users of Google Cloud Platform are eligible for a $300 free trial.

Using Google Cloud Shell

While you can use your laptop, in this codelab we will be using Google Cloud Shell, a command line environment running in the Cloud.

This Debian-based virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. This means that all you will need for this codelab is a browser (yes, it works on a Chromebook).

To activate Google Cloud Shell, from the developer console simply click the button on the top right-hand side (it should only take a few moments to provision and connect to the environment):

Once connected to the cloud shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID :

gcloud auth list

Command output

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

Command output

[core]
project = <PROJECT_ID>

If for some reason the project is not set, simply issue the following command :

gcloud config set project <PROJECT_ID>

Looking for your PROJECT_ID? Check out what ID you used in the setup steps or look it up in the console dashboard:

IMPORTANT: Finally, set the default zone and project configuration:

gcloud config set compute/zone us-central1-f

You can choose a variety of different zones. Learn more in the Regions & Zones documentation.

In this step, you will make sure the Cloud SQL API is enabled and create Cloud SQL instance, a Cloud Storage bucket and configure them for your WordPress blog.

Enable Cloud SQL API

If you're running this lab on qwiklab skip this step. Visit this link to make sure the Cloud SQL API is enabled.

Create a Cloud SQL instance

  1. Under Storage, click SQL
  2. Click Create instance
  3. Click Choose Second Generation
  4. Type wp in Instance ID field and click Create at the bottom of the screen. Wait for the instance to come up.

Create a User and a Database

  1. Click the wp instance you just created
  2. Click Access Control, then Users
  3. Click Create user account
  4. Type wp in the User name field, choose your password and type it in Password field, then click Create (make a note of this password because you will need it later in the lab)
  5. Click Databases tab
  6. Click New database button
  7. Type wp in the Database name field and click ADD

Create a Cloud Storage Bucket and set the default permission

  1. In the Google Cloud Platform menu, under STORAGE, click Storage
  2. Click CREATE BUCKET
  3. Type your desired bucket name (for example, my-wordpress-bucket-999) in Name and click Create
  4. Click Buckets at the top left
  5. Find the bucket you just created and click the vertical three dots on the right, then click Edit object default permissions as shown below
  6. Click Add item
  7. Choose User in the ENTITY field, then type allUsers in NAME field
  8. Click SAVE

Summary

In this step, you created a Google Developer Console project and created a Cloud SQL instance and Cloud Storage bucket and configured them for your WordPress blog.

First install Composer with the following commands:

mkdir -p ${HOME}/bin
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === rtrim(file_get_contents('https://composer.github.io/installer.sig'))) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php --filename=composer --install-dir=${HOME}/bin
export PATH=${HOME}/bin:${PATH}

Now you should run composer as follows:

composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.2.0 2016-07-19 01:28:52

...
...

If this step doesn't work, visit the Composer Download page in a new browser and follow the instructions there.

Summary

In this step, you installed Composer, a dependency management tool for PHP in your Google Cloud Shell environment.

Get the WordPress helper script

In Cloud Shell on the command-line, run the following command to clone the Github repository:

git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git

Change directory into php-docs-samples/appengine/wordpress.

cd php-docs-samples/appengine/wordpress

Install dependencies with the following command:

composer install --no-dev

Run the helper script to see the usage:

php wordpress-helper.php help setup

Create your WordPress project

The helper script will help you create your WordPress project. Run the command below:

php wordpress-helper.php setup -d ${HOME}/my-wordpress-project

and follow the instructions as shown below:

$ php wordpress-helper.php setup -d ${HOME}/my-wordpress-project

We will use the directory: /home/xxxx/my-wordpress-project. If the
directory exists, we will override the contents.
Do you want to continue? (Y/n) <Hit Enter>

A directory /home/xxxx/my-wordpress-project was created.

Please select the App Engine Environment (defaults to Flexible Environment)
  [0] Flexible Environment
  [1] Standard Environment
 > <Hit Enter>

Creating a new project for: Flexible Environment

Please select the region of your Cloud SQL instance (defaults to us-central1)
  [0] us-central1
  [1] us-east1
  [2] europe-west1
  [3] asia-east1
 > <Hit Enter (choose the number if you choose the other region)>

Using a db_region: us-central1
Downloading the WordPress archive...
Downloaded the WordPress archive.
Downloading the Batcache plugin...
Downloaded Batcache plugin.
Downloading the Memcached plugin...
Downloaded Memcached plugin.
Copying drop-ins...

Please enter project_id (mandatory input):
<Type your project id and hit enter - you can find your GCP project ID on your Qwiklabs page>
Please enter db_instance (defaults to 'wp'): <Hit Enter>
Please enter db_name (defaults to 'wp'): <Hit Enter>
Please enter db_user (defaults to 'wp'): <Hit Enter>

Please enter db_password (mandatory input):
<Type your password for the database user>

Do you want to use the same db user and password for local run? (Y/n)
<Hit Enter>

...
...

Your WordPress project is ready at /home/xxxx/my-wordpress-project

Then cd into the project directory:

cd ${HOME}/my-wordpress-project

Summary

In this step, you created your WordPress project in your Google Cloud Shell VM.

Deploy your WordPress blog

Deploy the project by running the following command:

gcloud app deploy

If you prompted to choose a region, chose us-central. This command will take several minutes. After it succeeds, visit your blog by opening https://YOUR-PROJECT-ID.appspot.com/, then you'll see the WordPress installation screen like this:

Fill the following field

Then click Install WordPress. Your WordPress blog will be ready in seconds!

Configure your WordPress blog

  1. Click Plugins
  2. Click Activate under Google Cloud Storage plugin
  3. Click Settings under Google Cloud Storage plugin
  4. Type the name of your Cloud Storage bucket in Bucket name for media upload
  5. Click Save Settings

This plugin allows you to upload media files to the Cloud Storage bucket and serve from there, instead of using the local file system. Because App Engine can spawn a new server for you, the media files must be shared between all the servers.

Optional step: upload media files

Feel free to play around with the WordPress Admin UI, especially the Media uploader which is configured to upload the images to your Cloud Storage bucket. You can browse your Cloud Storage bucket on the Cloud Storage Browser.

Summary

Now you have a WordPress blog running on App Engine Flexible Environment.

You learned how to run WordPress blog on App Engine Flexible Environment.

What we've covered

Next Steps

Give us your feedback