Sunday, June 21, 2009

Glassfish + JFastCGI = Java + PHP together at last

I've been running PHP under fast-cgi with Sun webserver before and it had me wondering if this was possible in Java as well. Some searching on finding the Fast CGI specs brought up a reference to jFastCGI and lo and behold, all the heavy lifting has been done!

The code is simple and to the point and includes simple documentation. Here are some short steps you need to take to do this for yourself.

Ensure you have PHP with FastCGI support enabled.

php-cgi -i | grep fcgi

This should give you a result like:

name="module_cgi-fcgi">cgi-fcgi

This means fcgi support is compiled into the php-cgi binary, which is all you need on the php side of things. I'll see about including a simple project that does a bit more than just show phpinfo.

What you need is to add a Servlet to your webapplication using the following addition to your web.xml
XML removed, thanks a lot Blogger for crapping over the contents of my post. The XML can be found in the PDF I linked
Then just add the JAR files to your deployment (or place them in the correct folder on the server) and start your php-cgi (php-cgi -b6666)
All the PHP files in your web application will now be sent to the native cgi php version and the results be sent back to you.

A good sample would be:
phpinfo();
?>

Massive kudos to Jullien Railland for working on this!

So now the question rises, why would you rather do this than using Quercus?
Well not everything is supported natively under quercus and you also don't want to run PHP inside your JVM (or anything else native for that matter)
Worst that can happen now is the FastCGI process going down.

I'll try to post more information on doing a good setup of FastCGI which includes restarts on failure, user segregation and thread pooling.

2 comments:

Julien said...
This comment has been removed by the author.
Julien said...

Hi Barry, I'm Julien, the guy behind jFastCGI. I'm really glad to see you use, (and maybe enjoy) it !
Your improvements seem to be really interesting, feel free to contact me (julien_dot_rialland_at_gmail_dot_com), so we could discuss about it !