Tuesday, July 28, 2009

How to Run PHP 5.x with Apache Tomcat 4.x or Apache Tomcat 5.x

I am going to discuss here few steps to run php 5.x on Tomcat 4.x or Tomcat 5.x. We can enable php only in one web application as well as run globally for all applications under tomcat. I assume here, you have Apache Tomcat 4.x or 5.x already installed and running in your machine.

Steps:
1. Download latest php 5.x zip file. I used php 5.0.2. You can download it from here: http://museum.php.net/php5/php-5.0.2-Win32.zip
2. Download latest pecl modules 5.x zip file. I used pecl 5.0.2. You can download it from here: http://museum.php.net/php5/pecl-5.0.2-Win32.zip

Note: PHP version and PECL versions should be same. If you want to try with some other version get from here: http://museum.php.net/php5/

3. Unzip php zip in C: drive(I used C: for my installation, you are open to try new things)
4. Copy php.ini-dist, in c:\php, as php.ini in c: drive
5. Uncomment the line (remove semi-colon at the beginning) in php.ini:
;extension=php_java.dll
6. Extract php5servlet.dll from pecl zip file to c:\php. Ensure that the file is actually present in c:\php (maybe you can't see the php5servlet.dll file in pecl archive, it means you are trying this with newer versions of php)
7. Now create a directory under webapps in your Apache Tomcat installation. Lets say it is named test.
8. Create WEB-INF directory under test
9. Create lib directory under WEB-INF
10. Create web.xml under WEB-INF with the following contents:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>php</servlet-name>
<servlet-class>net.php.servlet</servlet-class>
</servlet>
<servlet>
<servlet-name>php-formatter</servlet-name>
<servlet-class>net.php.formatter</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>php</servlet-name>
<url-pattern>*.php</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>php-formatter</servlet-name>
<url-pattern>*.phps</url-pattern>
</servlet-mapping>
</web-app>

11. Extract php5srvlt.jar from pecl zip archive and extract or unjar (jar -xvf c:\php5srvlt.jar ) it under c:\
12. Modify both the files reflect.properties and servlet.properties to change the line library=phpsrvlt to library=php5servlet and save them. This indicates the file name of the dll file which is loaded by the Java application to serve the requests.
N.B.: This has no connection with the name of the jar file which can be anything. you may get this error “java.lang.UnsatisfiedLinkError: no php5servlet in java.library.path” for any mistake in this step. Please check the two properties file, whether there are any blank spaces. There should be only one line, and no blank spaces.

13. Re-create the jar file with the following command
(jar -cvfm php5srvlt.jar c:\META-INF\MANIFEST.MF -C c:\servlet\ /.), assuming you extracted the php5srvlt.jar in c: drive
14. Copy the jar file to WEB-INF\lib directory created earlier.
N.B: If you want to run PHP for one web application then do step 14 only but if you want to run php globally for all applications under tomcat then copy the php5srvlt.jar into commons/lib directory as well
15. Add c:\php to your System or User Path in Windows environment (Hint: Right-click and select Properties from My Computer -> then go to advanced tab -> click on environment variable)
16. create a new system or user variable PHP_HOME with value c:\php
17. Create a file index.php under test with the following code:
<?php phpinfo(); ?>

18. Restart Tomcat
19. Restart your machine(it is not required but though as we have added system variable so there is no harm doing a restart)
20. Start tomcat server again.
21. Open your browser and go to http://localhost:8080/test/index.php(if you set your port to some other port than 8080 please specify it in the place of 8080)
22. Ensure that there are no errors displayed. Instead you get an informative screen with php version information and whole lot of details.

Hope this helps. Have fun!!!
http://aurosblog.com/?p=481
http://gogetauro.com/blog/?p=143

3 comments:

Maneesh said...

Thanks Aurobindo. If I have some issues, I will get in touch with you.

Aurobindo said...

Hi maneesh,

its nice to hear from you and thanks. Best of luck. Please try to change your blogger profile age, its 253 years.

Cheers

Unknown said...

Still not able to connect even after following these steps!! Still getting this error

java.lang.UnsatisfiedLinkError: no php5srvlt in java.library.path