Sunday, December 30, 2007

Installing PHP 5.2.5 on Windows XP for Tomcat 6.0.13 - Issues resolved

I was playing around with the idea of doing a small personal PHP project, and I decided to do a fresh install of the latest version of PHP, which is 5.2.5. Since I'm already using the Apache Tomcat 6.0.13 server, I decided to run PHP on that instead of setting up the Apache HTTP server. I've done something similar before, so I didn't think it would be much of a problem. However, it is a bit more tricky than expected.

Windows PHP installer is broken

Initially, I used the Windows installer. The PHP install directory used was "C:\Program Files\PHP". Installing was a breeze and I could even use the php5srvlt.jar and put it into the WEBINF\lib directory without doing anything. The reflect.properties and servlet.properties files already have the line

library=php5servlet

instead of

library=phpsrvlt

So I followed the steps outlined. When I tried to access the test page, I got this.
HTTP Status 500 -
type Exception report

message
description The server encountered an internal error () that prevented it from fulfilling this request.

exception
javax.servlet.ServletException: Servlet.init() for servlet php threw exception
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
.....(edited for brevity)

root cause
java.lang.UnsatisfiedLinkError: C:\Program Files\PHP\php5servlet.dll: Can't find dependent libraries
java.lang.ClassLoader$NativeLibrary.load(Native Method)
.....(edited for brevity)

I checked the install directory and php5servlet.dll was there. What are the dependent libraries? After reading some of comments on Angusman's site and some discussions on Java forums, it turns out that the problem is due to missing DLLs. I downloaded a tool called Dependency Walker. It is very useful, I loaded php5servlet.dll and got the following.

Dependency Walker says php5ts.dll is missing

So php5ts.dll is missing. Searched for the file on PECL4WIN but could not find it. So I downloaded the zipped PHP files, then I noticed that there is an error in the original installer for PHP 5.2.5. Apparently the updated installer still has issue.

Steps I took to get it working

  1. Uninstall the initial install and remove all the files in the install directory

  2. Unzip the downloaded PHP zipped file into the install directory.

  3. Download php5servlet.dll from PECL4WIN and put it in the install directory. If you don't have it you will get an error message like:
    exception
    javax.servlet.ServletException: Servlet.init() for servlet php threw exception
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    .....(edited for brevity)

    root cause
    java.lang.UnsatisfiedLinkError: no php5servlet in java.library.path
    java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682)
    .....(edited for brevity)


  4. Edit the following paths in the php.ini file: include_path = "C:\Program Files\PHP\PEAR", doc_root = "C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.13\webapps", extension_dir = "C:\Program Files\PHP\ext"

  5. Uncomment a few extensions in php.ini and ensure the DLLs are in the extension directory, eg curl or bz2

  6. Save the php.ini file.

  7. If you right-click and php.exe, php-cgi.exe and php-win.exe and see the properties, you will see this message "Security: This file came from another computer and might be blocked to help protect this computer." I chose to unblock, but I'm not sure if there's any issue leaving it on.
    Security message

  8. Create web.xml, php5srvlt.jar and test page like what Angusman says in the appropriate directory in the webserver. If php5srvlt.jar is not in the correct directory you'll get an error message like this:
    exception
    javax.servlet.ServletException: Wrapper cannot find servlet class net.php.servlet or a class it depends on
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    .....(edited for brevity)

    root cause
    java.lang.ClassNotFoundException: net.php.servlet
    .....(edited for brevity)

    I already have them so I skipped this.

  9. Create the following environment variables PHP_HOME="C:\Program Files\PHP", PHP_HOME_EXT="%PHP_HOME%\ext", PHPRC="%PHP_HOME%". PHPRC is used to determine the location of the php.ini file but it is not the only place where it can be set. The locations being searched for the php.ini file follows a certain order. For my purpose this setting PHPRC is sufficient. If this is not set, the default location will be used, and may result in php.ini not being loaded, which in turn results in the configuration settings being set to some unexpected values, modules not being loaded as seen in phpinfo(), etc. Some examples.

    Wrong include path
    Wrong include path
    Wrong extension directory
    Wrong extension directory

  10. Append the environment variables to the PATH environment variable.

  11. Reboot so that the PATH changes are set. If not, you'll get some error like this where the environment variable appears correctly on it's own but not in the PATH.
    Computer needs to be rebooted for PATH changes to be effected

No comments:

Post a Comment