Compiling Java support into PHP
The first thing you will want to do is grab the PHP sourcecode from php.net. I am using the standard RedHat 7.2 PHP and Apache RPM's, but you will need the PHP sourcecode in order to create the actual Java extension. This does NOT require replacing your current Apache or PHP RPMs on your server (which eases things considerably).
Okay, after you have the PHP sourcecode (I used the PHP 4.06 sourcecode, as that is the current version in use on RedHat 7.2), you will need to download Sun's Java SDK. Make sure you grab the full SDK and not just the JRE (Java Runtime Environment).
Unpack the PHP sourcecode:
tar -zxvf php-version_number.tar.gz
Unpack the Java SDK RPM shell script (:
chmod +x j2sdk-version-number.bin
./j2sdk-version-number.bin
rpm -Uvh j2sdk-version-number.rpm
After you have both packages untarred and the JDK is installed (RPM install defaults to /usr/java), you need to make sure the system can find the new Java installation. You do this by altering your PATH variable. Edit the /etc/profile file by adding the following to the bottom of the file:
export PATH=/usr/java/j2sdk{VERSION}/bin:$PATH
Log out and log back in and issue the env command from the command prompt to verify the proper PATH variable is in effect.
Next, edit your /etc/ld.so.conf file, by adding the following two lines:
/usr/java/j2sdk{VERSION}/jre/lib/i386
/usr/java/j2sdk{VERSION}/jre/lib/i386/server
To makes these changes go into affect, type this at the command prompt:
ldconfig
Now "cd" into the /usr/java/j2sdk{VERSION}/jre/lib/i386/ directory (or wherever libjava.so is installed on your machine) and enter the following command:
ldd libjava.so
The response you get back should contain zero "not found" error messages. If you do get a "not found" message, you can add the path for the "not found" file to /etc/ld.so.conf and re-run ldconfig again. Do this until you remove all the "not found" error messages from the ldd libjava.so command.
Once you have all the path issues settled (be absolutely sure that all your PATH settings are working) it is time to compile your Java PHP extension. Enter into the PHP sourcecode directory, from the file you de-compressed earlier and enter the following command at the command prompt:
./configure --with-java
After the configure process finishes up, type:
make
This will take a few minutes to compile everything. After the compile is done, and there are no errors mentioned, you need to copy the libphp_java.so and the php_java.jar to the /usr/lib/php4/ directory (check your php.ini file to see what the "extension_dir" variable is actually pointing to and copy these two files there if it is different from /usr/lib/php4). libphp_java.so will be located in the modules/java directory, and php_java.jar will be located in the ext/java directory.
Now that you've got both the necessary files copied to your PHP extensions directory, you need to edit your php.ini file to add the appropriate directives. At the bottom of your /etc/php.ini file, add these lines:
[java]
java.class.path=/usr/lib/php4/php_java.jar
java.library=/usr/java/j2sdk{VERSION}/jre/lib/i386/server/libjvm.so
extension=libphp_java.so
Whew! That's it. Now simply restart Apache and you should be all set:
service httpd restart
As mentioned, the vast majority of trouble you'll run across is with path problems with the Java libraries. Also, if you mess up something while compiling the PHP sourcecode, be sure to do a make clean before trying to recompile. This should prevent any corruption and ensure that you are getting a good PHP-Java extension compiled.
I hope you enjoyed this tutorial. It took me a while to figure out how everything worked, but I believe it will be worth the trouble. PHP alone is an extremely powerful language, but coupling it with the power of Java as well, should make it a nearly unstoppable force of usefulness.
Bonus material (taken from the very informative php.net website):
Shove this into a webpage after you've done the above to test your new Java extension:
<?php
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System'); // demonstrate property access
print 'Java version='.$system->getProperty('java.version').' ';
print 'Java vendor=' .$system->getProperty('java.vendor').' ';
print 'OS='.$system->getProperty('os.name').' '.
$system->getProperty('os.version').' on '.
$system->getProperty('os.arch').' '; // java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"); print $formatter->format(new Java('java.util.Date'));
?>
