Federico Cargnelutti

Simple is better than complex. Complex is better than complicated. | @fedecarg

Running PHP with Quercus in Jetty Web Server

with 14 comments

Jetty Web server can be invoked and installed as a stand alone application server. It has a flexible component based architecture that allows it to be easily deployed and integrated in a diverse range of instances. The project is supported by a growing community and a team with a history of being responsive to innovations and changing requirements. More info here.

Installing Jetty

First you need to download Jetty. It’s distributed as a platform independent zip file containing source, javadocs and binaries. The most recent distro can be downloaded from Codehaus:

$ wget http://dist.codehaus.org/jetty/jetty-6.1.14/jetty-6.1.14.zip
$ unzip jetty-6.1.14.zip
$ cp -R jetty-6.1.14 /opt/
$ cd /opt
$ ln -s /opt/jetty-6.1.14 jetty

Problems installing Jetty? More info here.

Running Jetty

Running jetty is as simple as going to your jetty installation directory and typing:

$ cd /opt/jetty
$ java -jar start.jar etc/jetty.xml

This will start jetty and deploy a demo application available at:

http://localhost:8080/test

That’s it. Now stop Jetty with cntrl-c in the same terminal window as you started it.

Installing Quercus

Quercus is a complete implementation of the PHP language and libraries in Java. It gives both Java and PHP developers a fast, safe, and powerful alternative to the standard PHP interpreter. Quercus is available for download as a WAR file which can be easily deployed on Jetty:

$ wget -P ~/quercus http://quercus.caucho.com/download/quercus-3.2.1.war
$ jar xf ~/quercus/quercus-3.2.1.war

Unpack the WAR file and copy all the jars to Jetty’s global library directory:

$ cp ~/quercus/WEB-INF/lib/* /opt/jetty/lib

Configuring Jetty

Edit the web.xml file:

$ vi /opt/jetty/webapps/test/WEB-INF/web.xml

Add the following between the web-app tags:

<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.html</welcome-file>
    <welcome-file>index.php</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

Create a PHP file inside the test application:

$ cat /opt/jetty/webapps/test/index.php
<?php phpinfo(); ?>

This file will be available at:

http://localhost:8080/index.php

It works! You are now ready to:

Instantiate objects by class name

<?php
$a = new Java("java.util.Date", 123);
print $a->time;

Import classes

<?php
import java.util.Date;

$a = new Date(123);
print $a->time;

Call Java methods

<?php
import java.util.Date;

$a = new Date(123);
print $a->getTime();
print $a->setTime(456);

print $a->time;
$a->time = 456;

And much, much more.

Written by Federico

January 4, 2009 at 6:39 pm

Posted in Java, Linux, PHP

14 Responses

Subscribe to comments with RSS.

  1. [...]  Running PHP with Quercus in Jetty Web Server () [...]

    vivanno.com

    January 5, 2009 at 5:40 pm

  2. [...] your languages together, you’re going to love this one: Federico Cargnelutti has posted an article about running your PHP programs under Quercus, a “100% Java implementation of PHP”, and [...]

  3. What about the performance. Did you do any benchmark ?

    MgX

    January 23, 2009 at 1:45 pm

  4. Hi,

    These directions are great! However, I followed these directions, and I keep getting an ERROR/404 when trying to display php files. Why doesn’t Quercus recognize the php files?

    thanks.

    Cindy

    January 24, 2009 at 2:49 am

  5. @MgX

    Benchmarks against PHP 5.2 with APC here: http://forum.caucho.com/?q=node/6

    @Cindy

    Forum and support http://forum.caucho.com/?q=forum/2

    Federico

    January 24, 2009 at 11:17 am

  6. I tried the directions again today, and it worked perfectly! Thanks for the great directions. Please disregard the previous message.

    Cindy

    January 24, 2009 at 4:41 pm

  7. I have been looking to give Quercus a spin for a while now, this howto will get me going, thanks!

    Robert Brainstorm

    February 3, 2009 at 5:39 pm

  8. I have set up Quercus over Tomcat and when I tried to run Dokuwiki (written in PHP) it came up with some bugs. Quercus is not a complete implementation of PHP.

    Michel

    February 26, 2009 at 4:45 am

  9. I couldn’t get this working in Jetty for the life of me with these directions. HOWEVER, if I just add the and sections to the etc/webdefault.xml file, it works flawlessly.
    Hope this helps someone.

    Phil Andrews

    March 18, 2009 at 11:06 pm

  10. [...] how to run PHP with Quercus in Jetty Web Server. Posted by Federico Filed in Java, PHP, Programming No Comments [...]

  11. Is it possible to run CodeIgniter through Quercus?

    Amit Kaushik

    April 14, 2009 at 2:15 pm

  12. work perfectly, great post, great QUERCUS !

    alessio

    May 19, 2009 at 4:36 pm

  13. If you experience problems with running certain PHP 5 application with Quercus, it my not be a PHP issue at all. This is true for running these same applications on IIS. It seems that some things require the use of APACHE Web server. This is what happens when you start to write code that is dependent upon a specific webserver or Operating System. It’s simply the fact that the PHP application is not written as portable code.

    Chaz Scholton

    June 6, 2009 at 10:02 pm

  14. Thanks mate for this post. I am running Quercus inside Jetty inside Grails. I can share some variables and session id between Grails and PHP! Plus, I have PHP as a tool inside my Grails web apps.

    Unfortunately Quercus is not always compatible with all PHP code, but I think it perfet for integrating Java and PHP. For Java we should think about Jython, JRuby, Groovy, Scala, Clojure…

    I think that the JVM can become a fantastic common execution environment, something like Parrot.

    Riccardo

    June 26, 2009 at 4:18 pm


Leave a Reply