The problem is, Moodle is written in PHP, and I am not a LAMP guy. I run Java, PostgreSQL, OpenSolaris, and GlassFish. (Thanks Sun for donating the server!) What's a JPOG guy going to do?

In this blog, I show three ways of running a PHP application on GlassFish. These all work with GlassFish and Moodle.

Quercus

Quercus is an implementation of PHP in Java. An interpreted version is free and open source, and a faster version can be licensed by those who feel the need for speed (which I do not). The Quercus folks will tell you that this is a more secure and scalable way of running PHP applications, which seems entirely plausible, provided, of course, that they did a good job with the PHP implementation.

I had my doubts, but Moodle is listed as a supported application and indeed, it worked like a charm.

Do the other things you need to do for Moodle: create a moodledata directory somewhere and make a moodle database with PostgreSQL (or, if you prefer, MySQL).

Deploy, using directory deployment. That way, you can keep making changes to the Moodle PHP files, which is the modus operandi when adding themes or plugins.

asadmin deploy /somewhere/moodle

(This is with GlassFish v3.)

Now point your browser to http://server:8080/moodle and run the Moodle setup process.

There are ominous warnings about missing xml-rpc and ssl libraries, but they haven't caused me any problems yet. Overall, this is very impressive and the simplest way of getting going for a non-PHP person.

Update 2010-05-16: Actually, I did run into issues with Quercus later and didn't have the time to resolve them. I then switched to the PHP/Java Bridge described below, and that held up without problems for the entire semester.

PHP/Java Bridge

The PHP/Java Bridge connects a JVM and a native PHP implementation. I tried this on Ubuntu Linux where I needed to install php5-common, php5-cli, php5-cgi, and php-pear. (Don't install php5—it shleps with it all of Apache, and the point of all this is not to install Apache.)

The installation procedure is similar. Download the latest PHP/Java Bridge binary from here. Download Moodle. Extract them into the same directory. Then you need to edit WEB/web-inf.xml and change the php_include_java parameter from the default On to Off, like this:

The advantage of using the bridge is that you are using genuine PHP, so there isn't any potential incompatibility issue. (The Quercus people will tell you that this is also a disadvantage—PHP is more prone to attacks than Java, and it is not all that fast.)

It is worth noting that both with Quercus and the PHP/Java Bridge, you can make Java calls from PHP code, which can come in handy if you want to extend the capabilities of your PHP program without the indignity of having to code in PHP.

jFastCGI

jFastCGI is another mechanism for making a servlet container serve PHP pages. I am guessing that it has a bit less overhead than the PHP/Java Bridge. It didn't work out of the box for me on Linux, but one of the developers, Barry van Someren, sent me a patch that worked. Here is how you can build it yourself.

Check out the latest code from SourceForge and build it. Curiously, the Ant script assumes that you have the servlet library on the global class path:

(The docs recommend port 6666, but for some reason, that is taken on my machine.)

Deploy the directory to GlassFish, and it works.

Summary

I am a Java guy and build my applications with JSF and JPA. But there is no denying that there is a lot of PHP code out there that does useful stuff. If you need to host a PHP app with your Java apps, you don't have to drink the LAMP Kool-Aid. There are multiple ways of making this work with GlassFish, which gave me the courage to go with one of them and use the others as a fallback.

Comments

Hi Cay,
Thanks again for helping me track down that bug with our interaction on Ubuntu php-cgi
I have a few more minor improvements in the pipeline (status page, JMX support)
Some of the bigger improvements are php-cgi failover (Julien has working code on that) and Async support (Servlet 3.0, or Atmosphere)
Thanks for the feedback and good luck with your PHP/Glassfish deployments
Your Captcha is broken.. it only has room for 1 digit and I got a lot of questions with the answer 10
Hex didn't work either :-/