PHP on Google App Engine

  • App Engine supports PHP 5.4
  • Over 35 built-in extensions
  • Familiar MySQL database interactions
  • Must be pure PHP, no custom C libraries
  • Support for Google Cloud SQL and Google Cloud Storage

Getting started

To setup your development environment:

  • Install Python 2.7 (required for SDK tools)
  • Install PHP 5.4 (the php-cgi binary is required)
  • Install the Google App Engine SDK for PHP
  • Install the MySQL server (required for local testing)

Creating a PHP application

  • Create a directory for your application
  • Create an app.yaml file (your app config)
    • Maps URLs to files in your application
    • This sample has a static images directory and a single PHP called on access of any URL
    • The application item is the application ID in App Engine
application: helloworld-php
version: 1
runtime: php
api_version: 1

handlers:
- url: /images
  static_dir: images

- url: /.*
  script: index.php
      

Writing the PHP code

  • Create some PHP files, and map them in your app.yaml
  • The sample snippet below just prints a message based on the time
<?
  print('<p>Hello GAE-PHP world!</p>');
  if (time() % 2 == 0) {
    print('<p>You visited on an even-numbered second since epoch</p>');
  } else {
    print('<p>You visited on an odd-numbered second since epoch</p>');
  }
?>
      

Test your application locally

  • Run the development application server
dev_appserver.py --php_executable_path=<path to php-cgi> <path to application directory>
      
  • If you're using Cloud SQL
    • Make sure MySQL is started locally
    • Set the database host in your code based on whether the app is running locally or live
if (isset($_SERVER['SERVER_SOFTWARE']) &&
  strpos($_SERVER['SERVER_SOFTWARE'],'Google App Engine') !== false) {
    // Set to the CloudSQL Host spec
  } else {
    // Set to localhost
  }
      

Deploy to App Engine

  • Create an App Engine application
  • Set the application ID to match what's in app.yaml
  • Right now, you'll need to have your account whitelisted for PHP deployment
appcfg.py update <path to application directory>
      

Summary

<Thank You!>