Click here to Skip to main content
15,171,053 members
Articles / Web Development / HTML
Posted 3 Sep 2013


7 bookmarked

PHP Web Apps on Google Cloud

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
19 Sep 2013Apache4 min read
Demonstrates Google Cloud's experimental support for PHP


The article intends to demonstrate PHP support in Google Cloud which has been recently announced on an experimental basis. I thank Google for allowing me access to this technology stack which is not yet publicly available on Google App Engine.  I quickly cobbled up this article over a Sunday to help technologists wanting to use PHP on Google Cloud (Google App Engine) which is an elegant & reliable PaaS to host WebApps. Some experience with Google Cloud shall help but is not mandatory. 



Over the past decade we have had newer trends overwhelming the Web technologies every now and then. PHP is one technology which has stood the test of time.  Although, it may seem that PHP is losing its midas touch, with newer stuff like Node.js and Ruby making steady headway, it still is huge. That is exactly the reason why Google, Microsoft have had to support PHP on their cloud offerings. Microsoft realized this sooner when Azure was still in Beta. I had even written a simple custom PHP blog engine running on Microsoft Azure. (This was the winner in PHP Category, within the contest organised by Microsoft India in 2009).    

Google Cloud (PaaS) however, initially stuck to Python, followed by Java and Go languages. Recently, Google announced experimental support for PHP. For me its -  better late than never. This presents a awesome opportunity for PHP enthusiasts and could be a game changer for Google Cloud.  I am sure Google shall open this up to everyone sooner rather than later. Currently, Google might allow you to enable your PHP applications to be deployed to App Engine, if you request here

Step 1: Setup

One of the first steps to is to setup your development environment up for Google Cloud. Simply download a& install Google App Engine SDK & PHP (In my case GoogleAppEngine-1.8.3.msi and If you use Java for GAE it may help to use Eclipse as IDE, but is not mandatory in this case. Relevant instructions for your OS are available here.  

Step 2: Create  

Lack of clear documentation makes creation of application a little tricky area. First step is to create a python Google Cloud app or simply take the sample app from the SDK. This contains a configuration file app.yaml. We just need to change few lines to make it a php web app.

1. Replace python with php.   

runtime: php  

2. Replace handler for php files. Note how static files need to be specified.     

- url: /css
  static_dir: css
- url: /js
  static_dir: js
- url: /fonts
  static_dir: fonts
- url: /assets
  static_dir: assets
- url: /shortener
  script: shortener.php
- url: /phpinfo
  script: phpinfo.php
- url: /.*
  script: index.php

3. Create php.ini file in the root folder for some restricted functions you may want to use.     

; php.ini
google_app_engine.enable_functions = "php_sapi_name, php_uname, phpinfo" 

Step 3:  Implement 

Code your php web app. In this case I wrote a simple PHP Web App based on Twitter BootStrap 3.0 demonstrating Google API calls from PHP. Although I would not use Bootstrap in this way in production it was to quickest way to get a responsive UI based on HTML5 and JQuery which can be served across PC, Tablets and Mobiles in an aesthetic manner. Note few things would not work on Google Cloud e.g functions like phpinfo();, file system access etc. However, one if free to use Google Cloud Storage. The code for this PHP  Web App is available for download.

This how my Web App looks on desktop

Image 2

This how the same Web App looks on mobile (Thanks to responsive HTML5 from Twitter BootStrap!) 

Image 3

Step 4: Call Google API   

Currently the demo calls Google's awesome URL Shortening API. PHP code used could'nt be any simpler. To be able to use Google APIs you need to download and use Google PHP Client Library from here. However a major difference exists between normal use and on Google Cloud, as we cannot use FileSystem Cache and and CURL on Google Cloud. To be able to use Google APIs on you need change your following lines in google-api-php-client\src\config.php.  (This would have been difficult to debug without this blog - Many Thanks!) 

// In google-api-php-client\src\config.php
'authClass'    => 'Google_OAuth2',
'ioClass'      => 'Google_HttpStreamIO',
'cacheClass'   => 'Google_MemcacheCache',

// We need to configure fake values for memcache to work
'ioMemCacheCache_host' => 'does_not_matter',
'ioMemCacheCache_port' => '37337',     

This is particularly troublesome as I did not memcache on my filesystem I had to switch between 'cacheClass'   => 'Google_FileCache' (Local Testing) and 'cacheClass' => 'Google_MemcacheCache'  (Google Cloud Production) . 

URL Shortening Logic is as simple as 

// shortener.php
$client = new Google_Client();
$service = new Google_UrlshortenerService($client);

if (isset($_GET['url'])) 
  // Start to make API requests.
  $url = new Google_Url();
  $url->longUrl = $_GET['url'];
  //Shorten URL
  $shortUrl = $service->url->insert($url);

The "Hello World" of PHP Kingdom is as simple as  


Running the Web App

Running Web App Locally is pretty easy. Run this command from out side root folder (php-4-gae in this case)

C:\eclipse\workspace> --php_executable_path=C:\php\php-cgi.exe php-4-gae   

For uploading to Google  Cloud run this command (You may be prompted for login id and password). 

C:\eclipse\workspace> update php-4-gae    


  • 31 Aug 2013: Uploaded first draft over a weekend. 

If I get some spare time over next weekend, I hope to publish next article using some popular framework like CodeIgnitor based REST API built on PHP and Google Cloud. And may be add some more Google API calls in the demo. 


This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


About the Author

Architect Symantec
India India
Software Architect

- Instrumental in transforming various concepts to successful concrete products/features.
- Passionate about quality in everything from the requirement specification to the end product.
- Involved with wide range of technologies and domains.
- Likes to think from customer or end user perspective.

Specialties: Architecture & Design, Web Services, Cloud Computing, C++, COM, C#, .NET
Clouds: Microsoft Azure, Amazon, Google App Engine
Other Interests : Linux, Java, HTML, PHP, Javascript and other cross platform technologies.

Connect with me @ LinkedIn

Comments and Discussions

QuestionGoogle App Engine Pin
Member 1255038227-May-16 6:06
MemberMember 1255038227-May-16 6:06 
QuestionGood Pin
Sahin Uddin Rony11-Nov-14 20:40
MemberSahin Uddin Rony11-Nov-14 20:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.