ASP.NET and PHP On IIS Together

phpUntitledI got a question the other day about running both ASP.NET and PHP on the same server and whether or not it works. The short answer I gave is “Yes”. The longer answer, I’m going to give here.

In point of fact, both ASP.NET and PHP work really well on the instance of IIS. They can’t share  session state but otherwise it’s a very peaceful existence.

First of all, PHP runs really well on Windows and IIS. There’s a lot of stuff that you probably didn’t know about it. Mark Brown and I did a talk about that at ZendCon – I’ll do a full write up of that talk sometime soon.

There are a ton of great resources out there for running PHP on Windows very well.

Easiest way to install PHP on Windows
Official site for PHP on IIS – Lots of good walk throughs and how to guides here.
RuslanY’s blog about IIS, FastCGI, PHP and other interesting stuff.
Brian Swan’s blog about PHP, SQL Server and more.
PDO driver for Microsoft SQL Server
SQL Server Reporting Services SDK for PHP
PHP: WinCache
Performance improvement in Joomla using WINCACHE user cache – great info on leveraging user cache.
Interop resources from Microsoft and PHP
URL Rewrite module for IIS7

I could keep going but this post is about running both ASP.NET and PHP on the same IIS instance.

Mike Volodarsky wrote a great article on leveraging the integrated pipeline in IIS7 that included leveraging ASP.NET Forms Authentication to restrict access to a PHP application. I’m not going to rehash that entire article but rather recommend that you leverage the applications that are up in the Microsoft Web Application Gallery such as Gallery. These applications are really simple to get set up on IIS7 through the wizard style installer in the WebPI.  

The first scenario that we’ll look at is a PHP application running as a subdirectory on the same server as an ASP.NET application. The first thing I’m going to do is get an ASP.NET application up and running through the Web Platform Installer (WebPI). I’m going to use BlogEngine.NET this demo. If you don’t already have the Web Platform Installer, it’s easy to install by going to http://www.microsoft.com/web/gallery/ and clicking "Install” next to any of the applications that you want to install. If you already have it installed, simply launch the Web Platform Installer, find BlogEngine.NET and start the wizard.

firstIf you want the application to be the root of the web server, then on the page where it’s asking about location, clear out the ‘application name’ field. It makes sense once you see it but it took me a second the first time that I tried it. I already had all of the dependencies installed for BlogEngine.NET so the rest was just following the wizard.

secondOnce BlogEngine.NET is installed, I can browse out to it and make sure that it’s all working. I can, if I want, do some configuration and change the title, skin and the like. However, that’s not the point of this little exercise so I’m going to skip it for the moment.

Now I’ve got an ASP.NET application up and running as the root of my server. Now I need to place the PHP application on the box to show them running side by side. The quick one that I want to install is Gallery which is a well known PHP application that does a good job at photo galleries. There are a number of galleries listed in the Web Application Gallery but the one that we want is “Gallery”.

ThirdAgain, I’m going to install the application through the WebPI. I have most of the dependencies installed already but I’m going to use SQL Server because I’ve already got one application using it, I might as well use it for the other. That’s going to require me to install the Microsoft Driver for PHP for SQL Server 2.0 and it’s associate that helps with IIS specifically. The good news is that these dependencies are selected and installed automatically by the wizard. Once the WebPI installer finishes, click “Launch Website” and finish out the installer.

At this point I’ve got BlogEngine.NET running at http://localhost and Gallery running at http://localhost/gallery proving out scenario one where PHP can run in a sub-directory under ASP.NET. The opposite is true as well but unless someone specifically requests it, I’ll assume that you can switch this example around and do it the other direction where you have a primarily PHP site with a little bit of ASP.NET running on the same server.

These different applications can share HTTP Handlers, URL Rewrite Rules, security rules (as stated in Mike’s article) and a bunch of different aspects of IIS. What they can’t share out of the box is session state. ASP.NET can store session in a number of places, including SQL Server and theoretically it’s possible to write a custom PHP session handler that would read from that same store but I haven’t tried it so I can’t guarantee that it’ll work.

In future posts, I’ll take a look at integrating HttpModules and HttpHandlers into the PHP pipeline and see how that works. In the mean time, have fun mixing and mashing technologies.

Leave a Reply

Your email address will not be published. Required fields are marked *