PHP Functions on Cloud Run 🐘 🎉

Grant Timmerman
Jan 15 · 3 min read

Introducing the PHP Functions Framework. This open source library allows you to easily deploy PHP applications to Cloud Run. In this blogpost, we’ll give you a quick introduction to this package and show you how to deploy a PHP function to Cloud Run!💨👟

PHP Functions Framework

The PHP Functions Framework allow anyone to easily develop self-contained PHP functions with a FaaS developer experience that can be run/tested locally and deployed to the Cloud. The framework is a package published on Packagist and source code is on GitHub.

Develop PHP Functions

The PHP Functions Framework allows you to use Symfony’s object-oriented web application framework to create simple HTTP request handlers. While testing on your computer, you can use PHP’s built-in web server (via a simple CLI command) to run your function’s code that responds to HTTP requests.

The framework is best illustrated with a sample. Let’s create a “Hello World” example by writing a simple function in a new file, index.php:

Then specify this dependency on the framework in a composer.json file:

Install this package with composer install.

Finally, run the Functions Framework locally with the following script that uses PHP’s built-in web server:

export FUNCTION_TARGET=helloHttp
export FUNCTION_SIGNATURE_TYPE=http
php -S localhost:8080 vendor/bin/router.php

Note: The export commands only need to be run once initially.

At http://localhost:8080/, you’ll see your PHP server running locally! 🐘

Containerize your Application

The PHP Function Framework is portable, meaning you can take your existing application code with the Functions Framework and run it anywhere that supports PHP.

The PHP Functions Framework repo includes an example Dockerfile that we can use/copy to build our container. Add the contents of this file (copied below) to a new file named Dockerfile next to your index.php file:

Deploy to Cloud Run

Deploying your application to Google Cloud is easy. Run the following commands to build your container and deploy to Cloud Run 💨👟:

# Set env var "GCP_PROJECT" to our project name
GCP_PROJECT=$(gcloud config list --format 'value(core.project)' 2>/dev/null)
# Set our Cloud Run region (so we aren't prompted)
gcloud config set run/region us-central1
# Build and upload your image in Google Container Registry
gcloud builds submit --tag gcr.io/$GCP_PROJECT/hellophp
gcloud run deploy hellophp \
--image gcr.io/$GCP_PROJECT/hellophp \
--region us-central1 \
--allow-unauthenticated \
--set-env-vars=FUNCTION_TARGET=helloHttp \
--set-env-vars=FUNCTION_SIGNATURE_TYPE=http

After ~30 seconds, you’ll get a URL that looks like this:

https://hellophp-q7vieseflq-uc.a.run.app/?name=Functions%20Framework

Note: This Hello World service was deleted due to abuse…

Feel free to curl your URL as much as you want and invoke our function!

There you have it! You’ve deployed your first PHP function to Google Cloud!

Learn More

Eager to learn more about Function Frameworks with Google Cloud?

Google Cloud - Community

A collection of technical articles published or curated by…

Grant Timmerman

Written by

Google • Explorer

Google Cloud - Community

A collection of technical articles published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

More From Medium

More from Google Cloud - Community

More from Google Cloud - Community

Define new paths with routing

More from Google Cloud - Community

More from Google Cloud - Community

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade