Running Quercus in Jetty Web Server

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.

About these ads

18 thoughts on “Running Quercus in Jetty Web Server

  1. Pingback: vivanno.com

  2. Pingback: PHP, Java and Jetty | The Desi Video Blog

  3. 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.

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

  5. 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.

  6. 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.

  7. Pingback: PHP Support in Google App Engine « fede.carg ( blog )

  8. 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.

  9. 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.

  10. Pingback: PHP Support in Google App Engine « UR-Technology

  11. Jetty runs on Android, so You san develop php web appliactions served from localhost.that means tons of App expertise and programmers can enter The platform… please write again about this topic

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s