Quercus on the Google App Engine
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. For the last couple of weeks, I’ve been looking at Google App Engine and what its possibilities are for Quercus. Some folks from a PHP shop in Britain got Quercus running, but the version they were using was pretty old and seemed to come from a bizarre cross slice of our SVN repository. We wanted to make sure that the current version of Quercus runs on GAE with all its performance and compatibility enhancements. So Scott created a GoogleQuercusServlet just for the task. I wrote up how to get started using Quercus on GAE and some notes about what PHP can and can’t do within GAE at the moment.
PHP Guestbook on GAE
I started with the Guestbook demo application included with the GAE SDK and converted it to PHP. The interesting thing about this example is that it uses the DataStore from PHP using the Java integration. Here are the original JSP version of the guestbook “view” and the PHP version I translated:
|
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %> <%@ page import="javax.jdo.PersistenceManager" %> <%@ page import="com.google.appengine.api.users.User" %> <%@ page import="com.google.appengine.api.users.UserService" %> <%@ page import="com.google.appengine.api.users.UserServiceFailureException" %> <%@ page import="com.google.appengine.api.users.UserServiceFactory" %> <%@ page import="guestbook.Greeting" %> <%@ page import="guestbook.PMF" %> <html> <body> <% <form action="/sign" method="post"> </body> |
<html>
<head> <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" /> </head> <body> if ($user != null) { <?php if ($greetings->isEmpty()) { <form action="/sign" method="post"> </body> |
| guestbook.jsp | guestbook.php |
It’s a pretty straightforward translation of the JSP to PHP.
Now to configure it, you need to edit the web.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet>
<servlet-name>guestbook</servlet-name>
<servlet-class>guestbook.GuestbookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>guestbook</servlet-name>
<url-pattern>/guestbook</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>sign</servlet-name>
<servlet-class>guestbook.SignGuestbookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sign</servlet-name>
<url-pattern>/sign</url-pattern>
</servlet-mapping>
<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>guestbook.php</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
This is simple too. The main changes I made were to change guestbook.jsp to guestbook.php in the welcome file list, add the GoogleQuercusServlet, and create a servlet mapping. Nothing too complicated. This servlet is in our SVN right now and will be available in Resin 4.0, due out this week or next. To include it, you’ll need to put the whole resin.jar into your WEB-INF/lib.
Next up, we needed to make a couple of specific changes in the appengine-web.xml:
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application></application>
<version>1</version>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
<static-files>
<exclude path="/**.php"/>
</static-files>
<resource-files>
<include path="/**.php"/>
</resource-files>
</appengine-web-app>
The important part here is to notice that we mark php files as resource files and not static files. This way the GAE won’t serve up their source!
Notes about PHP on GAE
The Google App Engine shows terrific promise for running PHP applications, but there is still quite a bit of work to do. A lot of the issues come from the way that PHP is written currently.
- Most PHP applications deal directly with SQL, which GAE’s DataStore doesn’t provide. If you were willing to code Java entities and use them from PHP, you’d be set, but that’s probably a lot to ask. Thomas Mueller, the developer of the H2 Database is looking at using the datastore as a backend for H2, which exposes an SQL frontend. This is particularly exciting because H2 has compatibility modes for MySQL and Postgres, which most open source PHP apps use. I tried using H2 in place of MySQL for MediaWiki and didn’t quite get it working, but I let Thomas know about the incompatibilities and they don’t look that hard to overcome.
- A lot of PHP apps and frameworks assume a writable file system, which GAE doesn’t allow. Now this isn’t too bad either because as the implementors of the PHP file system functions, we can override them and use the DataStore as a backend. For data files, this works out fine.
- On the other hand, many PHP apps and frameworks write PHP code. If you want to those to be compiled, we’d need access to a Java compiler since Quercus goes from PHP -> Java -> Java bytecode. This isn’t something that GAE allows now and it even if it did, the compiler would need to write to the “real” filesystem, which it can’t. This isn’t too bad, as we can always fall back to interpreting the PHP from our faux filesystem backed by the DataStore, but ideally we’d like to compile those for performance.
- So far, Quercus only runs on GAE in interpreted mode. We just need to fix up our precompiler and maybe build an ant task for it. Not too hard, but not done yet.
If you’re interested in using Quercus on the Google App Engine, please let us know about it and what we can do to make it work. At the moment, we’re just playing with this because it’s a cool and fun new technology, but we see a lot of potential to make this a real deployment platform, so we’d like to hear from you about your interest level and specific issues.
Tags: google app engine, quercus

April 28th, 2009 at 3:22 pm
It is good to see this post as I, and others, were wondering if Caucho was going to look into smoothing out getting Quercus working on GAE in a less hackish way then the previous demos posted on the web.
My question, and I’m sure that of many others is, if you do look into formalizing the deployment (which I hope), how would you license it, it’s difficult to think of GAE in a traditional CPU count based sense, as in theory every time your process is run it is run by a different CPU, and at any given moment could be using 0, 1, 10, or even 100 different CPUs depending on demand, so your existing licensing model doesn’t fit.
April 28th, 2009 at 3:40 pm
That’s a great point. Right now, we haven’t worked out exactly how to bill. GAE clearly keeps records of how much computing power you’re using, so we might work out something with that in the formula. Of course, we’d need Google to expose that data to third parties (i.e. Caucho) before we can start billing. For something like EC2, it’s a little more straightforward, but the environments appeal to different groups.
May 6th, 2009 at 5:22 pm
Even in EC2 it’s not as straightforward as you would think. The pull towards using EC2 for web apps is the same as that for GAE, just with more administrative overhead. EC2 provides API access for launching and terminating servers, so the pull towards setting up an application in EC2 is that you can scale for demand by automatically launching servers or shutting down servers.
I have a PHP Based web app running on EC2 right now, and I use an automated scaler to spin up or shut down instances depending on the traffic. At this very moment, it’s running on 3 machines, at times it runs on 12, other times late at night it goes down to 2. There is no set upper limit on the number of systems it could run on at any given moment so I can’t say I will set a max on the number of servers to 15 and buy a 15 server license, when the scaler may decide it needs 16 at some point. And it’s also financially irresponsible to buy a license for 15 servers when 99.9% of the time I’m only running on a max of 4.
May 6th, 2009 at 8:02 pm
The solution we’re looking at there is to create a paid AMI that allows you to use Resin 4.0/Quercus at a metered rate. That way you only pay for what you use and you don’t need to buy anything in advance. That’s something we’re looking at for 4.0.1, but it’s not an official option just yet. I’ll definitely post something if we get something like that going.
May 17th, 2009 at 2:08 am
Hi, I have PHP running on GAE with the information posted elsewhere on the net. In the introduction of this blog posting you are saying that you have a newer version with “performance and compatibility enhancements”. Do you have instructions how to use that verrsion? Kind Regards, Herbert
May 20th, 2009 at 5:02 am
I’m thrilled by the thought of using Quercus like this. My organization has a strong PHP following, but adopting a Service Orientated Architecture as an enterprise is going to move us into Java. We’re also considering how to leverage the cloud to give us more flexibility.
The idea of being able to build one environment that runs both PHP & Java, and can sit on our own servers or in the cloud… well, that’s really a dream in my books. The lack of support for SQL is something we might work around with web services. It would certainly make us rethink when an RDBMS is really the best solution.
I’ve thought about testing out Quercus previously, but the ability to use App Engine with it is what’s going to have me finally try it out and see if it’s worth pitching to management.
May 20th, 2009 at 4:56 pm
Herbert - The version of Quercus I mentioned in the blog does have some performance and compatibility enhancements, but the compiled version of Quercus, which is even faster, is not currently available for GAE. We’re working on how to make that work both technically and in terms of licensing, but you can be sure we’ll make a big announcement when we straighten all the details out.
tjdett - Good point on RDBMS vs SOA. It could make sense for GAE, especially when you have a new PHP application. Existing apps are of course more difficult, which is why we’re looking at adding an SQL layer.
Thanks for the comments!
May 21st, 2009 at 12:54 am
Ok, I will wait
I’m now using resin.jar from resin 4.0, that version is better than the earlier found version on the web ( in that version I had problems with foreach and datastore methods like deletePersistent )
The integration between Java and PHP with Quercus is great! I have now database CRUD working in GAE/Quercus/PHP with JDO classes and PHP wrappers around that.
I think I will move some personal PHP stuff to GAE/Quercus/PHP and I will lobby in the company to move from a java only environment to a mixed java/php environment.
May 22nd, 2009 at 4:10 pm
Herbert - Sounds awesome! If you have a blog or somewhere else that you’ve written up your project, let me know and I’ll be sure to link to it.
May 24th, 2009 at 5:25 am
Ok, before converting PHP apps to GAE first the problem “What! no MySQL?” must be solved
Below a early attempt from me to solve this problem.
http://sql-to-jdo-for-php-at-gae.appspot.com
May 31st, 2009 at 11:35 am
Fantastic idea! Please provide some source code so everybody could install i.e. Wordpress or Drupal on Google App Engine. Thank you Caucho!
August 3rd, 2009 at 10:50 am
jiql, the JDBC Wrapper for Google DataStore, now integrates with Quercus.
So you can run PHP applications with your same SQL queries.
http://docs.google.com/Doc?id=dhg923vj_2ghr8q9f3#PHP
October 21st, 2009 at 3:31 pm
[...] Read more at Quercus on the Google App Engine [...]
February 5th, 2010 at 6:50 pm
[...] P.S. If you need a review of how to set up a Quercus application in the App Engine, check out this earlier blog post. [...]
April 15th, 2010 at 6:30 am
[...] Further research lead me to a blog post by the company that makes Quercus entitled: Quercus on the Google App Engine [...]
May 1st, 2010 at 11:33 pm
[...] it was time to get the Quercus PHP to work. Here’s a reference from the Quercus creators: http://blog.caucho.com/?p=187 and another useful page is here: [...]
May 26th, 2010 at 4:33 pm
Can’t wait for GAE integration with compilation!
I hope to see more examples using Quercus and GAE! Please share any examples on Twitter (#quercus #gae) or here
Thanks,