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) to get an overview of PHP and learn ways to run PHP apps on GCP.
- Create a project in the Google Cloud Platform Console.
- Enable billing for your project.
- Install the Google Cloud SDK.
- Enable Cloud SQL API.
- Install 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.
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-n1-standard-1for 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 for more details.
Create the database you want your WordPress site to use:
$ gcloud sql databases create wordpress --instance wordpress
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
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.
$ composer require google/cloud-tools
Note: If you receive an error about extensions, install
zipPHP extensions and retry.
Now you can run the
wp-gaecommand which is included in that package:
$ php vendor/bin/wp-gae
Note: You can also install
google/cloud-toolsglobally, which will allow you to execute the command
wp-gae command will ask you several question in order to set up your Cloud SQL
database connection, and then write the required configuration to your
configuration file. It also copies the following files into your project directory
to allow WordPress to run on Google App Engine:
app.yaml: The App Engine configuration file that specifies the runtime and static asset handlers.
cron.yaml: The App Engine configuration file that ensures
wp-cron.phpis run every 15 minutes.
php.ini: For setting PHP configuration in App Engine specific to WordPress.
gae-app.php: The Front Controller, which is required for all App Engine applications.
Create a new WordPress project
To download WordPress and set it up for GCP, run the
$ 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
Note: To determine the region your database is in, use the
gcloud sql instances describe wordpress command.
$ gcloud sql instances describe wordpress | grep region
Update an existing WordPress Project
If you are migrating an existing project to Google Cloud, you can use the
$ php vendor/bin/wp-gae update path/to/your-wordpress-site
The command asks you several questions. After you answer them, your existing
project will contain the required files for deploying to App Engine, as well
as an updated
wp-config.php file. As this command overwrites
be sure to verify the changes are correct before deploying.
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
NOTE: If you receive any error in your application, such as "Error estabilishing
a database connection", set the
WP_DEBUG constant to
Enable the Google Cloud Storage plugin
To use the Google Cloud Storage plugin for media uploads, follow these steps:
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
Go to the Dashboard at
https://YOUR_PROJECT_ID.appspot.com/wp-admin. On the Plugins page, activate the
Google Cloud Storage plugin.
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.
To access this MySQL instance, use Cloud SQL Proxy. Download it to your local computer and make it executable.
Go to the the 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 \ -instances=YOUR_PROJECT_ID:us-central1:wordpress=tcp:3306 \ -credential_file=/path/to/YOUR_SERVICE_ACCOUNT_JSON_FILE.json
Note: See Connecting to Cloud SQL from External Applications for more options when running the Cloud SQL proxy.
Now you can access the Cloud SQL instance with the MySQL client in a separate command-line tab.
$ mysql --host=127.0.0.1 -u root -p mysql> use database wordpress; mysql> show tables; mysql> exit
Install and update WordPress, plugins, and themes
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
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.