h2. FastCGI and PHP: A User’s Story
p. !>http://farm3.static.flickr.com/2182/1715784373_a21d23e8e1_m.jpg! By: “Elizabeth Smith”:http://elizabethmariesmith.com/
p. Microsoft is releasing “FastCGI for IIS6″:http://www.iis.net/downloads/default.aspx?tabid=34&g=6&i=1521, meaning the shiny little “beta” tag is finally going the way of the dodo. What is FastCGI? I could go on for pages about the technical background, and Microsoft already has some great documentation on the subject, however I’ll put it in layman’s terms for those who aren’t Computer Science majors.
p. Unixy systems, like Linux, BSD, even OSX all use a “process” model instead of a “threading” model. They spawn a new process for new activities or jobs, and the systems are more optimized for this activity so the overhead is fairly small. Windows takes a threaded approach to the same problem; new activities or jobs spawn a new thread and the system is optimized for this, so spawning a new process is fairly resource intensive. CGI is a method that a web server can use for tools like PHP, Perl, or any other language that support it. CGI spawns a new process for each request, which can be really slow. FastCGI speeds this up with a very simple solution – instead of creating a brand new process for each request, it creates a “pool” of processes and reuses them. Without the constant processes startup and shutdown, you get a really fantastic speed increase. So what does this new technology being available on IIS mean for a PHP developer or IT professional? Well, let me tell you about my “day job”.
p. Most of my workday is spent dealing with Microsoft technologies. I have Windows XP for a desktop machine, use Microsoft SQL Server 2005 as a database backend, integrate web applications with Office and Microsoft CRM solutions, and work’s IT department knows every inch of those technologies. However, they haven’t a clue on how to install or manage other systems, and I really don’t have the time to do it myself, after all I need to be writing code, not administrating systems.
p. It wasn’t always that way. When I first began working there the environment was very mixed, Linux servers, a few Unix terminals here and there, and a fairly old NT domain. But then the old systems needed upgrading, and Microsoft seemed to have the solutions for most of the problems. And the new versions of Active Directory were really nice. So we slowly migrated. We eventually even gave up the old Linux servers and moved the code onto Apache2 and Windows Server.
p. When the mandate came down to move from our Apache servers onto IIS so we could install some .NET applications, we had a situation. A good deal of legacy PHP code was currently running on Apache and it would be both cost and time prohibitive to port, not to mention we’d have to buy third party software to achieve the same functionality we were currently getting from free extensions. But PHP can run on IIS, right? One of the greatest things about PHP is if you can compile it, you can run it.
p. So I helped set up PHP as an ISAPI module on the server, and it ran well, and ran pretty fast, at least comparable to Apache2 on Windows. But it crashed the server a minimum of once a day. The culprit was easy to find, we were using some extensions with thread safety issues. Many libraries and extensions used by PHP began life in the *nix world and so don’t have thread safety factored into the equation. Fixing it was a harder problem. We needed to use the extensions and couldn’t just rewrite the code. The solution to the crashing was to run PHP as CGI, but that was prohibitively slow.
p. Then I heard about the new FastCGI implementation for IIS. It was a beta, and generally it isn’t a good idea to run beta software on live systems, but anything was preferable to crashing every day or something so slow it was unusable even with a single tester. So I set up a test system (on a laptop) and it was beating the live server with regular CGI (a quad core beast) running our code. It even beat the server running with the ISAPI version of PHP. The test certainly convinced the boss, the live server had FastCGI beta installed.
p. And with that, our crashing issues vanished. I did run into a few bugs with the beta versions of FastCGI, but they were resolved with each new release. Moral of the story? FastCGI gave us a way to both run fast, and run stable on IIS without rewriting code or sacrificing non-thread safe extensions.
p. So what does this mean for your situation? If you’re interfacing with Microsoft technologies and PHP, and you’re currently running Apache2 and IIS you now have the option of a stable solution for PHP on IIS that doesn’t run like a snail. This means one less server to install and upgrade and worry about (unless you need Apache for other reasons). If you’re running Apache2 on Windows, you now have a competitor on the scene. IIS7 has some features (including an .htaccess like configuration that doesn’t kill performance) that might convince you to change servers. If you’re running in a mixed environment, you might want to consider moving to IIS, especially if you’re using Microsoft SQL Server somewhere in the mix, or don’t have an admin handy who knows how to keep the Apache server happy and fast. If you’re not running Windows anywhere, well, it might be worth it just to keep an eye on IIS.
p. If you’re interested in trying out FastCGI, and you’re using Vista, right now you have a problem. The available FastCGI install for Vista is only the beta version. The final version will be rolled into Service Pack 1 for Vista, so unless you’re brave enough to deal with the bugs in the Beta FastCGI, or brave enough to install a beta of the Service Pack, you’ll have to wait. The Server 2008 beta has FastCGI built in, so if you’re brave enough to give that a try you don’t even have to install anything extra.
p. For anyone currently on Windows XP or Server 2003, you can install IIS from your CD and download the FastCGI installer from “http://iis.net”:http://iis.net. Although installation is fairly easy, the configuration instructions can be slightly mind-boggling. Here is the quickest way to simply get the PHP FastCGI mapping set up.
p. First download a version of PHP, the PHP Windows installer is not (yet) configured to install the PHP fastcgi mapping (this is on the todo list, so if anyone wants to help out…) If you use the “non-thread safe” version of PHP available there (you don’t need threading support for FastCGI) you will see an additional speed increase. Unzip the download somewhere useful; “c:\php” and “c:\program files\php” are popular locations. Tada, you’ve installed PHP…but we do need to tell IIS how to use it.
p. This is going to take some command line magic. Don’t be scared. If you don’t know how to get a command line on your Windows machine, head to Start->Run and type in cmd (press enter). Now you have a nifty “dos box”.
p. The FastCGI installer put a magic script in %WINDIR%\system32\inetsrv that we’re going to use to tell IIS to use PHP with FastCGI. So type the following at your command prompt.
cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"C:\PHP\php-cgi.exe"
p. (If you put PHP at “C:\program files\php”, use that path instead of “C:\php” – remember Windows isn’t case sensitive)
p. Now you’re ready to stick a PHP file in your web root! If this scared you off a bit, the “Zend Core installer”:http://www.zend.com/products/zend_core/windows_preview will do FastCGI install and PHP configuration for you. You’ll also need to set up index.php as a “default document”, but IIS configuration is a topic for another day. Check out “http://iis.net/php”:http://iis.net/php for more information and in depth documentation.
p. Microsoft is (finally) stepping out and embracing some of the open source world. Whether you find this scary or exciting it does mean new tools for developers, and useful tools are always a good thing in my book. Take a look at your needs, and see if FastCGI can solve a problem for you.
p. “Elizabeth M. Smith”:http://elizabethmariesmith.com/about-me/ has been using PHP since time immemorial (PHP 4.0beta), but has used PHP 5 for so long now she’s forgotten how she ever got by without SPL and a real object model. Elizabeth is a certifiable (yes, we mean men in white coats and strait jackets) windows geek, if it can be compiled on windows, she’ll compile it, if not…she’ll fix it so it does. She enjoys doing very perverse things to Windows using PHP as well – all this in between caring for her 4 kids and husband.