This is a small command line tool for downloading and configuring WordPress for Google Cloud Platform. The script allows you to create a working WordPress project for App Engine standard environment or App Engine flexible environment.
- Install Composer
- Create a new Cloud Project on Developers Console
- Enable Billing on that project
- Create App Engine default bucket at App Engine Setting Page
- Install Google Cloud SDK
- Install mysql
- Enable Cloud SQL API (For App Engine flexible environment)
Configure Google Cloud SDK with your account and the Project.
$ gcloud auth login
...
...
$ gcloud config set project YOUR_PROJECT_ID
Then configure the App Engine default 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
If you will use App Engine flexible environment, create a Cloud SQL 2nd generation instance, and if you will use App Engine standard environment, create a Cloud SQL 1st generation instance.
In this guide, we use wp for various resource names; the instance
name, the database name, and the user name.
Go to the SQL settings in the Cloud Console and create
an instance wp and database named wp. Go to the Access Control ->
Users, then change the password for root@localhost. You will use
this password for accessing from App Engine application.
Also create the wp database in the local mysql server. The local
mysql instance is required to run wp-cli tool for
installing/upgrading plugins and themes.
You can create a new Cloud SQL Second Generation instance with the following command:
$ gcloud sql instances create wp \
--activation-policy=ALWAYS \
--tier=db-g1-small
Then change the root password for your instance:
$ gcloud sql instances set-root-password wp \
--password YOUR_INSTANCE_ROOT_PASSWORD # Don't use this password!
To access this MySQL instance, we’ll use Cloud SQL Proxy. Please download an appropriate binary from the download page, make it executable.
If you haven’t created a service account for the project, please create it on the Credentials section in the Console (Choose a new service account). Download the JSON key file and save it in a secure place.
Run the proxy by the following command:
$ cloud_sql_proxy \
-dir /tmp/cloudsql \
-instances=YOUR_PROJECT_ID:us-central1:wp=tcp:3306 \
-credential_file=PATH_TO_YOUR_SERVICE_ACCOUNT_JSON
Now you can access to the Cloud SQL instance with the normal MySQL client. Please create a new database and a user as follows:
$ mysql -h 127.0.0.1 -u root -p
mysql> create database wp;
mysql> create user 'wp'@'%' identified by 'PASSWORD'; // Don't use this password!
mysql> grant all on wp.* to 'wp'@'%';
mysql> exit
Bye
In the above example, I created a new database wp and a new user wp.
First install the dependencies in this directory as follows:
$ composer install
If it complains about extensions, please install phar and zip PHP
extesions and retry.
Then run the helper command.
$ php wordpress-helper.php setup
The command asks you several questions, please answer them. Then
you'll have a new WordPress project. By default it will create
my-wordpress-project in the current directory.
If you chose the flexible environment, skip this step.
This step will create a basic database setup in your local mysql
server. This is required to use wp-cli tool.
CD into your WordPress project directory and run the following command to run WordPress locally (be sure to keep the cloud SQL proxy running):
$ cd my-wordpress-project
$ vendor/bin/wp(.bat) server --path=wordpress
Then access http://localhost:8080/. Follow the installation steps, create the admin user and its password. Login to the Dashboard and update if any of the plugins have update.
Now it’s ready for the first deployment.
You can deploy your WordPress project by the following command.
$ gcloud app deploy \
--promote --stop-previous-version app.yaml cron.yaml
Then access your site, and continue the installation step. The URL is: https://PROJECT_ID.appspot.com/
Go to the Dashboard, and in the Plugins page, activate the following plugins:
- For standard environment
- App Engine WordPress plugin (also set the e-mail address in its setting page)
- Batcache Manager
- For flexible environment
- Batcache Manager
- GCS media plugin
After activating the plugins, try uploading a media and confirm the image is uploaded to the GCS bucket.
On the plugin page in the WordPress dashboard, you should see 2
drop-ins are activated; advanced-cache.php and object-cache.php.
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 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 as follows:
# 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.
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 sometimes release the security update for the php-docker image. Then you’ll have to re-deploy your WordPress instance to get the security update.
Enjoy your WordPress installation!
