Cloud DNS Cloud SQL Google Cloud

How to Install WordPress on Google Cloud Run with SSL

Google Cloud Run is a server less container architecture which is highly scalable for running WordPress.

In this guide you are going to learn how to install or deploy WordPress to Cloud Run and configure custom domain with SSL.

Best Hosting for your Business

PlatformReviewsPricing
Siteground★★★★★$3.95
Kinsta – Google Cloud★★★★★$30

Prerequisites

Steps to Deploy WordPress on Google Cloud Run

  1. Create Service accounts for Google Cloud SQL, Google Cloud Storage.
  2. Prepare Dockerfile, Custom PHP configuration, WordPress Media Offload plugin, .htaccess, and the service account Json key.
  3. Build docker image and push to Google Container registry.
  4. Create Cloud Run service using the image.
  5. Map custom domain and install SSL.
  6. Point Domain to Cloud Run
  7. Install WordPress
  8. Setup WordPress Offload plugin to store media files on Google Cloud Storage.

Let’s start the setup.

Enable APIs for Cloud SQL

Go to APIs and Services and click Enable APIs and Services and enable these two APIs.

  • CloudSQL API
  • CloudSQL Admin API

Create Service accounts

First we have to create service account for Cloud SQL

Go to IAM & Admin >> Service accounts and click Create service account

In the Service account details enter Service account Name

Click Create

In the next step, select Role and add the following roles.

Cloud SQL >> Cloud SQL Admin

Click Continue and complete the creation.

Now you need to follow the same steps above to create another service account for Google Cloud Storage.

Here you need to select role as the following.

Cloud Storage >> Storage Admin

You also need to create the Json key for this service account and save the key, we need to use it later.

Create Google Cloud Storage Bucket

Go to your Google Cloud Console and navigate to Storage >> Storage and create a bucket.

Enter the name for your bucket and create.

Note the name of your bucket.

Cloud SQL Details

I assume you have created Cloud Sql instance using the guide mentioned in the prerequisites section.

Create a database and user in your Cloud SQL.

Note down the username, password, database name and the instance connection name.

Prepare Docker Setup

Now you need to go to your cloud shell or if you have Google Cloud SQL installed on your local machine you can also open the terminal.

I am using Cloud Shell for this setup.

Create a directory to have our configurations.

mkdir cloudrun

Navigate inside the directory.

cd cloudrun

Setup .htaccess

Create .htaccess file and add the configuration for HTTPS redirect and pretty permalink.

nano .htaccess

Add the following.

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

This configuration will redirect all HTTP requests to HTTPS.

Configure Custom PHP settings

Create a configuration file for custom PHP settings.

nano custom-php.ini

Add the following.

memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
max_input_time = 1000
max_input_vars = 5000

You can modify the values as per your wish.

Add Service Account key

Copy the Json key file you just downloaded while creating service account for Google Cloud Storage to this directory.

Custom WP-Config

Create a WordPress configuration wp-config.php file for our installation.

nano wp-config.php

Add the following.

<?php
define('FORCE_SSL_ADMIN', true);
if (strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false)
    $_SERVER['HTTPS']='on';

define( 'DB_NAME', getenv('DB_NAME') );
define( 'DB_USER', getenv('DB_USER') );
define( 'DB_PASSWORD', getenv('DB_PASSWORD') );
define( 'DB_HOST', getenv('DB_HOST') );

define( 'DB_CHARSET', 'utf8' );

define( 'DB_COLLATE', '' );

define('AS3CF_SETTINGS', serialize(array(
    'provider' => 'gcp',
    'key-file-path' => '/var/www/html/service_account_conf.json',
)));

define('AUTH_KEY',         'YOUR_VALUE');
define('SECURE_AUTH_KEY',  'YOUR_VALUE');
define('LOGGED_IN_KEY',    'YOUR_VALUE');
define('NONCE_KEY',        'YOUR_VALUE');
define('AUTH_SALT',        'YOUR_VALUE');
define('SECURE_AUTH_SALT', 'YOUR_VALUE');
define('LOGGED_IN_SALT',   'YOUR_VALUE');
define('NONCE_SALT',       'YOUR_VALUE');

$table_prefix = 'CblEt_';

define( 'WP_DEBUG', false );

if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', __DIR__ . '/' );
}

require_once ABSPATH . 'wp-settings.php';

We will configure the env variables for database, user, password and host at Cloud Run while deploying.

Change the service_account_conf.json with the filename of yours.

Upload Offload Media Plugin

Create a wp-content/plugins directory.

cd wp-content/plugins

Download offload media plugin and extract it.

wget https://downloads.wordpress.org/plugin/amazon-s3-and-cloudfront.2.5.5.zip
unzip amazon-s3-and-cloudfront.2.5.5.zip
rm -rf amazon-s3-and-cloudfront.2.5.5.zip

Go to your main directory.

cd ~/cloudrun

Now your should have these files inside your cloud run directory.

  1. .htaccess
  2. custom-php.ini
  3. service_account.json
  4. wp-config.php
  5. wp-content/plugins which has your offload media plugin.

Create Dockerfile

Finally create the Dockerfile for building image. This file should be located alongside the other files.

nano Dockerfile

Add the following.

FROM wordpress:php8.0-apache

COPY ./custom-php.ini /usr/local/etc/php/conf.d/

WORKDIR /var/www/html
COPY ./wp-config.php /var/www/html/wp-config.php
COPY ./.htaccess /var/www/html/.htaccess
COPY ./offload.json /var/www/html/offload.json
COPY ./wp-content /var/www/html/wp-content/

This command will pull the latest WordPress image with PHP 8.0 and Apache. Then it will copy the custom configurations that we created above to the container.

Build Image and Push to Registry

You need create a docker image in order to setup Cloud run.

Execute the following command to build image and push it to Google Container Registry.

gcloud builds submit --tag gcr.io/PROJECT_ID/image_name

Once the build is complete and the image is pushed your will get a success message.

Create Cloud Run Service

Go to Serverless >> Cloud Run and click Create Service. This will enable all necessary APIs.

Enter Service name for your application.

Select the region same as the one where you have created your Cloud SQL instance.

Create Cloud Run Service

Click Next.

Select Container Image

Click Select and select the container image you just pushed earlier.

Select Container Image

Click Select to continue.

Configure Container Details

Click Advanced settings tab.

Change the port from 8080 to 80. As we don’t expose any custom port in our docker file we are using the default 80 port in our container.

Configure the capacity as per your wish.

Also you can configure how many instances you need.

Cloud Run Port Settings

Configure Variables & Secrets

Click Variables & Secrets tab.

Click Add variable and follow this to add your database, user, password and host details as shown in the following image.

  • DB_NAME database name
  • DB_USER username
  • DB_PASSWORD user password
  • DB_HOST :/cloudsql/instance_connection_name

Note the DB_HOST it needs to be in the exact format with :/cloudsql/.

Cloud Run Variables & Secrets

Configure Connections

Click Connections tab.

Cloud Run Cloud SQL connections

Select your Cloud SQL instance in the Cloud SQL Connections.

Click Next.

Configure Public access

Choose Allow all traffic in Ingress.

Choose Allow unauthenticated invocations in Authentication.

Cloud Run allow public traffic

Click Create.

Now your Cloud Run service will be deployed.

Once the deployment is completed you will see the following screen.

Cloud Run deployment

Once the deployment is completed you can view your deployment using the URL that is generated for you.

Configure Custom Domain

Go to Serverless >> Cloud Run and click Manage Custom Domains.

Click Add mapping.

Select your domain. If it is not visible you need to verify you are the owner of the domain.

Click Continue.

You will be presented with the DNS records that you need to add to your domain DNS.

Once you have added those records, complete the setup.

This will take around 20 min to deploy your certificate and for propagation.

Cloud Run setup Custom Domain

Once the certificate is deployed you can view your site on your domain name. You will have SSL installed and configured.

Install WordPress and complete your setup.

Configure Offload Media Plugin

Once the installation is complete, login to your dashboard and go to Plugins.

You will see the plugin that we have added inside the wp-content/plugins directory.

Activate the plugin.

Go to the plugin settings page and enter your bucket name and save the settings.

Configure Google Cloud Storage bucket

Now you have your WordPress configured to use Google Cloud Storage for your Media Library.

Get your Professional Google Cloud Architect certificate with this easy to learn course now.

Now you can use WordPress as usual.

Conclusion

Now you have learned how to install and configure WordPress on Google Cloud Run and setup custom domain with SSL.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

If you find this helpful? Support me!
Buy me a coffee Donation Please buy me a coffee