The Source for Java Technology Collaboration
User: Password:



Ludovic Champenois's Blog

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

Posted by ludo on March 09, 2007 at 09:53 PM | Comments (16)

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"?>
<web-app 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">
<description>Caucho Technology's PHP Implementation, Running on GlassFish Java EE 5</description>
<servlet>
<servlet-name>Quercus Servlet</servlet-name>
<servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
<init-param>
<param-name>php-ini-file</param-name>
<!-- the following value is a file name relative to the config directory
of the running GlassFish domain
for example GLASSFISHINSTALLDIR/domains/domain1/config/ dir
the php.ini file contains:
default_mimetype=text/html
otherwise the current Quercus will give a NPE
(will be fixed soon from Caucho team)!-->
<param-value>php.ini</param-value>
</init-param>
</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>

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:

<?php
echo "Hello World";
?>

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

<?php
  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...

<?php
# 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


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Great. All we need now is for the thousands of hosting providers around the world to deploy Glassfish instead of just Apache/PHP and we're all set!

    Posted by: goron on March 09, 2007 at 11:19 PM

  • Thanks a lot for posting this. I've been disappointed by how little attention Quercus has gotten, especially since many bloggers from Sun, NetBeans and on java.net have been writing about JRuby and other "scripting" language support. The Caucho team has done really impressive work IMO and deserves not just kudos, but also attention--they are much further along hosting a full, useful, alternate dynamic language on the JVM than JRuby is (and I don't mean that as a knock to the JRuby folks, it's just the fact of the matter). If Sun really wants to promote the JVM as multi-lingual, Quercus is a good case in point. Thanks again for this post. Cheers, Patrick

    Posted by: pdoubleya on March 10, 2007 at 12:27 AM

  • re: Goron -- we are working on it :-) - eduard/o

    Posted by: pelegri on March 10, 2007 at 07:51 AM


  • re: Patrick -- I, too, think well of the Quercus work... and I've been trying to give it more visibility, but a key difference between the jRuby work and Quercus is that jRuby is happening very transparently while it has been quite hard to get information on Quercus. I'd be very happy that that changed and this release seems a move in that direction; if the Quercus team continues to open up I think we can collaborate very nicely.

    I will be posting a spotlight on Ludo's blog in TheAquarium. I'll schedule it for 11pm PT on Sunday, that is as close to "prime" time as one can get from a World-Wide perspective. - eduard/o

    Posted by: pelegri on March 10, 2007 at 07:59 AM

  • Alternate config approach and short video here

    Posted by: alexismp on March 10, 2007 at 02:29 PM

  • Very cool ludo! What license is Quercus distributed under? It claims to be "open source" but I could never find a place to get the source alone. At any rate, having this working in GlassFish is really excellent.

    Posted by: headius on March 13, 2007 at 03:48 PM

  • re Headius: license is GPL. Src is under http://quercus.caucho.com/download/quercus-3_1-snap-src.jar

    Posted by: ludo on March 13, 2007 at 03:53 PM

  • See my quercus scripting experiments here.

    Posted by: axelclk on March 19, 2007 at 03:27 PM

  • Hi Ludo,
    Great entry. The initial hello world worked fine for me. Now I'm trying to get Mediawiki and Gallery applications to work with Glassfish. But in both applications, it always fails to connect to my mysql database. I get this error :

    Warning: A link to the server could not be established. url=jdbc:mysql://localhost:3306/ driver=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource com.caucho.quercus.QuercusModuleException: Can't find database for driver 'com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource' and url 'jdbc:mysql://localhost:3306/' [mysql_connect] Error connecting to mysql

    Is it mandatory to register a data source with jndi? Can't I connect to the MySQL database with a different driver like : com.mysql.jdbc.Driver?

    It would be great if I can get this to work properly. Any pointers would help greatly :)

    Posted by: karthikks on April 07, 2007 at 01:54 AM

  • I'm having the same problem as karthik, I can't get MediaWiki to see my mySQL DB.

    Warning: A link to the server could not be established. url=jdbc:mysql://localhost:3306/ driver=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource com.caucho.quercus.QuercusModuleException: Can't find database for driver 'com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource' and url 'jdbc:mysql://localhost:3306/' [mysql_error] failed with error [0] .

    Any solutions? Without DB access, PHP is somewhat useless on Glassfish.

    Posted by: moazamraja on July 22, 2007 at 10:54 PM

  • I am running experiments on my mediawiki site. It is running on my in GlassFish Java EE 5 Application Server. Trying to make the link to the mysql was the hardest part

    Posted by: dynasource on January 14, 2008 at 03:03 PM

  • I had the same problems as karthik as well. With your help it seems to work. Here is my test site.
    Thanks for the recap of all files one should have on the harddisk for configuring a PHP engine with GlassFish.


    Posted by: waarborg on January 15, 2008 at 01:39 PM

  • Hi folks

    First off, make sure you are using the 3.1.4 release of Quercus when setting up a mediawiki site. Also, the "A link to the server" error from
    mysql_connect() could mean a few things. One, your mysql server might not be running or could not be contacted via the network. Two, the username and password might not be valid on the server. Check that by testing the username and password in the mysql command line program. Also, be sure to use ConnectorJ 3.1.14, the mysql-connector-java-3.1.14-bin.jar should be on the CLASSPATH of your environment.

    Posted by: mo_at_caucho on January 16, 2008 at 06:47 PM

  • Ludo, I was able to get a php webapp working with Glassfish V2 (9.1ur1). Thanks for your blog.
    The only difference, I make when configure Quercus for Glassfish V2 server, is to copy the jar files to "GLASSFISHINSTALLDIR/domains/domain1/lib" directory instead of "GLASSFISHINSTALLDIR/lib/addons"

    Posted by: davisn on April 03, 2008 at 12:00 PM

  • When i try to run a php file i get this error: javax.servlet.ServletException: 'php-ini-file' is not a recognized init-param

    I use glassfish V2 and Netbeans 6.1.

    Posted by: nalle on May 21, 2008 at 02:41 AM

  • Forget my last posting.. i got that to work. Now i got another problem =) Something about encoding. The error looks like this:
    com.caucho.quercus.QuercusModuleException: com.caucho.quercus.parser.QuercusParseException: /C:/nbprojekts/build/web/tinymce/filemanager/includes/general.php:7: java.io.CharConversionException: illegal utf8 encoding at (169)
    Check that the script-encoding setting matches the source file's encoding. What is wrong?

    Posted by: nalle on May 21, 2008 at 03:27 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds