Serverless Blueprints: Google Cloud Run & Cloud SQL Setup

1. High-Level Architectural Summary

Zero CMS features a highly optimized, fully stateless backend perfectly suited for modern serverless container environments like Google Cloud Run. By utilizing lightweight native PHP and bypassing heavy third-party core packages, the bootstrap cycle of Zero CMS executes in under 1.5 milliseconds. This raw execution speed completely eliminates serverless latency cold-starts: container scaling is immediate and entirely transparent to end-users.

In a production serverless environment, the CMS operates in a fully decoupled architecture:

  • Stateless Compute: Google Cloud Run manages stateless PHP containers, scaling down to 0 active instances when idle to achieve absolute base-cost optimization.
  • Relational Storage: A Google Cloud SQL MySQL database instance manages data tables with strict multi-tenant boundaries.
  • Object Storage: Asset uploads and cropped media files stream directly to a Google Cloud Storage bucket (or AWS S3) via zero-dependency drivers.

2. Sizing Strategy for the Lowest-Cost Setup

To host Zero CMS with maximum performance for the lowest possible price (under $10/month or even fully covered under GCP's Free Tier under low traffic), configure your Google Cloud components with these precise settings:

  • Cloud Run Compute Sizing: Allocate 512 MB RAM and 1 CPU per container instance. Set the Minimum Instances to 0 (so that you pay exactly $0.00 when there is no traffic) and set the CPU Allocation to "CPU is only allocated during request processing" to avoid paying for idle server cycles.
  • Cloud SQL Database Sizing: Provision a shared-core db-f1-micro database instance (the smallest tier available, which costs around $9/month) running MySQL 8.0. Set the storage disk type to a 10 GB HDD (or SSD) and disable "automatic storage increase" for development to lock in the baseline rate. Additionally, use Single-Zone hosting to bypass multi-zone replication costs.
  • Cloud Storage Sizing: Standard GCS Buckets cost only $0.02 per GB per month. Enable GCS lifecycle policies to auto-purge temporary files after 30 days to optimize storage overhead.

3. Provisioning Cloud Resources (Cloud SQL & GCS)

To provision the GCP infrastructure natively using the Google Cloud CLI, execute the following commands in your terminal. Ensure that you have already run gcloud auth login and set your target GCP project via gcloud config set project YOUR_PROJECT_ID.

Step 1. Provision GCS Storage Bucket: Create an optimized, uniform-access Google Cloud Storage bucket to host public media uploads and cached crops:

Create Google Cloud Storage Bucket

Create Google Cloud Storage Bucket bash
gcloud storage buckets create gs://YOUR_BUCKET_NAME --location=YOUR_REGION --uniform-bucket-level-access

Step 2. Provision Cloud SQL Instance: Create an ultra-low-cost, shared-core db-f1-micro database instance running MySQL 8.0, locked to a single zone with a 10GB HDD size constraint to guarantee the lowest possible base cost:

Create Low-Cost MySQL Instance

Create Low-Cost MySQL Instance bash
gcloud sql instances create YOUR_INSTANCE_NAME \
--database-version=MYSQL_8_0 \
--tier=db-f1-micro \
--region=YOUR_REGION \
--storage-size=10GB \
--storage-type=HDD \
--availability-type=zonal

Step 3. Provision Database & DB User: Initialize a new SQL database schema container and configure a secure database user account on the Cloud SQL instance:

Create Database & DB User

Create Database & DB User bash
gcloud sql databases create zerocms_db --instance=YOUR_INSTANCE_NAME
gcloud sql users create zerocms_db_user --instance=YOUR_INSTANCE_NAME --password=your_strong_password_here

4. The Unix Socket Connection Config (.env)

To connect Cloud Run securely to Cloud SQL without public IP exposure or expensive NAT gateway costs, the containers must communicate via a local Unix Domain Socket. Zero CMS integrates an automated Unix socket connection resolver in src/Database/DB.php that activates dynamically whenever DB_SOCKET is present in the environment.

Inject the following environment parameters directly into your Google Cloud Run service revision:

Cloud Run Environment Configurations (.env)

Cloud Run Environment Configurations (.env) ini
# Database credentials
DB_HOST=127.0.0.1
DB_USER=zerocms_db_user
DB_PASS=your_strong_password_here
DB_NAME=zerocms_db

# The Cloud SQL Unix Socket Connection Path (forces PDO socket resolution)
DB_SOCKET=/cloudsql/YOUR_GCP_PROJECT_ID:YOUR_GCP_REGION:YOUR_CLOUDSQL_INSTANCE_NAME

# Cloud Storage configurations
STORAGE_DRIVER=gcs
GCS_BUCKET=your-custom-gcs-bucket-name

5. Step-by-Step Deployment & Seeding Workflow

Because serverless Cloud Run compute is completely stateless, database migrations and initial seeding operations should never be run inside the web container on boot. Instead, Zero CMS utilizes isolated **Google Cloud Run Jobs** to run migrations and seeder scripts to completion independently.

Follow this standard, low-cost deployment pipeline blueprint:

Step 1. Build the Container: Package your codebase and compile the optimized static CSS assets centrally, uploading the image to Artifact Registry via Cloud Build:

Build Container Image via Cloud Build

Build Container Image via Cloud Build bash
gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/zerocms-app:v1

Step 2. Execute Migration & Seeding via Cloud Run Jobs: Create and execute a short-lived Cloud Run Job to construct database schemas and seed multi-tenant tenant domains:

Execute Cloud Run Migrations & Seeding Job

Execute Cloud Run Migrations & Seeding Job bash
gcloud run jobs create zerocms-seed-job \
--image gcr.io/YOUR_PROJECT_ID/zerocms-app:v1 \
--command="php" \
--args="bin/seed" \
--set-env-vars="DB_SOCKET=/cloudsql/YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_NAME,DB_USER=...,DB_PASS=...,DB_NAME=..." \
--set-cloudsql-instances="YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_NAME"
gcloud run jobs execute zerocms-seed-job

Step 3. Deploy the Web Service: Deploy the stateless frontend service, enabling scale-to-zero compute:

Deploy Stateless Cloud Run Web Service

Deploy Stateless Cloud Run Web Service bash
gcloud run deploy zerocms-service \
--image gcr.io/YOUR_PROJECT_ID/zerocms-app:v1 \
--min-instances=0 \
--max-instances=10 \
--set-cloudsql-instances="YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_NAME" \
--set-env-vars="DB_SOCKET=/cloudsql/YOUR_PROJECT_ID:YOUR_REGION:YOUR_INSTANCE_NAME,DB_USER=...,DB_PASS=...,DB_NAME=..."