Using the CGI Servlet and HtAccessFilter with Jetty 6 to serve PHP applications

Scenario 1: Jetty configured as a general webserver

This approach configures a context to serve static files from the root context with *.php files to be processed by PHP and per directory .htaccess files to control access.

Drop this xml into $jetty.home/webapps/root/WEB-INF/web.xml

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

<!-- ===================================================================== -->
<!-- This is a template web.xml to serve as a root context                 -->
<!-- ===================================================================== -->
<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"
        metadata-complete="true" version="2.5">

        <description>Root Context</description>

    <!-- ================================================================= -->
    <!-- Run PHP in FastCGI mode and make sure it keeps running            -->    
    <!-- ================================================================= -->
        <listener>
                <display-name>FastCGILauncher</display-name>
                <listener-class>
                        au.com.lastweekend.cgi.FastCGILauncher
                </listener-class>
        </listener>

        <context-param>
                <param-name>FCGI_Launcher.php.command</param-name>
                <param-value>/usr/bin/php-cgi -b localhost:7543</param-value>
        </context-param>
        
        <context-param>
                <param-name>FCGI_Launcher.php.env.PHP_FCGI_MAX_REQUESTS</param-name>
                <param-value>1000</param-value>
        </context-param>


    <!-- ================================================================== -->
    <!-- Set the resourceBase for our Servlets and Filters                  -->
    <!-- ================================================================== -->
        <context-param>
                <param-name>au.com.lastweekend.servlet.resourceBase</param-name>
                <param-value>/var/www</param-value>
        </context-param>
  
        <!-- Also need to make the container's default servlet use the same     -->
        <context-param>
                <param-name>org.mortbay.jetty.servlet.Default.resourceBase</param-name>
                <param-value>/var/www</param-value>
        </context-param>
        

        <welcome-file-list>
                <welcome-file>index.php</welcome-file>
                <welcome-file>index.pl</welcome-file>
        </welcome-file-list>

    <!-- ================================================================== -->
    <!-- Check HtAccess authorisation for all requests                      -->
    <!-- ================================================================== -->

        <filter>
                <filter-name>htaccess</filter-name>
                <filter-class>
                        au.com.lastweekend.htaccess.HtAccessFilter
                </filter-class>
                <init-param>
                        <param-name>accessFile</param-name>
                        <param-value>.htaccess</param-value>
                </init-param>
        </filter>

        <filter-mapping>
                <filter-name>htaccess</filter-name>
                <url-pattern>*</url-pattern>            
        </filter-mapping>


    <!-- ================================================================== -->
    <!-- Apply the PathInfoFilter for our PHP requests                      -->
    <!-- ================================================================== -->
   
        <filter>
                <filter-name>pathinfo</filter-name>
                <filter-class>
                        au.com.lastweekend.pathinfo.PathInfoFilter
                </filter-class>
                <init-param>
                        <param-name>pathSpec</param-name>
                        <param-value>*.php</param-value>
                </init-param>
        </filter>

        <filter-mapping>
                <filter-name>pathinfo</filter-name>
                <servlet-name>PHP-CGI</servlet-name>
                <dispatcher>REQUEST</dispatcher>
        </filter-mapping>

    <!-- ================================================================== -->
    <!-- The CGIServlet to invoke PHP via FastCGI                           -->
    <!-- ================================================================== -->

        <servlet>
                <servlet-name>PHP-CGI</servlet-name>
                <servlet-class>au.com.lastweekend.cgi.CGIServlet</servlet-class>
                <init-param>
                        <param-name>CGI_commandPrefix</param-name>
                        <param-value>/usr/bin/php-cgi</param-value>
                </init-param>
                <init-param>
                        <param-name>FCGI_host</param-name>
                        <param-value>localhost</param-value>
                </init-param>
                <init-param>
                        <param-name>FCGI_port</param-name>
                        <param-value>7543</param-value>
                </init-param>
                <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
                <servlet-name>PHP-CGI</servlet-name>
                <url-pattern>*.php</url-pattern>
        </servlet-mapping>

    <!-- ================================================================== -->
    <!-- The CGIServlet to invoke Perl via Process exec                      -->
    <!-- ================================================================== -->
        <servlet>
                <servlet-name>Perl</servlet-name>
                <servlet-class>au.com.lastweekend.cgi.CGIServlet</servlet-class>
                <init-param>
                        <param-name>CGI_commandPrefix</param-name>
                        <param-value>/usr/bin/perl</param-value>
                </init-param>
        </servlet>

        <servlet-mapping>
                <servlet-name>Perl</servlet-name>
                <url-pattern>*.pl</url-pattern>
        </servlet-mapping>

</web-app>

Testing PHP

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.

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