Skip to main content

100% Java Quercus PHP engine running in GlassFish Java EE 5 Application Server...

Posted by ludo on March 9, 2007 at 9:53 PM PST

Bonjour, Comment Java?

I spent a few hours playing with  Quercus, the  Caucho Technology's 100% Java implementation of PHP 5, trying to make it run with the GlassFish Java EE 5 application Server. Quercus comes with many PHP modules and extensions like PDF, PDO, MySQL, and JSON.
Download the quercus engine war file. Unzip this quercus-3_1-snap.war war file, and copy the 2 files quercus.jar and resin-util.jar from the WEB-INF/lib archive directory to the GlassFish lib/addons directory (GLASSFISHINSTALLDIR/lib/addons) If the GlassFish Application Server is not using JDK 1.6, but only JDK 1.5, you'll also need the javax.scripting.* JSR 223 apis. You can get the jsr223-api.jar file from the Phobos CVS repository, and store it as well under the GLASSFISHINSTALLDIR/lib/addons directory.

One final configuration file for php is needed. It is called GLASSFISHINSTALLDIR/domains/domain1/config/php.ini and should contain as a minimum this property value:

default_mimetype=text/html

Nothing more, nothing less. (If you are using a different GlassFish domain, just put this php.ini file in the domain's config directory.

So to recap, this is the files you should have on disk for configuring a PHP engine with GlassFish:

  • GLASSFISHINSTALLDIR/
    • lib/
      • addons/
        • quercus.jar (taken from the quercus-3_1-snap.war)
        • resin-util.jar (taken form the quercus-3_1-snap.war)
        • jsr223-api.jar (taken from Phobos CVS repository)
    • domains/
      • domain1/
        • config/
          • php.ini that contains default_mimetype=text/html

That's it...

Now using your favorite IDE (vi, or NetBeans), you can create a simple Web Application, and what you need to do is declare the PHP engine as a servlet and the location of the php.ini file, as follow:

xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" /> version="2.5">
>Caucho Technology's PHP Implementation, Running on GlassFish Java EE 5>
>
>Quercus Servlet>
>com.caucho.quercus.servlet.QuercusServlet>
>
>php-ini-file>

>php.ini>
>
>
>
>Quercus Servlet>
>*.php>
>
>
>index.php>
>
>

Now you are all set: this Web App is PHP ready. Just Add PHP files under the web folder area, where you would put your JSPs files.

The simplest index.php can be:

 echo "Hello World";
?>

If you want to test the Java interoperability, you can go wild and crazy with this:

  echo "Hello mom, look I have a date: ";
  $a = new java("java.util.Date", 123);
  echo $a->toString();
?>

Or even crazier, you can test that the Ajax jMaki project is working also in PHP applications, including the ones using the Quercus PHP pure Java engine running int he GlassFish Java EE 5 container...The sky is the limit.

A  complex PHP application  is MediaWiki.  Untar the mediawiki PHP application archive inside the WEB-INF area of your web application, and redeploy it (if you are using NetBeans, you have almost nothing to do as NetBeans does an update of the deployed build area when files are modified in the source area). Activate the Web Application at the correct URL to see if MediaWiki 1.9.3 home page is correct as below:

GlassFishPHPQuercus_html_72d1eeab.jpg

Here is the PHP index page...


# Initialise common code
require_once('./includes/WebStart.php' );

# Initialize MediaWiki base class

require_once("includes/Wiki.php" );

$mediaWiki = new MediaWiki();

wfProfileIn('main-misc-setup' );

OutputPage::setEncodings();
# Not really used yet


# Query string fields

$action = $wgRequest->getVal( 'action', 'view' );
$title = $wgRequest->getVal( 'title' );

$wgTitle = $mediaWiki->checkInitialQueries( $title,$action,$wgOut,
$wgRequest, $wgContLang );


if ($wgTitle == NULL) {
  unset( $wgTitle );
}

#
# Send Ajax requests to the Ajax dispatcher.
#
if ( $wgUseAjax && $action == 'ajax' ) {   
require_once( $IP . '/includes/AjaxDispatcher.php' );
 
$dispatcher = new AjaxDispatcher();

$dispatcher->performAction();
$mediaWiki->restInPeace( $wgLoadBalancer );
exit;

}

wfProfileOut('main-misc-setup' );
# Setting global variables in mediaWiki

$mediaWiki->setVal('Server', $wgServer );
$mediaWiki->setVal('DisableInternalSearch', $wgDisableInternalSearch );

$mediaWiki->setVal('action', $action );
$mediaWiki->setVal('SquidMaxage', $wgSquidMaxage );
$mediaWiki->setVal('EnableDublinCoreRdf', $wgEnableDublinCoreRdf );
$mediaWiki->setVal('EnableCreativeCommonsRdf', $wgEnableCreativeCommonsRdf );
$mediaWiki->setVal('CommandLineMode', $wgCommandLineMode );
$mediaWiki->setVal('UseExternalEditor', $wgUseExternalEditor );
$mediaWiki->setVal('DisabledActions', $wgDisabledActions );

$wgArticle = $mediaWiki->initialize ( $wgTitle, $wgOut, $wgUser, $wgRequest );
$mediaWiki->finalCleanup( $wgDeferredUpdateList, $wgLoadBalancer, $wgOut );

# Not sure when $wgPostCommitUpdateList gets set, so I keep this separate from finalCleanup

$mediaWiki->doUpdates($wgPostCommitUpdateList );
$mediaWiki->restInPeace($wgLoadBalancer );
?>

The setup page as rendered with Pure Java Quercus PHP engine on GlassFish: (Sorry, I did not have the time to configure the MySQL database yet)

GlassFishPHPQuercus_html_m251ad121.jpg

For more info, read http://quercus.caucho.com/ and the documentation under http://quercus.caucho.com/quercus-3.1/index.xtp .

The pure Java PHP engine is still an Alpha version, so please try it, break it and give feedback. The list of PHP applications the caucho team will be testing is very impressive:

And do not forget that the Ajax jMaki project is PHP friendly, and JSP friendly, and Server Side JavaScript Phobos friendly...

A Plus,

Ludo

Related Topics >>