Configure Tomcat 6 for PHP 5
This guide shows how to install PHP 5.x or above into Tomcat 6 or above.
Download and install Java, Tomcat and PHP
Unix/Linux
- Use the graphical packet installer or the command
yum install phpto install PHP. - Download and install Java 6 or above.
- Download and install Tomcat.
Windows
On Windows you need to install Java, Tomcat and PHP manually:
- Download and install Java 6 or above.
- Download and install Tomcat.
- Download and install PHP. Select a "typical installation" and install "other cgi".
- Reboot your windows machine after the software installation.
Install a PHP web application into Tomcat
- Copy the PHP web application JavaBridgeTemplate.war to the Tomcat webapps directory.
- Wait two seconds until Tomcat has loaded the web application.
- Browse to http://127.0.0.1:8080/JavaBridgeTemplate554 and http://127.0.0.1:8080/JavaBridgeTemplate554/test.php to see the PHP info page.
- Congratulations, you have successfully installed a PHP web application into Tomcat.
PHP support for all existing Tomcat web applications
- Stop Tomcat.
- Move the libraries
JavaBridge.jar,php-servlet.jarandphp-script.jarfrom thewebapps/JavaBridgeTemplate554/WEB-INF/libdirectory over to the tomcat lib directory. - Edit the Tomcat
conf/web.xml. Add the following 9 lines marked with a +:
<web-app xmlns=... >
+ <listener><listener-class>php.java.servlet.ContextLoaderListener</listener-class></listener>
+ <servlet><servlet-name>PhpJavaServlet</servlet-name><servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>
+ </servlet>
+ <servlet><servlet-name>PhpCGIServlet</servlet-name><servlet-class>php.java.servlet.PhpCGIServlet</servlet-class>
+ <init-param><param-name>prefer_system_php_exec</param-name><param-value>On</param-value></init-param>
+ <init-param><param-name>php_include_java</param-name><param-value>On</param-value></init-param>
+ </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>
...
</web-app> - Start Tomcat again. Now you can add PHP scripts to tomcat.
- Add a PHP test file
to some web context, for example "examples", and browse to<?php echo java("java.lang.System")->getProperties(); ?>http://yourHost.com:8080/examples/test.php. - Check the process list using the Unix/Linux or Windows task manager. There should be 5
php-cgiFastCGI executables waiting for requests from Java. - Please see our FAQ for more information how to set up a load balancer or how to create a distributable PHP/Java web application.