Run PHP on Google App Engine

Table of contents

Introduction

By adding Java to their App Engine, Google has opened the door for a whole slew of languages that have been implemented on the JVM, now including PHP via Quercus.

http://blog.caucho.com/?p=187

This very tutorial is deployed on GAE.

I must admit that I was pleasantly surprised by how effortless it was. OK, it's a very rudimentary PHP application, the only PHP code used was to run the examples described on the code blocks and do some includes; nevertheless I didn't feel the need to change a single line of code.

Also, deploying a Java application to GAE is simpler than a Python one. Not only because you have a very handy Eclipse plugin, but you will also find configuring the file appengine-web.xml a lot easier when compared to app.yaml.

Requirements

  1. Download and install Java SE JDK.
  2. Download and install Eclipse for PHP Developers.

Build a GAE Web Application project

  1. Create your application on Google App Engine.
  2. Install Google Plugin for Eclipse and restart your Eclipse.
  3. From the new project dialog choose "Web Application Project". The complete project directory structure looks like this:
    myProject/
      src/
        ...Java source code...
        META-INF/
          ...other configuration...
      war/
        ...JSPs, images, data files...
        WEB-INF/
          ...app configuration...
          lib/
            ...JARs for libraries...
          classes/
            ...compiled classes...
    
  4. Copy all your PHP and static files to myProject/war.
  5. Download Quercus binary (WAR file).
  6. Unzip it and copy all files inside the folder quercus.war/WEB-INF/lib to myProject/war/WEB-INF/lib.
  7. Edit your deployment descriptor file web.xml. Mine looks like this:
    <pre lang="xml" file="web.xml">
    <?xml version="1.0" encoding="utf-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
        <description>PHP Tutorial</description>
    
        <servlet>
            <servlet-name>Quercus Servlet</servlet-name>
            <servlet-class>com.caucho.quercus.servlet.GoogleQuercusServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>Quercus Servlet</servlet-name>
            <url-pattern>*.php</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>index.php</welcome-file>
        </welcome-file-list>
    </web-app>
    
  8. Edit your configuration file appengine-web.xml. Mine looks like this:
    <?xml version="1.0" encoding="utf-8"?>
    <appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
        <application>php-tutorials</application>
        <version>1</version>
    
        <!-- Configure java.util.logging -->
        <system-properties>
            <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
        </system-properties>
    
        <static-files>
            <include path="/**" expiration="600s" />
            <include path="/**.png" expiration="30d" />
            <include path="/**.jpg" expiration="30d" />
            <include path="/**.gif" expiration="30d" />
            <include path="/**.ico" expiration="30d" />
            <include path="/**.swf" expiration="30d" />
            <include path="/**.css" expiration="7d" />
            <include path="/**.js" expiration="2d 12h" />
            <exclude path="/**.php" /> 
        </static-files>
        <resource-files>
            <include path="/**.php" />
        </resource-files>
    </appengine-web-app>
    

    The application element must match the application identifier of the application you created on step 1.

Test & Deploy your application

Run your application using the Run As » Web Application command and point your browser to http://localhost:8888/.

Finally, press the Deploy App Engine Project button to deploy to Goole App Engine and point your browser to your application root to see it in action http://my-application-id.appspot.com/.

You may get an error suggesting you to use --enable_jar_splitting switch. This happens when Google founds a jar file too large to upload.
To fix this, open the command line on your project root folder and execute the following command: <path-to-appengine-java-sdk>/bin/appcfg.cmd --enable_jar_splitting update war.