This article is the start of a series of articles on how PHP interoperates with other technologies. When people think of PHP, the first thing people think of is LAMP. But PHP is not limited to the run just under Apache, you can have PHP run under various configurations, including the command line. Here’s the first article in this series which shows you how to get PHP working in IIS.
A few weeks back I had attended the Virtual Tech Days events, specifically the Interop sessions. I was quite interested in seeing what Microsoft had to offer for PHP developers. Going through the oveview of the programs, they seem to have come up with quite a few solutions for PHP developers. One of the important integrations for PHP is the ability to run PHP directly from IIS, as a FastCGI app, without having to install Apache in Windows.
To make this task of installing PHP under IIS as easy as possible, Microsoft has provided Microsoft Web Platform Installer, which helps you install PHP and other server modules required to create your Web Platform. To get started all you have to do is to head over to http://php.iis.net/ and click on the install blue PHP button there. That launches the Microsoft Web Platform Installer interface which installs PHP along with the required dependencies required. If you’re on Windows XP, you’ll need Service Pack 2 more above and .NET framework 2 on your PC. I was pleasantly surprised with the ease of setting it up. Even though PHP works under IIS 6.0, it’s recommended that you run PHP under IIS 7.0 since some of the additional modules you’ll need (like the URL rewriting) are available on IIS 7.0.
The Microsoft Web Platform Installer installer works on Windows XP, Vista and Windows Server 2003 and 2008. Apart from installing the server components on your system, this installer also gives you quick installation of popular web applications like WordPress, Moodle LMS and Acquia Drupal to get you kickstarted in creating solutions in PHP on Windows. To learn more about it, check out – http://www.microsoft.com/web/Downloads/platform.aspx
At this point most LAMP developers would be asking the question – But how will my existing application port over to the new platfom? Microsoft does provide a few additional modules which priovide the same functionality on IIS:
Rewriting the URL:
If you are looking for a functionality which is similar to Apache’s mod_rewrite, which allows you to create custom URL, here’s the answer to your problem. URL Rewriter for IIS 7.0 helps you create such custom URLs through rules on IIS. You can install URL Rewriter for IIS using Microsoft Web Platform Installer as well. For more information and links to documentations on this module head over to: http://www.iis.net/extensions/URLRewrite.You can also import your existing Apache’s rewrite rules into IIS using the URL Rewriter. Here’s an artice which shows you how: http://learn.iis.net/page.aspx/470/importing-apache-modrewrite-rules/
.htaccess rules in IIS?
Now how do you get all your .htaccess rules to work on IIS? To do this you’ll have to translate the rules which you created in .htaccess to IIS 7.0′s web.config settings. The web.config in IIS 7.0 uses an xml style syntax to define rules so you will have to translate you current .htaccess rules into this syntax. Here’s an article which helps you learn more on this process with examples to illustrate the transformations between the two formats- http://learn.iis.net/page.aspx/557/translating-htaccess-content-to-iis-webconfig/
PHP Extensions in Windows
Now to get those PHP extensions which you use and love working on Windows. All PECL extensions for PHP (pecl.php.net) should work on Windows if you compile them. If you are looking for pre-compiled versions of these extensions for Windows, pecl4win.com used to host them earlier, but that seems to be down as of now. You can get some of the (unofficially) compiled PHP exenstions at: http://downloads.php.net/pierre/
To install an PHP extension, you’ll need to download the dll file for the extension. Then find the path to your extension directory in your php.ini. The path to the extensions should come in the line starting with extension_dir. If this is not set in your php.ini, you can create a new entry by adding this line in your php.ini –
extension_dir = c:/php/extensions/
Now you can copy the PHP extension dll’s you downloaded to this folder. To enable the extension you have to add the a line in php.ini in the following format: extension = abcde.dll, where abcde.dll is the file name of the dll you downloaded to the PHP extension directory. Restart your webserver and check in your phpinfo() to see if your extension has been loaded.
Improve your PHP Application’s Performance on IIS:
Once you’ve got your application working on IIS, here’s how to supercharge it. To boost the performance of PHP on your installation, you should check out the Windows Cache Extension for PHP. One of the ways this extension helps your improve the performance of your PHP application is by keeping the complied PHP bytecodes in memory for faster access and resuing this for subsequent accesses to the same scripts. Caching the opcodes in memory also helps reduce i/o overheads required to fetch the php scripts when they are accessed. To learn more about Windows Cache Extension for PHP and the link to the Microsoft Web Platform Installer installer for this module head over to : http://www.iis.net/extensions/WinCacheForPHP
All these tools and modules does ease the work of developers who need to deploy their applicatons in Windows without much effort. Overall, Microsoft has come a long way in getting PHP applications working on the Windows platform.
One you’ve got PHP working in Windows, you can start using the other Microsoft technologies like Silverlight for PHP and the PHP SDK for Azure. More on these topics in the upcoming series.