All Things Cloud, Google Cloud Platform, Google Cloud Run

HOWTO: A Google Cloud Run Container Managed WordPress Site with Cloud SQL Backend

1 0
Share This Content
Read Time:5 Minute, 24 Second

Example Site: WordPress Google Cloud Run managed container instance web service (fully Google managed underlying container infrastructure) connected with Cloud SQL backend:

Credit and thanks to Lawrence Aritao for original content and example:

https://lawrence.aritao.dev/deploying-wordpress-on-cloud-run-in-2021-d2f3c58b039b

Below is another sample WordPress site that uses the Google Cloud Run service using a WordPress containerized installation with a Cloud SQL connected backend:

https://carlscloud-wp-v2-r2tsycvj3q-uc.a.run.app and with a web service domain mapping : https://blog.carlscloud.com

Domain mapping link documentation:  https://cloud.google.com/run/docs/mapping-custom-domains

A stateless docker built container WordPress installation with no worries on management of underlying resource infrastructure as Google Cloud Run automatically scales containers on demand and manages the number of maximum container instances to spin up for each deployed web service instance.

*You are only billed for the time container is “requested”. 

Cloud Run pricing here:  https://cloud.google.com/run/pricing

May 2021 : Google Cloud Run Pricing Breakdown

Google Cloud Run Documentation:  https://cloud.google.com/run/docs

https://www.cloudnerve.com/2021/05/24/updated-deploying-wordpress-on-cloud-run-in-2021/

Credit to Peter Kracik for his original tutorial and source code.

HOWTO:  Step by Step for installation process of a WordPress dockerized container instance ran on Google Cloud Run with a Google Cloud SQL (cloud managed) MySQL backend database.

These are the steps we will follow:

Create a Project On Google Cloud Platform

Enable billing.

Activate Cloud Run API, Cloud SQL API, and Cloud SQL Admin API

Use the console search to locate the APIs easily 😊

Prepare the image for Cloud Run.

Since the repository’s last update, WordPress has upgraded to 5.7.2, and PHP released version 8.

See below for forked source code repository pull information from GitHub.

lawrenceaph/wp-gcloud-run

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

**Choose an affordable instance.**

Create a database:

Deploy the container 🎉

Pick a cool name for your service.

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.

https://api.wordpress.org/secret-key/1.1/salt/

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:

:/cloudsql/retail1-300715:asia-east1:sql1

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:

The actual text of the secret (the password) is not seen anywhere. It is passed directly without showing up in the interface.

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).

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
100 %
Angry
Angry
0 %
Surprise
Surprise
0 %
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
العربية简体中文NederlandsEnglishFrançaisDeutschItalianoPortuguêsРусскийEspañol
0
Would love your thoughts, please comment.x
()
x