This tutorial shows how to deploy a small WordPress site to the App Engine flexible environment.
Creating and configuring a Cloud SQL Second Generation instance
Create a Cloud SQL Second Generation instance:
gcloud sql instances create tutorial-sql-instance \ --activation-policy=ALWAYS \ --tier=db-n1-standard-1 \ --region=us-central1
Set the root password for your instance:
gcloud sql users set-password root --instance tutorial-sql-instance \ --password [YOUR_SQL_ROOT_PASSWORD] \ --host %
where
[YOUR_SQL_ROOT_PASSWORD]
is a secure password of your choice.Download and run the Cloud SQL Proxy:
./cloud-sql-proxy \ -dir /tmp/cloudsql \ -instances=[YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance \ -credential_file=[PATH_TO_YOUR_SERVICE_ACCOUNT_JSON]
where
[YOUR_PROJECT_ID]
is your Google Cloud project ID.[PATH_TO_YOUR_SERVICE_ACCOUNT_JSON]
is the path to the service account JSON file that you downloaded previously.
The following output indicates 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
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
Clone the sample repository:
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
Go to the directory that contains the sample code:
cd php-docs-samples/appengine/flexible/wordpress
Install dependencies:
composer install
Run the helper script:
php wordpress.php setup -n \ --dir=./wordpress-project \ --db_instance=tutorial-sql-instance \ --db_name=tutorialdb \ --db_user=tutorial-user \ --project_id=[YOUR_PROJECT_ID] \ --db_password=[YOUR_DATABASE_PASSWORD]
where:
[YOUR_PROJECT_ID]
is your project ID.[YOUR_DATABASE_PASSWORD]
is your database password.
The
-dir
parameter specifies the location of your WordPress project.The helper script writes information to
wordpress-project/wordpress/wp-config.php
. Inspect the content ofwp-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
Go to your WordPress project directory:
cd wordpress-project
Deploy the WordPress project:
gcloud app deploy \ --promote --stop-previous-version app.yaml cron.yaml
In your browser, enter the following URL:
https://PROJECT_ID.REGION_ID.r.appspot.com
Replace the following:
PROJECT_ID
: Your Google Cloud project IDREGION_ID
: A code that App Engine assigns to your app
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.
Update WordPress itself:
vendor/bin/wp core update --path=wordpress
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
Update themes:
vendor/bin/wp theme update --all --path=wordpress
Deploy the project again:
gcloud app deploy \ --promote --stop-previous-version app.yaml cron.yaml