How to Deploy WordPress on Google Cloud Run: A Step-by-Step Guide


Google Cloud Run is a fully managed compute platform that allows you to deploy containerized applications directly onto Google Cloud’s infrastructure. It automatically scales your application up or down based on traffic, ensuring that you only pay for the resources you actually use. Deploying WordPress on Cloud Run can be a great way to leverage this serverless platform for hosting a scalable, low-maintenance WordPress site. In this article, we’ll guide you through the process of deploying WordPress on Google Cloud Run.

Why Choose Google Cloud Run for WordPress?

Google Cloud Run is ideal for hosting web applications like WordPress for several reasons:

  1. Scalability: Cloud Run automatically scales your application based on traffic. This is particularly useful for WordPress sites that experience variable traffic patterns.
  2. Cost Efficiency: You only pay for the compute resources you use, making it cost-effective for smaller websites or applications with sporadic traffic.
  3. Managed Infrastructure: Cloud Run handles the underlying infrastructure, including scaling, load balancing, and security, so you can focus on your application.
  4. Serverless: Being serverless, Cloud Run abstracts away server management, allowing you to deploy and manage your application without worrying about server maintenance.

Prerequisites

Before you begin, ensure you have the following:

  1. Google Cloud Account: You need an active Google Cloud account with billing enabled.
  2. Docker Installed: WordPress will be containerized using Docker, so you need Docker installed on your local machine. Download it from the official Docker website.
  3. Google Cloud SDK: Install the Google Cloud SDK to interact with Google Cloud services from your local machine. You can install it from the Google Cloud SDK website.

Step 1: Set Up a MySQL Database

WordPress requires a MySQL database. In a production setup, it’s best to use a managed database service like Google Cloud SQL. Here’s how you can set it up:

  1. Create a Cloud SQL Instance: Go to the Google Cloud Console, navigate to Cloud SQL, and create a new MySQL instance.
  2. Set Up the Database: Once the instance is ready, create a new database for WordPress. Note down the database name, username, and password, as you will need them later.
  3. Enable Public IP (Optional): Enable public IP if you want to connect directly from Cloud Run. Ensure to configure the necessary firewall rules.

Step 2: Create a Dockerfile for WordPress

Next, you need to create a Docker container for your WordPress site:

  1. Create a New Directory: Start by creating a new directory for your WordPress project:
   mkdir wordpress-cloudrun
   cd wordpress-cloudrun
  1. Create a Dockerfile: Inside this directory, create a Dockerfile:
   touch Dockerfile
  1. Add the Following Content to the Dockerfile:
   # Use the official WordPress image
   FROM wordpress:latest

   # Install any additional PHP extensions or packages you need
   RUN docker-php-ext-install mysqli

   # Expose port 8080 (the port Cloud Run expects)
   EXPOSE 8080

   # Use wp-config.php that is configured to read environment variables
   COPY wp-config.php /var/www/html/wp-config.php

   # Change the default CMD to use PHP built-in server for Cloud Run
   CMD ["php", "-S", "0.0.0.0:8080", "-t", "/var/www/html"]
  1. Create a Custom wp-config.php: Create a custom wp-config.php file in the same directory, which reads database credentials from environment variables:
   <?php
   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', '');

   $table_prefix = 'wp_';

   define('WP_DEBUG', false);
   define('WP_HOME', getenv('WP_HOME'));
   define('WP_SITEURL', getenv('WP_SITEURL'));

   if ( !defined('ABSPATH') )
       define('ABSPATH', dirname(__FILE__) . '/');

   require_once(ABSPATH . 'wp-settings.php');

This custom wp-config.php is configured to pull the database credentials and site URL from environment variables, which will be set in Cloud Run.

Step 3: Build and Push the Docker Image

  1. Build the Docker Image:
   docker build -t gcr.io/[YOUR_PROJECT_ID]/wordpress-cloudrun .

Replace [YOUR_PROJECT_ID] with your actual Google Cloud project ID.

  1. Push the Docker Image to Google Container Registry:
   docker push gcr.io/[YOUR_PROJECT_ID]/wordpress-cloudrun

Step 4: Deploy WordPress to Google Cloud Run

  1. Deploy to Cloud Run:
   gcloud run deploy wordpress --image gcr.io/[YOUR_PROJECT_ID]/wordpress-cloudrun --platform managed --region us-central1 --allow-unauthenticated --update-env-vars DB_NAME=[DB_NAME],DB_USER=[DB_USER],DB_PASSWORD=[DB_PASSWORD],DB_HOST=[DB_HOST],WP_HOME=https://[CLOUD_RUN_SERVICE_URL],WP_SITEURL=https://[CLOUD_RUN_SERVICE_URL]

Replace [DB_NAME], [DB_USER], [DB_PASSWORD], and [DB_HOST] with your Cloud SQL database credentials, and [CLOUD_RUN_SERVICE_URL] with the URL provided by Cloud Run after deployment.

  1. Post-Deployment Setup: Once deployed, visit the Cloud Run URL to complete the WordPress setup.

Step 5: Configure Cloud SQL Connection (Optional)

If your Cloud SQL instance is not publicly accessible, you’ll need to set up a private connection using the Cloud SQL Auth proxy. Here’s how:

  1. Create a Cloud SQL Auth Proxy Service Account: Create a service account with the Cloud SQL Client role.
  2. Download the Service Account Key: Save the key file and use it when deploying with the --add-cloudsql-instances flag.
  3. Deploy with Cloud SQL Auth Proxy:
   gcloud run deploy wordpress --image gcr.io/[YOUR_PROJECT_ID]/wordpress-cloudrun --platform managed --region us-central1 --allow-unauthenticated --add-cloudsql-instances [INSTANCE_CONNECTION_NAME] --update-env-vars DB_NAME=[DB_NAME],DB_USER=[DB_USER],DB_PASSWORD=[DB_PASSWORD],DB_HOST=localhost,WP_HOME=https://[CLOUD_RUN_SERVICE_URL],WP_SITEURL=https://[CLOUD_RUN_SERVICE_URL]

Replace [INSTANCE_CONNECTION_NAME] with your Cloud SQL instance connection name.

Why Use Google Cloud Run for WordPress?

Google Cloud Run is well-suited for certain types of WordPress deployments, especially in cases where:

  • Traffic is Variable: Cloud Run’s automatic scaling makes it ideal for sites with sporadic or unpredictable traffic patterns.
  • Server Management is a Concern: Cloud Run abstracts away server management, allowing developers to focus purely on application code.
  • Cost Efficiency is Key: Since you only pay for the compute resources used during request handling, Cloud Run can be cost-effective for low-traffic sites.

Limitations and Considerations

While Cloud Run is powerful, it’s important to note that:

  1. Cold Starts: Since Cloud Run scales down to zero when idle, there may be a slight delay (cold start) when the service is invoked after a period of inactivity.
  2. Persistent Storage: Cloud Run is stateless and does not provide persistent storage. You need to use external services like Cloud SQL, Cloud Storage, or Firebase for any data persistence.
  3. Complexity: For high-traffic or more complex WordPress setups, Kubernetes (GKE) or another managed service like Amazon ECS might be more appropriate due to better control over resources and more advanced scaling options.

Conclusion

Running WordPress on Google Cloud Run allows you to take advantage of a fully managed, scalable, and cost-efficient serverless environment. While it’s great for specific use cases, especially where traffic is unpredictable or low, it’s essential to consider the stateless nature of Cloud Run and plan accordingly. For larger or more complex deployments, consider using Kubernetes or other container orchestration platforms for better control and flexibility.