One of the biggest players in the cloud hosting industry has finally opened its doors to PHP applications. JetBrains’ Maarten Balliauw shows how easy it is to get your app up and running.

Working with PHP on the Google Cloud Platform
1 Kommentar

At Google I/O this year, the web company announced support for PHP on Google App Engine. We can now make use of this platform-as-a-service cloud for hosting our PHP applications, including e-mail support, task queues, Memcache, integration with Google Accounts and more. The Google cloud comes with a MySQL database as well. Time to put this platform to the test!

While the PHP runtime for Google App Engine is still in preview, it offers a glimpse of features that will be available in the future. In this article, we’ll be exploring some of the services available in Google’s cloud and combining them to build a simple application.


The Google Cloud

Let’s start with the basics: what is in Google’s cloud offering and why should we care? If we look at, there are a number of services offered:

  • App Engine, a platform-as-a-service which takes care of provisioning, scaling, load balancing and a number of other infrastructure services in a transparent way. And we can run PHP on it!
  • Compute Engine, infrastructure-as-a-service where we can have a complete Linux virtual machine we can install software on.
  • Cloud Storage, a REST-based storage system we can host any files used by our application. It can also be used to offload static content from our web server.
  • BigQuery, a big data solution for analyzing vast amounts of data.
  • Cloud SQL, a scalable and managed MySQL database solution.

There are more services than the ones listed. Basically every API available through Google (Translate, Maps, Prediction, …) can be used with our applications deployed to Google cloud.

Throughout this article, we’ll be making use of Google App Engine and Google Cloud SQL,two services that make up for a complete „LAMP“ stack (even though the „A“ is probably not Apache, but a webserver built by Google).

Required software

Before we can start, we need some software installed. Google provides an SDK which includes a simulated App Engine environment, including a local version of Google Accounts, Memcache and so on. We will need:

  • Python 2.7
  • PHP 5.4
  • App Engine PHP SDK

Check the documentation for the required steps for your operating system.

We’ll be using PhpStorm as our IDE, as it comes with integration for Google App Engine. A 30-day trial can be downloaded from The Google App Engine plugin can be enabled by following the instructions available at

Meet ShortnR, a URL shortening service.

Let’s build a sample application! „Hello World“ is probably too basic (who needs a database for that?) and building the next Twitter or Facebook application has been done a thousand times. How about… a URL shortener service?

Figure 1: ShortnR - Short URLs made simple.

Our URL shortener will perform a couple of basic tasks:

  • Create a short URL for a normal URL
  • Redirect users hitting the short URL to the normal URL
  • Display the last five shortened URLs on the front page

Code listings in this article may be simplified a little. This sample application’s full source code can be found on

File | New Project…

When creating a new project in PhpStorm using the File | New Project… menu, we can give our project a name, directory location and specify the project type we want to start off with. Since we have installed the Google App Engine plugin, we can select App Engine Project to begin.

On the next screen, we have to specify some information about our application and environment. We have to enter:

  • The App Engine Application ID, typically the name we give our App Engine project when first created through
  • The SDK directory, where we installed the App Engine SDK
  • The path to a Python 2.7 executable

Figure 2: Google App Engine project settings.

Once this is done, a new project is created in which we can find several files. The app.yaml is an important one: it holds all information the Google App Engine runtime will use to host our application. This includes an application name, the handlers we want to make use of and the services we will be using. If we open this file we can see that all requests will be dispatched to the main.php file, which contains a simple „Hello, world!“.

We can now already test our application locally, in the simulated App Engine environment, by clicking the Run/debug configuration selector in the toolbar and providing the path to a PHP executable. We can also debug our application locally. If we wanted to deploy already, the Tools | Google App Engine for PHP | Upload App Engine PHP app… menu will do the trick.

Making it look nice

A bit of a good design for our service will definitely attract more users, so let’s add a bit of Twitter Bootstrap to it. After downloading and extracting into our project, we’ll have to map some static files. By default, no files and folders in a Google App Engine project are exposed to the Internet. We have to add static file handlers in our app.yaml file to explicitly expose our Twitter Bootstrap JavaScript, CSS and images. Under the handlers element, add the following entries:

- url: /img
  static_dir: public/img
- url: /js
  static_dir: public/js
- url: /css
  static_dir: public/css

This will map the URL /img to our project’s /public/img folder, /js to /public/js and so on. We’re using the static_dir handler, but note there are many others available for mapping URLs to scripts, automatically minify JavaScript and CSS files or enforce use of SSL and/or authentication. See the App.yaml file specification for more information.

Shortening a URL

Our app.yaml file defines a script handler as well, where all traffic is routed through main.php. We will be keeping this pattern, but know that you can add as many script handlers as you want that map to different URLs. Our main.php script will map incoming URLs to other PHP scripts, like so:

$requestedPage = substr($_SERVER['REQUEST_URI'], 1);
if (substr($requestedPage, 0, 1) == 'u') {
    require_once 'perform_redirect.php';
} else {
    require_once $requestedPage . '.php';

A word of warning: This is probably not the most efficient and secure way of doing things, but since it’s a sample application it’ll do. In real life, verify that the value presented in $requestedPage actually maps to an existing page. The above approach opens doors to displaying pages that aren’t supposed to be exposed to the Internet.

On our homepage, we have an HTML form with one field, named url, which we want to work with for shortening the URL. We will be storing the URL in our Google Cloud SQL database, as well as in the Memcache cluster that comes with Google App Engine, for fast access and to reduce load on the database.

Working with the database

First of all, we’ll have to make sure we have a database to work with. From the Google API Console at, create a new Google Cloud SQL instance. Once done, open the SQL Prompt and issue the following MySQL queries to create a new database for our application as well as an application-specific user with access to this database:

CREATE USER 'shortnr'@'%' IDENTIFIED BY 'password_here';

We can now open a database connection from our application using the PDO, mysql or mysqli extensions. The code below shows opening the connection using PDO and creating a table named url.

Figure 3: The database table on Google Cloud SQL.


A good way of reducing load on the database is by caching data that is often used in a dedicated system, such as Memcache. Google App Engine provides an implementation of Memcache which can be used in our applications and in which we can store data in memory.

Google doesn’t use the „real“ Memcache server, but they are playing nice and provide us with a full alternative to the Memcache functions we know from PHP. Here’s how we connect to the Google App Engine Memcache cluster:

$memcache = new Memcache();

There is no need to specify server information since Google makes sure this works out-of-the-box. Let’s store a PHP array in Memcache:

$memcache->set('homepage:list', array('value1', 'value2'));

In order to display recently shortened URLs on the homepage, we can query Memcache:

$lastShortened = $memcache->get('homepage:list');

We can also make use of the Memcache Viewer from the App Engine dashboard to add/remove keys, see Memcache hit ratios and so on. We can also inspect values, as can be seen below.

Figure 4: Memcache Viewer.

Deploying our application

Next to testing our application locally (by clicking the Run/debug configuration selector in the toolbar), we can also deploy our application to Google App Engine. The Tools | Google App Engine for PHP | Upload App Engine PHP app… menu will trigger a deployment of the project we have currently open.

The deployment will run in a separate tool window and output progress of updated files and folders as can be seen in the code below.

C:Python27python.exe C:/google_appengine/ -e  --passin --no_cookies -R --runtime=php update .
10:21 AM Host:
10:21 AM Application: ; version: 1
10:21 AM 
Starting update of app: ps-int-testing, version: 1
10:21 AM Getting current resource limits.
Password for : 10:21 AM Scanning files on local disk.
10:21 AM Cloning 8 static files.
10:21 AM Cloning 10 application files.
10:21 AM Uploading 2 files and blobs.
10:21 AM Uploaded 2 files and blobs
10:21 AM Compilation starting.
10:21 AM Compilation completed.
10:21 AM Starting deployment.
10:21 AM Checking if deployment succeeded.
10:21 AM Deployment successful.
10:21 AM Checking if updated app version is serving.
10:22 AM Completed update of app: , version: 1

Process finished with exit code 0

Note that Google App Engine will verify the application and ensure it can be displayed in a browser. This means that if your application has an error or PHP can not interpret the application, this process will fail after a while and the new version will not be deployed.


As stated before, Google’s cloud offers a wide variety of services. In this article, we’ve only scratched the surface of what we can do with Google App Engine and Google Cloud SQL and there are even more services around! If you want to read more on Google App Engine and PHP, check out More information on PhpStorm’s Google App Engine integration is available from

Being a cloud fanboy, I’ve experimented a lot with the big boys, Amazon and Windows Azure. While all have very mature solutions, I think Google App Engine for PHP provides a fresh look at how applications can be deployed and managed, and how the cloud platform can be integrated in a transparent way. Google App Engine comes with a Memcache cluster and we don’t have to do anything for it. syslog() just works. It can send e-mail using mail(). We can even accept inbound e-mail and map it to a URL on our application. The cloud is here to stay, and I think the future of platform-as-a-service is bright.

Maarten Balliauw is a Technical Evangelist at JetBrains. His interests are all web: ASP.NET MVC, PHP (can’t choose between both platforms…) and Windows Azure. He has published many articles in both PHP and .NET literature such as MSDN magazine and PHP architect. Maarten is a frequent speaker at various national and international events such as MIX (Las Vegas), TechDays, DPC, … His blog can be found at He’s also on Twitter as @maartenballiauw.

Unsere Redaktion empfiehlt:

Relevante Beiträge

Benachrichtige mich bei
1 Kommentar
Inline Feedbacks
View all comments
Fahad Rafiq

Well, if you go for cloudways, you dnt need to do hard coding as above you describe. You can host php in a click on google cloud

- Gib Deinen Standort ein -
- or -