Starting a local instance of IIS Express with PHP support

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.

After you have got used to the instant feedback of a browser refreshing immediately after you save your changes it can be difficult to go back to the old save / upload / refresh cycle of yore. Maybe you have a pre-processor cycle to build your CSS files or lint your JavaScript. Whatever your reason you can’t help but appreciate the advantages of running a web server on your dev computer.

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:

image

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:

  1. Load Web Platform Installer
  2. Search "php express” in the search box in the top right
  3. Click the Released column 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.
    image
  4. Your installer should have a button marked Add where the screenshot above says Installed. Click it.
  5. Click Install at the bottom of the Web PI Window.
  6. 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:

  1. Open a cmd window 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 CTRL on your keyboard and right clicking on the folder. Pressing down CTRL gives you two extra options in the context menu which are normally hidden.

    One is Open command window here which 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.

    Instead select 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).

    image
  2. Open your start menu, type Command and then when the search result comes up that says “Command Prompt” you need to right click and select Run as Administrator.

    image

    Confirm that you will allow it to make changes to your computer when asked.
  3. Once you’re in that command prompt get to the correct folder by typing cd /d and then right click and choose paste to paste your path in. The should be a space between the /d and the path you paste. Press Enter.

  4. Now that you’re in the correct project directory just enter runphp.cmd and press Enter.

    image

  5. 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:

     image
  6. 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.

image

If you right click it and then choose View All Applications you should see a simple window like this:

image

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:

image

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...

No comments :