PHP with Microsoft IIS and Microsoft SQL server

Tags:

You often hear that using PHP under Microsoft servers is a bad idea, because it doesn’t work very well.

But is that even true? You guessed it… no. It is actually possible to get a PHP based database application working under IIS and MS SQL Server perfectly fine, it just may take some configuration.

You can even get Apache mod_rewrite type of functionality under IIS with Isapi_Rewrite.

Why use IIS/MSSQL?

There’s a myriad of reasons for using IIS and MSSQL instead of the more traditional and proven Apache+MySQL combination for running PHP.

  • ASP.NET – You already have ASP or ASP.NET based applications
  • Familiarity – You’re more familiar with Microsoft products
  • etc.

There are also some advantages, like IIS’s caching features which can cache even dynamic pages, like the ones produced by PHP. You can also combine ASP pages and PHP pages under the same site.

Getting everything running

Nowadays you get PHP with FastCGI bundled with IIS 7, so getting PHP working with it is a matter of putting PHP files in your web root. With IIS 6, you’ll need to install PHP manually. You can find the official FastCGI extension and PHP for IIS 6 from Microsoft’s official IIS site.

In case your PHP files aren’t executed, you should check that PHP files are associated with the correct handler. At least with IIS 7, you’ll want to associate *.php with the PHP – FastCGI handler.

Connecting to the MS SQL database should work out of the box at least with PDO.

mod_rewrite with Isapi_Rewrite

For Apache’s mod_rewrite style functionality, you can use the Isapi_Rewrite module. The full version priced at $99 is reasonable, but you can also get the slightly limited Lite-version which is a free download.

The installation should be as simple as running the MSI installer. After that, be sure to read the documentation on the site. Note that for the Lite-version, you can only have one htaccess file, which affects your server globally.

With the Lite-version, since you’re limited to one htaccess file, you will have to work a bit differently. If you want to rewrite an URL under yoursite/example/ to yoursite/example/index.php, you’ll have to remember that the path will need to contain /example/ in it, because it’s only caught globally. If you have the full version, you should be able to just put a htaccess file under your example directory and use it like Apache’s .htaccess.

Possible compatibility issues with PHP

You should note that under IIS, you will get some different $_SERVER variables. This may cause some issues if you’ve used Apache-specific ones in your PHP code.

Another thing to note is that PHP’s short_open_tags feature is disabled by default, at least with the IIS 7 bundled PHP. This will effectively disable the tags, which are often used in Zend Framework view scripts for example. You may have to manually enable it, so if you use short tags, remember to check php.ini

Differences between MS SQL and MySQL

If you are not very familiar with MS SQL, you may run into several annoying and possibly confusing problems. Some database admin tools, such as Zend Studio, will run CREATE TABLE queries so that the table owner will not be dbo, and will instead use your login name. This may cause problems in statements, since if the owner is not dbo, you will have to prefix the owner’s name in the statement, like “SELECT id FROM Myuser.exampletable”.

An excellent reference for MS SQL’s Transact-SQL syntax can be found at MSDN’s Transact-SQL manual.