Thursday 26 January 2012

PHP running in Tomcat

It is fun to have PHP running in Tomcat and not too hard to achieve. Especially if you follow the instructions at PHP/Java Bridge or even here.

  1. Don't forget to Install and configure PHP on your machine or it won't work at all - duh!;
  2. From the JavaBridge Project add JavaBridge.jar, php-script.jar and php-servlet.jar to to the <tomcat home>/lib folder. (Do not be tempted to placed them in the ext folder as it will not work).
  3. Edit the <tomcat-home>/conf/web.xml file as follows:

        <filter>
            <filter-name>PhpCGIFilter</filter-name>
            <filter-class>php.java.servlet.PhpCGIFilter</filter-class>
        </filter>
        <!--
        <filter-mapping>
            <filter-name>PhpCGIFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        -->
        <!-- the following adds the JSR223 listener. Remove it if you don't want to use the JSR223 API -->
        <listener>
            <listener-class>php.java.servlet.ContextLoaderListener</listener-class>
        </listener>

        <!-- the back end for external (console, Apache/IIS-) PHP scripts; remove it if you don't need this -->
        <servlet>
            <servlet-name>PhpJavaServlet</servlet-name>
            <servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>
        </servlet>

        <!-- runs PHP scripts in this web app; remove it if you don't need this -->
        <servlet>
            <servlet-name>PhpCGIServlet</servlet-name>
            <servlet-class>php.java.servlet.fastcgi.FastCGIServlet</servlet-class>
            <load-on-startup>0</load-on-startup>
        </servlet>
       
        <servlet-mapping>
            <servlet-name>PhpJavaServlet</servlet-name>
            <url-pattern>*.phpjavabridge</url-pattern>
        </servlet-mapping>

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


    And at the end of the file:
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>index.php</welcome-file>
        </welcome-file-list>


  4. Create a new Tomcat project;
  5. Add a index.php file with this contents:

    <?php phpinfo();?>

  6. Start the server up and browse to it.
An alternate is to use Quercus which is a Java implementation of PHP which apparently supports the DBGp protocol which would be great for use with Liferay as it has the Quercus servlet as its default method of supporting PHP. The only problem is finding out how to debug the thing!!!

    No comments:

    Post a Comment