Deploy PHP applications in Google App Engine


  

Google App Engine offers native support only to  Java and Python. This made many web developers unhappy as they were using PHP for programming. It is little known fact that you can actually deploy PHP applications in Google App Engine. This edition of techblog is dedicated entirely for explaining this.

As you might be knowing Google offers about 500 MB space and 1 GB bandwidth for free if you deploy these applications in App Engine. Hence it will be quite useful if you could actually use PHP for development. Another feature is that you can use GQL (Google Query Language) to access and store data in Google’s own data centres. Needless to say, if you host an application in Google’s servers you don’t have worry about issues like scalability.

 

Setting up the System for development

This tutorial assumes that the reader has the basic knowledge on Google App Engine (If not, please read this tutorial first). At the very outset itself, let me clarify this point. The native support is only for Java and Python. This means that we can do programming in PHP and deploy them in App Engine only if we use a proper Java or Python library for ‘interpreting’ PHP.

 

php in google app engine

 

This can be done using Quercus. It is a Java implementation of the PHP language. We will be writing all our applications in PHP and then link them to this library before deployment.

 

  • First of all, register a new application in Google App Engine (refer to the previous tutorial)
  • Download the core framework from Techblog’s official Google Code Repository  (Click here to download the file directly)
  • Unzip the file to a directory in your computer
  • Go to ‘war\WEB-INF\’ folder and open appengine-web.xml  using your favourite text editor (Nano or Gedit in Linux , Notepad or Notepad++ in Windows)
  • Change the name of the application in that file to the name you registered
  • Download Google App Engine SDK for Java from Google.
  • Use appcfg to upload the contents of ‘war’ folder to app engine server (Please refer to the previous tutorial). If you are in Windows , open CMD and type:

cd ./your_sdk_directory/bin

appcfg.cmd update C:\your_directory\war

 

Done!

 

Checking the Application

 

You can go the application URL and try to access the index.php file and php_info.php file. In my case, the URLs are

http://php-program.appspot.com/index.php

http://php-program.appspot.com/php_info.php

 

And you can see that they are working properly. Now you can do programming in  PHP and deploy them in App Engine!

 

How it works?

Please note that all the Java functions are already implemented and you can start programming in PHP (provided you use the file downloaded from techblog repository) and if you are not interested in Java, you can SKIP this segment.

 

Let’s make simple  a ‘Hello World’ application:

If you look at the web.xml file you can see a servlet mapping like the one shown below:

 

servlet servlet-name="resin-php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"/>
<servlet-mapping url-pattern="*.php" servlet-name="resin-php"/>
      

 

It is just like the mapping in  Java frameworks like Spring. All it does is that it looks for the URL pattern *.php (all URLs end with .php) and redirect the request from the browser to QuercusServlet.

This servlet will redirect the request to the right Java class. In this case, we need a HelloWorld servlet :

 

package tecblog.sample.javaphp;

import com.caucho.quercus.module.AbstractQuercusModule;


public class HelloWorld extends AbstractQuercusModule {

   public String myhello(String mystring)
   {
     return "Hello, " + mystring;
   }
}

 

This class will basically print (to be specific – it returns) the message ‘Hello’ followed by your string.

And we can have a simple php file (say hello.php) and add these:

 

<?php echo myhello("world") ?>

 

This will output ‘Hello world’ when accessed by the user.

 

 

Do I need to know Java?

No. You can start adding new PHP files to the framework we downloaded from techblog repository. You may note that we have used two or three php (note that socket connections are not allowed in app engine)  files in that zipped file. You can replace or add new files to the same directory and upload them to Google App Engine!

Be Sociable, Share!

Tags: , ,


TechBlog on Facebook

Comments (10)

 

  1. Prashanth says:

    Very good Article.keep it up.

  2. Takako Mway says:

    hello bros!! Amazing site!

  3. I’m happy to see people have courage enough to try PHP on Google App Engine. I think its still interesting way to code some cool apps, after a year experimenting with it.

  4. Andrew Molo says:

    Please assist me with the links of the previous tutorials . Thanx

  5. Andrew Molo says:

    Can you please provide me with the previous tutorials links?

  6. Admin says:

    You can find them under the category listing… Plus there is a search box … hope this helps

  7. Usha says:

    How does it get the php version..when run this sample, I get php version as 5.2 which is very old. Do I have flexibility to upgrade to new version?

  8. Nizar says:

    I have followed all the steps here, but it only shows Hello world with no access to other files. Can any one help me ?

  9. Admin says:

    I’m afraid not

  10. Admin says:

    Google recently changed the settings. Now this won’t work any more :(

Leave a Reply