If you're a developer that works in Microsoft Visual Studio you’re probably quite used to the handy local web server that appears when you summon it with an F5. There are many alternative ways to spin up a local web server. Recently I’ve used virtual machines, Node, even PHP comes with its own built in dev server. They all have their strengths but this article shows you how to keep it in the family with a quickly deployable instance of IIS Express that supports both .Net and PHP.
Today I’m going to show you how quickly and easily it is to get the best of all worlds; a server that can run your asp.net sites, serve up the static html sites that you might have built a while back using Dreamweaver templates or some such and it wont even flinch when it comes across your PHP powered contact form.
The server that can do all of this is IIS Express. It will already be installed on your computer most likely. It comes with Web Matrix, one of Microsoft’s hobby development tools. It is now also the server of choice on modern versions of Microsoft Visual Studio.
The next few sections will guide you though the one-off installations and then the final section will show you the one-liner you can use in the future to start your PHP & Asp.net enabled server.
Get the Web Platform Installer
All of the required tools can be found in Microsoft Web Platform Installer. Search your start menu for “Web Platform”. If you don’t have that you can normally get it from its homepage here:
At the time of writing though, that page seems to have been broken for the last few days. In case you can’t access it still then you can get a direct download here:
Get IIS Express
You probably have this already installed. Check by looking for this folder:
C:\Program Files (x86)\IIS Express\
If that doesn’t exist then load up Web Platform Installer and search using the box in the top right. Just type
IIS Express and press enter:
For me it wasn’t listed as installed because my copy came with either Web Matrix or Visual Studio so its worth checking that folder first.
Get PHP For IIS Express
After you have IIS Express installed you need to get the PHP addon for it. Steps are as follows:
- Load Web Platform Installer
- Search "
php express” in the search box in the top right
- Click the
Releasedcolumn to sort the results by release date. This is useful so you can make sure you install the latest PHP version. At the time of writing the latest version for me is
PHP 5.6.0 For IIS Express.
- Your installer should have a button marked
Addwhere the screenshot above says
Installed. Click it.
Installat the bottom of the Web PI Window.
- For me, it also wanted to install a second package named
Microsoft Drivers 3.2 for PHP v5.6 for SQL Server in IIS Express. Let it do this and confirm the licence agreements when it asks you.
That’s it! From now on you can start up your server with one simple command.
After I show you how in the next section, please make sure to read the two gotcha’s that I experienced while setting this up so that you don’t get tripped up.
Launching a web server from your project folder
The crucial command you need to type is a simple script file called
runphp.cmd. Follow these steps:
- Open a
cmdwindow using your favourite terminal software. If you aren’t already thinking of your preferred command line window then you can do it using built in tools by pressing
CTRLon your keyboard and
right clickingon the folder. Pressing down
CTRLgives you two extra options in the context menu which are normally hidden.
Open command window herewhich would be very handy as it would put us right in the place we wanted to be but unfortunately it doesn’t open it with Administrator privileges.
Copy as path. This will copy the path which we can paste in later. It should be just near Send to (so quite far down if you have a lot of utilities installed).
- Open your start menu, type
Commandand then when the search result comes up that says “Command Prompt” you need to right click and select
Run as Administrator.
Confirm that you will allow it to make changes to your computer when asked.
- Once you’re in that command prompt get to the correct folder by typing
cd /dand then right click and choose
pasteto paste your path in. The should be a space between the /d and the path you paste. Press
- Now that you’re in the correct project directory just enter
- The screen will fill with lots of scrolling text that will make you briefly feel like the ultimate hacker and you will hopefully see this at the end:
- Set the window aside, you will want to leave it open for the duration of your development session.
That’s it! It was a long journey to get here but just remember that next time all you really need to do is open an command window in administrator in your project directory and runphp.cmd.
Viewing your website
In the system tray near your clock you should see a little icon with stacked silver boxes.
If you right click it and then choose View All Applications you should see a simple window like this:
Click on the
localhost link and it will show your site in the browser. For me, I just added a simple
index.php to the folder with a
phpinfo() call to be sure my PHP was working:
Gotcha to look out for
WebMatrix will auto install an older PHP v5.3 package the first time it sees a PHP website. If you already installed a PHP for IIS Express which targeted a newer version of PHP then it will break things and all your sites will now run on PHP v5.3.
The solution is to just re-order your PATH environment variable so that the version of PHP you want to run is the first one in the list (or the only one, up to you).
Edit your PATH env variable by just typing
environment into your Start Menu or into the Control Panel search box. You should be presented with an option something like “Edit the system environment variables”. Click it and then click the
Environment Variables button in the bottom right of the screen that opens.
PHP for IIS Express installs to
C:\Program Files (x86)\IIS Express\PHP\ by default.
For me I had v5.3 and v5.6 paths set in my
PATH and ended up with v5.3 first in the path so
runphp.cmd always ran the older php v5.3 each time rather than the v5.6 like I wanted.
Conclusion and lessons learned
So that’s it. You can now quickly and easily open an administrator command prompt in your project folders and
runphp.cmd to spin up an IIS Express server. You can run Asp.net, PHP or plain old html sites.
There is actually a Node for IIS Express package out there as well but that’s a tale for another day...