Scalable WordPress with Google App Engine

It’s important when developing a site that it’s not specifically tied to one hosting platform. Here at Big Bite we use a range of services. Perhaps a small site might start out on a DigitalOcean droplet, then after a few months of growth it needs more resources and we decide to move it onto AWS. This has always been the logic behind building a starter kit like Suzie. In this blog I want to cover using WordPress on Google App Engine (GAE). If you’re in the process of deciding where to host your WordPress site you may also find my previous post useful: Scalable WordPress with AWS.

Caveats

With most complex set ups there some drawbacks – and some of these could be deal breakers:

  • Plugins can’t be installed on the production site – plugins must be committed to git and deployed.
  • Some plugins may not be compatible, as there’s no permanent filesystem.
  • Doesn’t directly deploy from Git
  • Doesn’t support pre-deployment commands for example: Composer

Here at Big Bite, we believe it to be good practice to test plugins on a staging server before moving them into production, so the first point isn’t a big concern for us. If a plugin requires that its settings are stored in a config file, simply committing that file to the repository solves the issue.

As for the last two points, these really aren’t much of issue and any risk associated with these can be avoided using a CI like Travis.

Google Cloud Platform

Sign in to your GCP account, at which point you should be presented with the main dashboard. The first step is to create a project, depending on if you’ve ever used GCP before you will either be faced with a New Project modal, or you will need to click on the dropdown beside the logo and select Create Project.

GCP Create Project

Give your project a name, I’ll set mine up as Codeshack

GCP Project Name

Google Cloud Storage

Once the project has been created we will want to find the Google Cloud Storage (GCS) bucket that has been created for it. GCS is Google’s answer to S3, but better, as a CDN is automaically added to your images. On top of this you can request images at any size (includng cropping) and GCS will deliver them on the fly. Let’s visit Storage Browser, your bucket will be named project-id.appspot.com. Make a note of the name and select the Settings tab, followed by Interoperability. We need to create some access keys, but first we need to Enable Interoperability Access.

GCS Enable Interoperability Access

Then create your keys by selecting Create a new key, and make a note of them as you will need them later.

GCS Create a new key

Google Cloud SQL

It’s time to add a database to the project, if you do have more one project on Google Cloud Platform remember to make sure you’re still on the correct project shown near the GCP logo. As this project should have no instances you’ll be shown a modal, select Create Instance.

GCSQL Create Instance

You will be given a choice of which instance type you want, we want the newest type Second Generation, go ahead a select Choose Second Generation

GCSQL Choose Second Generation

Instances don’t automaically have an empty database set up on them so we will want to create one, or import an existing site database. In order for us to do this we will need remote access, but instead of anyone having access we can add our IP to the allow list. Under the section Authorized Networks select + Add Network and enter your IP address.

GCSQL Authorized Networks

We’re ready to create the instance so go ahead and select Create. Once it’s finishing creating select on Access Control tab just under the instance name. We need to change the root password to something we know. Select Change root password and enter a password (remember to make a note of it).

GCSQL Change Root Password

Finally we have two pieces of information we need to make note of about the instance; the IP address (listed as IPv4 address), and the database host (listed as Instance connection name).

GCSQL Connection Information

Now we need to create an empty database or import one. I’m going to create an empty database using the MySQL command line tool, but feel free to use whatever tool your comfortable with. For example, Sequel Pro is an excellent choice if using a Mac.

Running in terminal replace 199.199.244.244 with your instance IP.

$ mysql --host=199.199.244.244 --user=root β€”password

Next you’ll be asked for the password, enter the root password you created earlier. Once connected replace codeshack with your desired database name.

create database codeshack; exit;

Configure Our Code

At this stage, with most of the set up done, we’re almost ready to deploy β€” we just need to configure our code. I’m assuming you already have Suzie set up; if not go check it out. To deploy to Google App Engine we need to install their CLI. It’s easier to install it with Homebrew, alternatively
you can visit Google Cloud SDK

$ brew install Caskroom/cask/google-cloud-sdk

In terminal go to your project folder and run gcloud init

$ cd ~/projects/codeshack
$ gcloud init

If you’ve never used the Google Cloud SDK before you will be asked to sign in. After which select your project from the list. Now it’s time for us to add two files in the root of our project to tell Google App Engine how handle our WordPress site. First of these is familiar file php.ini, use the following replacing project-id.appspot.com with your GCS bucket name from earlier. Note this file is also available to view on GitHub

; Standard PHP Settings
extension = "curl.so"
allow_url_include = "1"
upload_max_filesize = 8M
display_errors = 1
; Google Settings
google_app_engine.enable_functions = "php_sapi_name, gc_enabled"
google_app_engine.allow_include_gs_buckets = "project-id.appspot.com"
google_app_engine.disable_readonly_filesystem = 1

Next is the app.yaml where we include our Storage and SQL information. As the file is pretty large I recommendation viewing it on Github. We’ll work through the necessary sections, starting with Database:

DB_NAME: "suzie" #Set to your desired database name in the section Google Cloud SQL
DB_USER: "root" #Leave as root
DB_PASSWORD: "password" #Set as root password created during the section Google Cloud SQL
DB_HOST: ":/cloudsql/project-id:us-central1:db-instance-name" #Use Instance connection name from the section Google Cloud SQL

Next up we have the GCS (Google Cloud Storage):

SYNC: "gcs" #Leave as gcs
GCS_KEY: "XXXXXXXXX" #Use keys created during Google Cloud Storage section
GCS_SECRET: "xxxxxxxxxxxxxxxxx" #Use keys created during Google Cloud Storage section
GCS_SERVE: "true" #Leave as true
GCS_BUCKET: "project-id.appspot.com" #Set to your bucket name found during Google Cloud Storage section

An important note on GCS_SERVE; this option allows you to switch between getImageServingUrl when set to true or getPublicUrl when set to false. When running on Google App Engine it should be set to true as it acts like a CDN. The only time you want it set to false is if you need work locally and still connect to the GCS bucket.

The last item we need to set is the SITE_URL which should be the same as your bucket name (adding the http://), or alternatively you can confirm by visiting App Engine and in the top right corner.

GAE Site Url

Ready to Deploy

We’re finally ready to deploy, one of the caveats from the beginning of the post is important. As Google App Engine doesn’t deploy from Git and doesn’t support pre-deployment commands, like Composer, you will need to ensure your project directory where you will run the deploy command already had a composer install completed in the directory. In short, it has a vendor folder. Google App Engine simply copies all files in the project directory.

Go ahead run the deploy.

$ gcloud app deploy

Closing

I’m very impressed with the Google setup and would be open to using in production. Please be aware that Google App Engine support in Suzie has only been used in test conditions, so take some time testing to ensure you’re happy with the preformance before using it on a production site. It’s also worth noting that if you don’t want to use Suzie, Google has an offical WordPress plugin – infact about half of Suzie’s GCS implementation is based on it.

There are some areas I haven’t covered as I felt they could be easily added yourself:

If you happen to know a better way to set up WordPress on Google App Engine, please drop me email. Thanks for reading.

Want to know a little more about us?

We are a friendly bunch and love talking all things WordPress. Whether you’re using one of our open source projects, want to discuss a blog article or hire us for your next project, we would love to hear from you.

Contact us