These instructions are for using PHP with Jetty 5.x only.

Jetty and PHP

Running PHP applications using the Jetty CGI servlet.
TODO: Find a FASTCGI servlet which by all accounts will have a significant impact on throughput and performance

Jetty as a general web server

You have Jetty configured to serve static files from the root context, and you want *.php files to be processed by PHP. Add the CGI servlet into the context as shown below.

TODO: Find a way to configure jetty so that *.php/some/path/info is passed to PHP with SCRIPT_NAME=*.php and PATH_INFO=/some/path/info

<!-- Set up the root context -->
<Call name="addContext">
   <Set name="ResourceBase">path/to/html/Or/PHP/files</Set>
   <!-- add miscellaneous handlers eg HtAccessHandler--->
   <Call name="addServlet">
      <Arg>Common Gateway Interface</Arg>
      <Put name="commandPrefix">/usr/local/bin/php-cgi-fix</Put>
      <!-- Possibly required for Windows installs
      <Put name="ENV_SystemRoot">C:\WINDOWS</Put>
      <Put name="ENV_REDIRECT_STATUS">200</Put>
   <Call name="addWelcomeFile">
   <!-- add more handlers, resource handler, not found etc... -->

PHP as a webapp

Your php app needs special environment variables for configuration or uses PATH_INFO information.

For example, yourapp.php/path/info expects yourapp.php to be processed by PHP with PATH_INFO=/path/info

Assuming jetty is configured to auto-discover web applications in a webapps directory and your php application files are in a directory yourapp.

Create a directory webapps/yourapp/WEB-INF and the standard webapps/yourapp/WEB-INF/web.xml

<code xml>
<?xml version="1.0" encoding="ISO-8859-1"?>


   <!- Path, other ENV_variables including ENV_SystemRoot, ENV_REDIRECT_STATUS on Windows -->

   <!-- Any other URL patterns that are needed by your app to be processed by PHP -->

 <!-- If you want http://yourhost/yourapp to call yourapp/yourapp.php then make a welcome file -->


If your app makes use of Apache's mod_rewrite functionality they have a look at URL Rewrite Filter and add the configuration to yourapp/WEB-INF/web.xml

<code xml>

htaccess Authentication

WEB-INF/jetty-web.xml can be used as normal to configure the HTAccessHandler. The index form is required because you need the authentication check to occur before the PHP scripts are called.

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "">
<Configure class="org.mortbay.jetty.servlet.WebApplicationContext">

  <Call name="addHandler">
     <Arg type="int">0</Arg>
        <New class="org.mortbay.http.handler.HTAccessHandler"/>

PHP Configuration

For this purpose PHP must be compiled as a cgi binary.

Refer to the PHP documentation for security considerations but be mindful that they are likely to be referencing the default Apache configuration and your Jetty configuration is likely to be highly customised.



In both scenarios above the command prefix is pointing to a script php-cgi-fix that works around the fact that PHP running as cgi uses a non-standard CGI environment variable SCRIPT_FILENAME to reference the script, rather than taking the first argument. In certain circumstances PHP will get a bad SCRIPT_FILENAME, then use SCRIPT_NAME to find and parse the file. This produces the expected HTML but also provides a "Not Found" header, (Status: 404) which can confuse some browsers.



Since Jetty 5.1.5 the CGI servlet sets SCRIPT_FILENAME, but not correctly in all cases


The phpinfo() function is very handy for testing your configuration as it will dump lots of information about the configuration.

Save the following to a file phpinfo.php into your webserver directory and navigate your browser to http://<yourjettyhost>/phpinfo.php and check the results are what you expect.

  <title>PHP Test</title>
 <?php phpinfo(); ?>