I have just added a support for App Engine Standard Environment to the WordPress helper script. In this post, we will walk through how to run WordPress on App Engine Standard Environment. Oh but which environment to choose; flexible, or standard? You may want to read The App Engine Environments before the decision.
- Create a new Cloud Project at Developers Console
- Enable Billing on that project
- Create App Engine default bucket at App Engine Settings Page
- Install Google Cloud SDK
- Install mysql locally
Configure Google Cloud SDK
Configure Google Cloud SDK with your account and the project.
$ gcloud auth login ... ... $ gcloud config set project YOUR_PROJECT_ID
Let’s configure the GCS bucket for later use. The default App Engine bucket looks like YOUR_PROJECT_ID.appspot.com. Change the default acl of that bucket as follows:
$ gsutil defacl ch -u AllUsers:R gs://YOUR_PROJECT_ID.appspot.com
Create and configure a Cloud SQL first generation instance
We will use wp for various resource names; in particular for the instance name and the database name.
Go to Cloud SQL console and create a new fist generation instance wp and create a new database wp. Go to Access Control -> Users, then change the password for root@localhost user. You will later use this password for accessing from App Engine.
Create a database and a user on local mysql
Create a database and a user on the local mysql server. This dummy database is needed for maintenance reasons, in particular, to update your plugins and themes easily. You can use this local database for running your WordPress locally, or for running a tool named wp-cli for maintenance purpose.
Assuming you have already installed and configured the mysql server, use mysql command to create a database and a user as follows (don’t use the example password below):
$ mysql -h 127.0.0.1 -u root -p mysql> create database wp; mysql> create user 'wp'@'localhost' identified by 'PASSWORD'; mysql> grant all on wp.* to 'wp'@'localhost'; mysql> exit Bye
Download and configure WordPress
We provide a convenient tool for downloading and configuring WordPress. Let’s use this tool here. First, clone one of our git repos:
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
Then cd into the `php-docs-samples/appengine/wordpress` directory and run `composer install`.
$ cd php-docs-samples/appengine/wordpress $ composer install
Then run the `wordpress-helper.php` as follows:
$ php wordpress-helper.php setup -d ~/my-wordpress-project
After answering few questions, you’ll have fully working WordPress installation at `~/my-wordpress-project` directory.
Run WordPress locally
Chdir to the project directory and run the following command to run WordPress locally:
$ cd ~/my-wordpress-project $ vendor/bin/wp server --path=wordpress
If you’re on Windows, use wp.bat:
> cd C:\my-wordpress-project > vendor/bin/wp.bat server --path=wordpress
Then access http://localhost:8080/. Finish the installation step. Login to the Dashboard and update if any of the plugins have update.
Now it’s ready for the first deployment.
$ gcloud app deploy --promote --stop-previous-version app.yaml cron.yaml
Then access your site at https://PROJECT_ID.appspot.com/ and finish the WordPress installation process (Note: please do this immediately after the first deployment).
Go to the Dashboard, and in the Plugins page, activate the App Engine WordPress plugin and Batcache Manager plugin. Try uploading a media and confirm the image is uploaded to the GCS bucket.
Check if Batcache is working
In the plugin page, you should see these 2 drop-ins are activated as follows:
To make sure it’s really working, you can open an incognito window and visit the site because the cache plugin only serves from cache to anonymous users. Then go to the memcache dashboard in the Cloud Console and check the hit ratio and number of items in cache.
Because the wp-content directory on the server is read-only, you have to do this locally. Run WordPress locally (`vendor/bin/wp server –path=wordpress`) and update plugins/themes in the local Dashboard, then deploy, then activate them in the production Dashboard. You can also use the wp-cli utility in the project’s vendor directory as follows (you have to run Cloud SQL Proxy or local mysql server for this tool to work):
# To update all the plugins $ vendor/bin/wp plugin update --all --path=wordpress # To update all the themes $ vendor/bin/wp theme update --all --path=wordpress
First Deactivate them in the production Dashboard, then remove them completely locally. The next deployment will remove those files from the production environment.
Update WordPress itself
Most of the case, just download the newest WordPress and overwrite the existing wordpress directory. It is still possible that the existing config files are not compatible with the newest WordPress, so please update the config file manually in that case.
We walked though how to run WordPress on App Engine standard environment. It performs well because we’re leveraging Memcached service provided by App Engine. Also thanks to the App Engine autoscaler, Google will automatically spin up new instances when your site is getting lots of traffic.
If you need to run new WordPress site, please give this guide a try and let us know what you think.