Miles to go …

August 23, 2007

PHP in GlassFish using Caucho Quercus

Filed under: web2.0 — arungupta @ 10:00 pm

Quercus is Caucho Technology’s 100% Java implementation of PHP 5. Ludo described the steps to deploy PHP web applications on GlassFish. Caucho has released a new version of Quercus since then. This blog entry is an update to the steps described earlier.

  1. First, PHP-enable GlassFish.
    1. Unjar quercus-3.1.1.war and copy the JAR files in "WEB-INF/lib" directory to "GLASSFISH_HOME/domains/domain/lib" directory. That’s it! Although the original entry requires to copy the JARs in "GLASSFISH_HOME/lib/addons" directory but that didn’t work.
  2. Create a PHP web application
    1. Create a new Web application project, lets say "hellophp", using NetBeans IDE and choose GlassFish as the server.
    2. Replace the contents of "web.xml" with the following fragment:

      <?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>
        </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>

      This will declare PHP engine as the servlet.

    3. Add a new page "index.php" in "Web pages" folder. The contents of the page are:

      <?php
      echo "Hello World!";
      phpinfo();
      ?>

      This page prints "Hello World!" on the browser and some configuration settings of PHP. The directory structure of the created project looks like:

      META-INF/
      META-INF/MANIFEST.MF
      WEB-INF/
      WEB-INF/classes/
      WEB-INF/sun-web.xml
      WEB-INF/web.xml
      index.jsp
      index.php

      Notice, "index.jsp" is only a template file to get started with JSPs and "sun-web.xml" is GlassFish-specific deployment descriptor. These files are not required for this PHP application although it does not hurt to leave them in the webapp as well.

  3. Deploy the application by right-clicking on the project and selecting "Deploy Project". Your first PHP application in GlassFish is now deployed at "http://localhost:8080/hellophp/index.php".

Now that you have verified that your GlassFish is ready to host PHP applications, try the different applications that are described in Ludo’s blog.

Technorati: php glassfish caucho quercus

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. jMaki, PHP and GlassFish – again using Caucho Quercus
  2. Debug application code deployed on GlassFish using NetBeans ?
  3. Ruby-on-Rails Hello World on GlassFish
  4. Database-enabled RoR WAR on GlassFish
  5. Rails and Java EE integration – Native Rails on GlassFish v3

9 Comments »

  1. [Trackback] jMaki is a light-weight framework for build Web 2.0 applications. It provides support for multiple languages – Java (1, 2, 3, 4, 5, 6) , PHP, Ruby (1, 2), Phobos (1). The numbers in parentheses indicate the entries that I’ve…

    Comment by Arun Gupta's Blog — August 24, 2007 @ 6:08 am

  2. Since your adding quercus capabilities to a an entire domain, you might as well modify config/default-web.xml .

    I imagine using NetBeans here is for further PHP->Java integration.

    Also, is Quercus 3.1.1 new?

    Comment by Alexis MP — August 24, 2007 @ 7:33 am

  3. Yeah 3.1.1 is new and does not require php.ini. NetBeans is useful for two purposes:

    1). It creates a template webapp project easily.
    2). The reason you mentioned.

    I’m not fully aware of the purpose of modifying default-web.xml. The name seems to indicate it’s the default web.xml if none is packaged in webapp. Is that right ?

    Comment by Arun Gupta — August 24, 2007 @ 7:48 am

  4. thanks

    Comment by 月饼 — August 26, 2007 @ 10:51 am

  5. Glassfish and Quercus works fine!

    But, what about datasources? I ‘ve been experimenting with the pg_connect() call in php to access a PostGreSql db with no success.
    There’s a good tutorial for Resin here http://quercus.caucho.com/quercus-3.1/doc/quercus-overview.xtp#databases

    How´s this done in Glassfish?

    Thanks

    Comment by Fredrik W — October 11, 2007 @ 1:09 am

  6. Is the datasource problem solved?

    I spend a lot of time trying to get pg_connect working, but no success :-/

    Thanks!

    Comment by Searle — March 8, 2008 @ 11:09 am

  7. The description above gives support for PHP per WAR-module which is cool. BUT if one want it generally it’s just to include this servlet definition in the file default-web.xml:

    <!– PHP support –>
    <servlet>
    <servlet-name>Quercus Servlet</servlet-name>
    <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>Quercus Servlet</servlet-name>
    <url-pattern>*.php</url-pattern>
    </servlet-mapping>

    Comment by Niklas Norberg — June 3, 2008 @ 4:03 am

  8. If anyone is trying to configure default-web.xml, use php not just in webapps but anything in docroot folder and also use a custom php.ini, I found this path works since there is no WEB-INF for docroot:

    <servlet>
    <servlet-name>Quercus Servlet</servlet-name>
    <servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
    <init-param>
    <param-name>ini-file</param-name>
    <param-value>../config/php.ini</param-value>
    </init-param>
    </servlet>

    If you use phpinfo() to display the php.ini location, it always says WEB-INF/php.ini no matter what. That must be a bug.

    Comment by pkcinna — June 19, 2008 @ 4:25 pm

  9. You need to add the datasource in your web.xml:

    <resource-ref>
    <res-ref-name>jdbc/base</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>

    I used PDO to access it:

    <?php
    $pdo = new PDO("java:comp/env/jdbc/base");
    echo "<table border=1>"
    $sql = "SELECT valor FROM Tabla1";
    foreach ($pdo->query($sql) as $renglon) {
    echo "<tr><td>$renglon[valor]</td></tr>";
    }
    echo "</table>"
    ?>

    For a complete tutorial (in Spanish), visit programacioncotidiana.blogspot.com/2011/02/tutorial-como-acceder-sybase-desde-php.html

    Comment by Arturo Tena — February 20, 2011 @ 2:37 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress