Google App Engine

Running WordPress in App Engine

In this tutorial, we will show you how to install WordPress, how to to test it with AppEngine on your local development environment, and how to deploy a simple WordPress installation to the Cloud Platform. This tutorial assumes you are already reasonably comfortable deploying PHP applications to App Engine, and comfortable working with WordPress and MySQL in other environments.

Prerequisites

Before beginning this tutorial, please make sure you have completed the following steps.

  • You should have downloaded and installed the Google App Engine for PHP SDK for Microsoft Windows, Mac OS X or Linux
  • You should have installed a local copy of MySQL 5 in your development environment
  • You should have created a Cloud Console project, and have enabled billing on the project (this is necessary to create a Cloud SQL instance)

This tutorial will also assume you know the following file paths. As these can vary depending on how you installed the SDK, they may be specific to your environment.

  • APP_ENGINE_SDK_PATH - This is the path to the App Engine SDK you have installed on your development environment
  • YOUR_PROJECT_ID - This is the Project ID of the Cloud Console project you created. A project ID typically contains lowercase characters and dashes (-) but not uppercase characters or spaces.

Installing WordPress on your development environment

Before deploying WordPress to the Cloud Platform, we will run it locally on our development server. Running WordPress locally allows you to to install and test any custom plugins and themes.

Step 1. Set up your local application

We'll start by creating an App Engine application to run WordPress. If you haven't already, create an empty folder to contain your application. We'll refer to the path of this folder as APPLICATION_DIRECTORY for the rest of this tutorial.

Next, create a file in the newly created directory called app.yaml, and populate with the following text. Be sure to substitute YOUR_PROJECT_ID with your actual project ID.

application: YOUR_PROJECT_ID
version: wp
runtime: php
api_version: 1

handlers:
- url: /(.*\.(htm|html|css|js))$
  static_files: wordpress/\1
  upload: wordpress/.*\.(htm|html|css|js)$
  application_readable: true

- url: /wp-content/(.*\.(ico|jpg|png|gif))$
  static_files: wordpress/wp-content/\1
  upload: wordpress/wp-content/.*\.(ico|jpg|png|gif)$
  application_readable: true

- url: /(.*\.(ico|jpg|png|gif))$
  static_files: wordpress/\1
  upload: wordpress/.*\.(ico|jpg|png|gif)$

- url: /wp-admin/(.+)
  script: wordpress/wp-admin/\1
  secure: always

- url: /wp-admin/
  script: wordpress/wp-admin/index.php
  secure: always

- url: /wp-login.php
  script: wordpress/wp-login.php
  secure: always

- url: /wp-cron.php
  script: wordpress/wp-cron.php
  login: admin

- url: /xmlrpc.php
  script: wordpress/xmlrpc.php

- url: /wp-(.+).php
  script: wordpress/wp-\1.php

- url: /(.+)?/?
  script: wordpress/index.php

Next we need to add a php.ini file to enable a couple of methods that WordPress uses. Like the app.yaml file, this should sit in the top level directory of your project. The file needs to contain one line.

google_app_engine.enable_functions = "php_sapi_name, gc_enabled"
upload_max_filesize = 8M

Lastly we need to add a cron.yaml file to invoke scheduled tasks on WordPress. Like the app.yaml and php.ini file, this also should sit in the top level directory of your project. Using a text editor, add the following text to the file:

cron:
- description: wordpress cron tasks
  url: /wp-cron.php
  schedule: every 2 hours

This will trigger the wp-cron.php script every two hours from App Engine, which means WordPress will attempt scheduled tasks like scheduled posts every two hours. If you would like more frequent attempts, you can adjust this frequency as described in the scheduled tasks documentation.

Step 2. Download WordPress

Download the latest version of WordPress from wordpress.org. This tutorial was tested with WordPress 3.5.1. Unpack the archive and place it into a new folder called wordpress inside your APPLICATION_DIRECTORY.

Google does not maintain WordPress or verify the integrity or safety of any software provided by WordPress.org. Please exercise caution and judgment when working with any third party code.

Step 3. Create your WordPress user and database on your local MySQL instance

We will create a database on our local MySQL server, and a user to allow WordPress to connect to database (and later to Cloud SQL).

As Cloud SQL will provide its own security features between your database and your App Engine Application, we can use the same username and password in production as we do on our development environment.

Make sure your local MySQL server is running. Connect to your local MySQL server as the root user and run the following:

CREATE DATABASE IF NOT EXISTS wordpress_db;
CREATE USER 'wp_user'@'localhost' IDENTIFIED BY 'wp_password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wp_user'@'localhost';

wordpress_db, wp_user and wp_password are placeholders and should be changed to match your settings.

Step 4. Create your wp-config.php configuration file

In your APPLICATION_DIRECTORY/wordpress folder you should find a file called wp-config-sample.php. Create a copy of this file and call it wp-config.php. Place it in the same directory as the original file.

Then edit the file in a text editor, and change the following lines:

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

/** MySQL database username */
define('DB_USER', 'username_here');

/** MySQL database password */
define('DB_PASSWORD', 'password_here');

to settings used in Step 3

/** The name of the database for WordPress */
define('DB_NAME', 'wordpress_db');

/** MySQL database username */
define('DB_USER', 'wp_user');

/** MySQL database password */
define('DB_PASSWORD', 'wp_password');

Depending on where and you have installed your MySQL server, you may also need to change the DB_HOST constant as well. Refer to the WordPress documentation for more details.

Step 5. Run WordPress Locally

Now we're ready to run WordPress locally on App Engine. To do this, just start up your Development Server and point it to the application you just created. On Linux and Mac OS X this command looks like the following:

$ APP_ENGINE_SDK_PATH/dev_appserver.py APPLICATION_DIRECTORY

Once you've started your application, you should see a line of output listing the address of the default server (this can vary, but is usually http://localhost:8080/). Visit this URL and if the previous steps were successful you should be presented with the setup screen for WordPress. You can continue to set up WordPress and install plugins if you wish.

Step 6. Set up your Cloud SQL instance

We will need a Cloud SQL instance to store our hosted WordPress database. To set this up, visit your billing-enabled project in the Google Cloud Console, visit the Cloud SQL section, and choose New Instance. You should see a screen similar to the following.

Most of these options can be left as their defaults.

  • Instance ID you'll use this to connect to your instance from PHP. You can call it whatever you like. In this tutorial we'll call it wordpress
  • Tier will vary depending on how much traffic you expect your blog to receive and what plugins you install. For the purposes of this tutorial though, a D0 should be fine.
  • Authorized App Engine Applications - for a new Cloud Console Project, this should be automatically populated with an App Engine Application with the name of your project. If not, you should add it here.

Once you've created your instance, you'll need to create the appropriate database and user by running the same SQL commands that you ran on your local instance in Step 3. There are several ways to run SQL in a Cloud SQL instance (for full details, see the Cloud SQL documentation). One way is to import the commands from a file on Google Cloud Storage. We've already set up a public file on Cloud Storage that contains the same SQL as in Step 3 (see http://storage.googleapis.com/appengine-php/setup.sql).

To import the file into your new Cloud SQL instance, click on your instance ID from the Cloud SQL dashboard and, while in the Summary section choose Import.... In the dialog that appears, enter gs://appengine-php/setup.sql into the field Cloud Storage Path. Leave Into database empty. You can quickly verify if this step worked by checking in the Operations Log section of your instance dashboard (you should see Import Succeeded).

Step 7. Make your WordPress application Cloud SQL aware

Before uploading your application to App Engine, we'll need to make a tweak to the wp-config.php file so that when hosted on App Engine it will connect to your Cloud SQL instance rather than attempt to connect to a local instance.

Find the line in your wp-config.php file that reads define('DB_HOST', 'localhost');, and change it to the following:

if(isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'],'Google App Engine') !== false) {
  define('DB_HOST', ':/cloudsql/YOUR_PROJECT_ID:wordpress');
}else{
  define('DB_HOST', 'localhost');
}

Before continuing, you may wish to repeat Step 5 quickly to make sure your installation still works locally.

Step 8. Deploy your WordPress installation to App Engine

Okay! Now we're ready to push our blog up to Google App Engine. This step will deploy any PHP code in your local project to App Engine, however any information in your local MySQL database (such as configuration changes or content that you might have set up on your local installation) will NOT be copied up to App Engine.

To deploy an application to App Engine, just use the appfcg.py command from the App Engine PHP SDK as described in Uploading your Application. The command is:

APP_ENGINE_SDK_PATH/appcfg.py update APPLICATION_DIRECTORY

This will read your app.yaml file, ask you for your username and password, and then push your code to App Engine.

If all went well, you should now have a WordPress blog on App Engine running at the URL wp-dot-YOUR_PROJECT_ID.appspot.com. Congratulations!

Step 9. Optimising your WordPress Blog

Now that you've succesfully deployed your WordPress blog, let's look at some ways in which you can optimise your installation to work well with App Engine. We'll do this by installing three useful plugins.

  • Google App Engine for WordPress - This plugin extends WordPress to work well with Google App Engine, including sending mail via the App Engine Mail API, and storing and serving uploaded files via Google Cloud Storage
  • Memcache Object Store - This will speed up your WordPress installation by storing frequently accessed data in Memcache. This plugin automatically takes advantage of Google App Engine's Memcache service.
  • Batcache - The Batcache plugin uses Memcache to store and serve cached versions of fully rendered pages, which speeds up your site.

Google does not develop or maintain the Memcache Object Store or Batcache plugins, and cannot verify the integrity or safety of this software. Please exercise caution and judgment when working with any third party software.

Installing Google App Engine for WordPress Plugin

The Google App Engine for WordPress plugin extends WordPress to take advantage of several App Engine services, including using the Mail API to send e-mail notifications, and Google Cloud Storage to store and serve uploaded media such as images. There are several different ways to install plugins - in this step we're going to show just one.

The following steps should be performed on your local development copy of WordPress

  • In the Admin section of the WordPress installation in your local development environment, visit Plugins > Add New > Search
  • Search for the plugin called Google App Engine for Wordpress. Once you've found it, choose Install Now.

Installing the Memcached Object Cache Plugin

The following steps should be performed on your local development copy of WordPress

  • In the Admin section of the WordPress installation in your local development environment, visit Plugins > Add New > Search
  • Search for the plugin called Memcached Object Cache. Once you've found it, choose Install Now

In your wordpress installation directory, find the file APPLICATION_DIRECTORY/wordpress/wp-content/plugins/memcached/object-cache.php and copy it to APPLICATION_DIRECTORY/wordpress/wp-content/object-cache.php

Installing the Batcache Plugin

The following steps should be performed on your local development copy of WordPress

  • Download the latest version of Batcache
  • Follow the Batcache installation instructions to install it on your local blog.
  • In your wp-config.php file, add the following lines at the end of the file:

    $batcache = [
      'seconds'=>0,
      'max_age'=>30*60, // 30 minutes
      'debug'=>false
    ];
    

Update your blog

Now that you've installed the plugins, you can deploy them using the same appcfg.py update command that you used in Step 7. Once you've done this, log in to the administration console of your WordPress blog on App Engine and check that the plugins are listed there.

Create your Cloud Storage Bucket

We will need to create a Cloud Storage bucket that will be used by your WordPress blog to efficiently store and serve the files that you upload to WordPress.

To set this up, visit your billing-enabled project in the Google Cloud Console. Visit the App Engine section, which should take you to the App Engine dashboard for your App Engine app. Visit the Application Settings section and make a note of the Service Account Name for your application. You will need this in a subsequent step. The service account name looks like an e-mail address, such as hello-php-gae@appspot.gserviceaccount.com.

Next, visit the Cloud Storage section of your Cloud Console project, and choose New Bucket. Give the bucket a unique name. In this example, we've called our bucket hello-php-gae-files but you should call yours something different.

After you've created the bucket, it should appear in the list of buckets associated with your project. Select the checkbox next to the bucket name so that the Bucket Permissions bucket appears. Add a user with the same e-mail address as the Service Account Name for your application that you noted above, and give it the Read/Write permission. This will allow your application to be able to read and write to the GCS bucket. Save the permissions.

Enable the plugins on your blog

The following steps should be performed on your hosted copy of WordPress on App Engine

Now, we just need to activate the Google App Engine for WordPress plugin and configure it. Log into the WordPress administration section of your blog, and visit the Plugins section. Activate the Google App Engine for Wordpress plugin.

Now visit Settings > App Engine. Enable the App Engine mail service - this will use the App Engine Mail API to send notifications from WordPress. Optionally, enter a valid e-mail address that mail should be sent from (if you leave this blank, the plugin will determine a default address to use). The address of the account you used to the create the Cloud Console project should work. Under Upload Settings, put in the name of the bucket you created above. Hit Save Changes to commit everything.

Congratulations! You should now have a blog that loads faster, can send mail, and can support adding images and other media to blog posts!

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.