Husband, father, photographer, lawyer, developer. https://www.instagram.com/lawrencea/
It’s still an awesome way to build a site 🔥
This is an updated guide for using Cloud Run and WordPress in 2021. Credit to Peter Kracik for his original tutorial and source code.
Here is a sample site that uses Cloud Run and WordPress: https://aritao.org.
These are the performance metrics on Lighthouse for the site:
WordPress is popular publishing tool for sites and blogs. As an open-source content management system, it is forever free to use.
Google Cloud Run is a platform for running services in containers without managing underlying hardware. Cloud Run can deploy a service that “hosts” a WordPress site.
These are the steps we will follow:
Create a project on Google Cloud Platform. Enable billing.
Activate the Google Cloud Run API, Google Cloud SQL API, and Google Cloud SQL Admin API.
Prepare the image for Cloud Run.
Set up a Cloud SQL server and database.
Deploy the container with instructions and variables.
Run the service and configure WordPress.
Connect WordPress to a storage service.
Enjoy your new, scalable blogging / site-building solution.
Create a Project On Google Cloud Platform
Activate Cloud Run API, Cloud SQL API, and Cloud SQL Admin API
produced the original source code for the solution in this guide, and it is available here. The repository contains everything you need to build a container image for use with Google Cloud Run.
Since the repository’s last update, WordPress has upgraded to 5.7.2, and PHP released version 8. Since I wanted to use those versions, I forked the original repository and updated it. By the time this is published, a pull request should be pending. Meanwhile, the fork is here:
Setup to run WordPress on Google Cloud Run. Contribute to lawrenceaph/wp-gcloud-run development by creating an account…
Use Google Cloud Shell if you don’t want to install the gcloud command line tool locally.
A quick way to create the container image is to use google cloud shell. Access it by clicking on the Activate Cloud Shell button:
Clone the repository and switch to the directory it downloads to:
git clone https://github.com/lawrenceaph/wp-gcloud-run.git cd wp-gcloud-run
Run a command to build the container and submit it to the Google Container Registry (gcr):
gcloud builds submit -t gcr.io/<project name>/<wpcloudrun>
Replace <project name> with your project name, and <wpcloudrun> with a container name of your choice. It will take a few minutes for the process to finish.
Set Up the Cloud SQL server and Database
Create a database:
Deploy the container 🎉
Use container port 80, and reduce the maximum instances. I used five. This can be increased later if your site gets high traffic.
You will need to connect the container to your SQL instance:
Supply environment variables.
Go to the URL below to generate WordPress salts. These are strings of code that will attach to passwords to increase their complexity. You will use these as environment variables.
Here’s a sample of the output.
The full set of Environment Variables are:
For the DB_HOST value, copy the SQL instance from the Overview section:
Add a colon and forward slash to the copied text:
As an additional security measure, GCP’s Secrets Manager API can store a database password as a secret and reference it in Cloud Run as an environment variable. This is an optional step, but a good practice. When stored, secrets look like this:
When used in Cloud Run, the secret displays its outward name but hides the sensitive data:
Under “Configure how this service is triggered”, select “Allow all traffic” and “Allow unauthenticated invocations”.
After deployment, a URL will become available. Click it to bring up this happy sight:
Fill in the details, log-in, and navigate to the Dashboard. Visit the plugins section and look for WP-Stateless.
Connect WordPress to a storage service.
Activate the WP-Stateless plugin:
Follow the instructions of the plugin to fully enable it. You may opt for automatic setup (requires Google authorization), or manual setup. For those who opt for manual setup, the plugin has very clear instructions. Once connected, choose a mode (“CDN” works well for me) and upload your media the usual way.
Enjoy your new, scalable blogging / site building solution! 🎉🎉🎉
Congratulations! You now have a WordPress site running as a service on Google Cloud Run. If traffic increases, Cloud Run can scale to the maximum instances specified. If traffic hits zero, Cloud Run can downscale to zero as well. Very cool.
Cost-wise, the largest expense will be the Cloud SQL database, which at lowest specs generates a 7–8 USD monthly bill. It’s not cheap, but you can use this SQL server with additional Cloud Run deployments or sites.
Next Steps 💪
You may wish to connect a domain, or practice updating themes, plugins, or even the WordPress installation itself. (The container image is read-only, so updating requires new container builds).
Update plugins and themes by placing the latest versions in the app folder and triggering a new gcloud build. A revised deployment on Cloud Run with the updated image completes the upgrade. The same process works for upgrading WordPress (but keep a copy of wp-config.php as it is tedious to re-type everything).
You may also wish to upgrade your site into a Progressive Web App. If you’re interested in a guide for doing that on Cloud Run, let me know in the comments. Cheers!