Hosting WordPress in the App Engine Flexible Environment

This tutorial shows how to deploy a small WordPress site to the Google App Engine flexible environment.

Objectives

  • Create a second generation Google Cloud SQL instance.
  • Configure a sample WordPress site.
  • Deploy the sample WordPress site to the App Engine flexible environment.

Costs

This tutorial uses billable components of Cloud Platform including Google Compute Engine and Cloud SQL.

New Cloud Platform users might be eligible for a free trial.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform project.

    Go to the Projects page

  3. Enable billing for your project.

    Enable billing

  4. Enable the required APIs.

    Enable the APIs

  5. Install and initialize the Cloud SDK.
  6. Create a new service account. Download the JSON key file and save it in a secure place.
  7. Install PHP and Composer.
  8. Download Cloud SQL Proxy and make it executable. Also add the location of the Cloud SQL Proxy executable file to your PATH environment variable.
  9. Install a MySql client and verify that the location of the mysql executable file is in your PATH environment variable.

Creating and configuring a Cloud SQL Second Generation instance

  1. Create a new Cloud SQL Second Generation instance:

    gcloud sql instances create tutorial-sql-instance \
        --activation-policy=ALWAYS \
        --tier=db-n1-standard-1
    
  2. Set the root password for your instance:

    gcloud sql instances set-root-password tutorial-sql-instance \
        --password [YOUR_SQL_ROOT_PASSWORD]
    

    where [YOUR_SQL_ROOT_PASSWORD] is a secure password of your choice.

  3. Run Cloud SQL Proxy:

    cloud_sql_proxy \
        -dir /tmp/cloudsql \
        -instances=[YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance=tcp:3306 \
        -credential_file=[PATH_TO_YOUR_SERVICE_ACCOUNT_JSON]
    

    where

    • [YOUR_PROJECT_ID] is your project ID.

    • [PATH_TO_YOUR_SERVICE_ACCOUNT_JSON] is the path to the service account JSON file that you downloaded previously.

    The output should indicate that the proxy is ready for new connections:

    Listening on 127.0.0.1:3306 for [YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance
    Ready for new connections
    
  4. In another terminal window, create a new database and a user:

    mysql -h 127.0.0.1 -u root --password=[YOUR_SQL_ROOT_PASSWORD]
    mysql> create database tutorialdb;
    mysql> create user 'tutorial-user'@'%' identified by '[YOUR_DATABASE_PASSWORD]';
    mysql> grant all on tutorialdb.* to 'tutorial-user'@'%';
    mysql> exit
    

    where:

    • [YOUR_SQL_ROOT_PASSWORD] is the root password for your Cloud SQL instance.
    • [YOUR_DATABASE_PASSWORD] is a secure password of your choice.

Setting up the WordPress project

  1. Clone the sample repository:

    git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
    
  2. Go to the directory that contains the sample code:

    cd php-docs-samples/appengine/wordpress
    
  3. Install dependencies:

    composer install
    
  4. Run the helper script:

    php wordpress-helper.php setup -n \
        -d ./wordpress-project \
        --db_instance=tutorial-sql-instance \
        --db_name=tutorialdb \
        --db_user=tutorial-user \
        -p [YOUR_PROJECT_ID] \
        --db_password=[YOUR_DATABASE_PASSWORD]
    

    where:

    • [YOUR_PROJECT_ID] is your project ID.
    • [YOUR_DATABASE_PASSWORD] is your database password.

    The -d parameter specifies the location of your WordPress project.

  5. The helper script writes information to wordpress-project/wordpress/wp-config.php. Inspect the content of wp-config.php to verify that your names, project ID, and database password are correct.

    if ($onGae) {
        / Production environment */
        define('DB_HOST', ':/cloudsql/[YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance');
        / The name of the database for WordPress /
        define('DB_NAME', 'tutorialdb');
        / MySQL database username */
        define('DB_USER', 'tutorial-user');
        / MySQL database password /
        define('DB_PASSWORD', '[YOUR_DATABASE_PASSWORD]');
    } else {
        / Local environment */
        define('DB_HOST', '127.0.0.1');
        / The name of the database for WordPress /
        define('DB_NAME', 'tutorialdb');
        / MySQL database username */
        define('DB_USER', 'tutorial-user');
        / MySQL database password /
        define('DB_PASSWORD', '[YOUR_DATABASE_PASSWORD]');
    }
    

Deploying the WordPress project to the App Engine flexible environment

  1. Go to your WordPress project directory:

    cd wordpress-project
    
  2. Deploy the WordPress project:

    gcloud app deploy
    
  3. Access the site and install WordPress:

    gcloud app browse
    

    This command opens a browser and displays a WordPress installation page. Follow the instructions on the page.

    As an alternative to entering the preceding command, enter this URL in a browser:

    https://[YOUR_PROJECT_ID].appspot.com
    

    where [YOUR_PROJECT_ID] is your project ID.

Updating WordPress, plugins, and themes

It's important to keep WordPress, plugins and themes up to date. You can keep these items updated by using the wp tool. After an update, you need to re-deploy the WordPress project.

  1. Update WordPress itself:

    vendor/bin/wp core update --path=wordpress
    
  2. Update plugins:

    vendor/bin/wp plugin update --all --path=wordpress
    # Just in case it updates any of the dropins, copy the files:
    cp wordpress/wp-content/plugins/batcache/advanced-cache.php \
        wordpress/wp-content/plugins/memcached/object-cache.php \
        wordpress/wp-content
    
  3. Update themes

    vendor/bin/wp theme update --all --path=wordpress
    
  4. Deploy the project again:

    gcloud app deploy
    

Cleaning up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:

Delete the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

To delete the project:

  1. In the Cloud Platform Console, go to the Projects page.

    Go to the Projects page

  2. In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click
      Delete project
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Delete non-default versions of your app

If you don't want to delete your project, you can reduce costs by deleting the non-default versions of your app.

To delete an app version:

  1. In the Cloud Platform Console, go to the App Engine Versions page.

    Go to the Versions page

  2. Click the checkbox next to the non-default app version you want to delete.
  3. Click the Delete button at the top of the page to delete the app version.

Delete your Cloud SQL instance

To delete a Cloud SQL instance:

  1. In the Cloud Platform Console, go to the SQL Instances page.

    Go to the SQL Instances page

  2. Click the name of the SQL instance you want to delete.
  3. Click the Delete button at the top of the page to delete the instance.

What's next

Learn how to run the PHP Bookshelf sample in the App Engine flexible environment.

Learn how to run the PHP Bookshelf sample on Container Engine.

Learn how to run the PHP Bookshelf sample on Compute Engine.

Learn more about WordPress.

Learn about using WordPress multisite with your own domains and Mailgun.

Try out other Google Cloud Platform features for yourself. Have a look at our tutorials.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...