Installing PHP 5 on IIS in 5 simple steps

Categories:

Set the PHP Application Mapping in IIS

Before IIS will parse PHP scripts, you must first create an application mapping for PHP. This application mapping basically tells IIS what ISAPI dll, or CGI executable to use to process files of a certain file extension (in this case .php). There are two ways in which we can do this, via the command line or via the IIS MMC snap in. I'm going to show you how to do both.

As this is a universal PHP installation guide for IIS 5.0, 5.1 and 6.0 there are limited universal ways in which to add the PHP application mapping via the command line. What ever you do though do not use the adsutil.vbs script to try and add you PHP application mapping, at least not without understanding it's limitations. When you add/delete an application mapping with adsutil.vbs there is no way you can add/delete a single item at a time. So by using adsutil.vbs to add an application mapping you will effectively delete every other mapping at that level. However, what you can do is use David Wang's awesome chglist.vbs script to add the PHP application mapping without effecting existing mappings. You can download this script from David's old blog or from the bottom of this page. Once you have saved the script, simply open a command window and change into the directory that contains your script and type this in;

chglist.vbs W3SVC/ScriptMaps "" ".php,D:\PHP\php5isapi.dll,5" /INSERT /COMMIT

This will add the PHP ISAPI script map to the root of the IIS metabase, and any sites created after adding this script will automatically inherit it. If you want to add the PHP application mapping to a specific website then you will need to modify the W3SVC/ScriptMaps section of the command to suite. If none of this makes sense at all, don't worry because all this can be done using the GUI as well which we will cover now.

  • Open the IIS MMC and right click on the Web Sites container (or click on any specific website you wish to add PHP to) and select the properties menu item


figure 6

  • Once the Web Sites Properties window opens select the Home Directory tab and click the Configuration button as shown below in figure 7.


figure 7

  • This will bring you to the Application Configuration window. Click the Add button at the bottom to add the PHP application mapping as shown below in figure 8.


figure 8

  • In the Extension field enter .php and then click on the browse button in the top right and select the php5isapi.dll file from inside you PHP installation directory (e.g D:\PHP). The default is to accept all verbs, but if you wish you can limit the verbs to tighten security for you PHP application mapping. Ensure that the Script engine and Check that file exists options as ticked and then press ok until you exit the properties windows.


figure 9

That is it, IIS should now be configured to serve PHP content. There is one exception though and that is if you are using IIS 6 then you will also need to configure a Web Extension, so if you are using IIS 5.0 or 5.1 you can proceed onto the next section. You can set the Web extension via command line using iisext.vbs or via the Gui. Using the iisext.vbs at the command prompt you'll need to type in this, but be sure to change the path to php5isapi.dll to suit your environment ;

iisext.vbs /AddFile D:\php\php5isapi.dll 1 PHP5ISAPI 1 "PHP 5 ISAPI"

To add the extension via the GUI, read on;

  • Open the IIS MMC and expand the server tree. Right click on the Web Service Extensions option and select the Add a new Web service extension option.


figure 10

  • In the Extension name field put in a descriptive name, such as PHP 5 ISAPI as shown below in figure 11. Make sure the Set extension status to Allowed option is checked and then click the Add button.


figure 11

  • In the Add file window click the Browse button and navigate to your PHP installation directory and select the php5isapi.dll file as shown below in figure 12. Then click the ok button until you exit back into the IIS MMC.


figure 12

All done, now your IIS 6 web server is all set to serve PHP content.

Comments

Brashquido's picture

For those of you that are

For those of you that are being prompted to download their php files rather than them being processes server side, double check you application mapping settings as per figure 9 above in the article. The ONLY way I have been able to successfully get IIS 5.1 to prompt to download a php file rather than process it is if the application mapping is incorrect. IIS will not let you save the mapping if the path to the php5isapi.dll is incorrect, so be sure that you have .php as the extension. If you have IIS Lockdown/URLScan installed, then you will need to add .php to the allowed extensions list.

Sorry about any spelling/grammar errors. I do my best, but unfortunately I am a product of the spell checker age and as the text editor on this site is very basic and doesn't have a spell checker there is bound to be the odd mistake that gets through. Feel free to point any mistakes out.

Anonymous's picture

This might be your problem

Brashquido, I believe I have my IIS and PHP5 set up correctly and this how I test it:

http://localhost/[serverDirectory]/phpinfo.php

so for example I test my phpinfo.php page as follows:

http://localhost/ServerDir/phpinfo.php where ServerDir is my virtual server directory.

I think a lot of people are testing their pages like this:

C:\[serverDirectory]\phpinfo.php

so it would be like I was doing this:

C:\ServerDir\phpinfo.php

This prompts me to download the .php file, if others are doing this just make sure you're testing it with the localhost url.

-Brandon

Brashquido's picture

That's a very good point

That's a very good point Brandon. For PHP scripts to be properly processed you need to access you PHP scripts via IIS, not windows explorer.

Anonymous's picture

Restart your server

Thanks for this excellent article Brashquido! I do have a newbie question though. When you say "Restart your server..", is it enough to stop and start the web services in IIS, or does the whole machine need to be rebooted?

Brashquido's picture

The main reason to restart

The main reason to restart your server is to update the environment varaibles. A restart might not be necessary, but it is one way to be sure the environment is refreshed properly upon install. Restarting your IIS services could well be enough. When you do it be sure that the Path variable from under the Environment section of the output of your phpinfo.php file contains the correct environment variables.

Anonymous's picture

Disabled Buttons

Hello Brashquido,
For some reason i could not add new mappings through the gui using iis5.0 because the OK button was disabled at the add menu in the configuration. But thanks to your great tutorial which points to a very nice script i could add the mapping. I have two more questions that i will be glad if you could answer. 1-Do you have an idea about why i am facing this strange problem about disabled buttons in the add mapping menu(i am new to web programming maybe its not strange but couldnt find info on the net)
2-My need was to add .html and php-cgi.exe mapping so that i could view embedded php. Now its working fine at last,but only for the root directory. So if i create a new virtual directory although it works for the root directory and i still see .html in the mappings for the new virtual directory, i cannot have the php code executed at the virtual directory- like in the initial situation.Why do you think this happens?
Thank you very much,
Dennis

Anonymous's picture

Re: prompt for downloading .php file

I got the same problem as well. Upon inspection, i noticed that the Application mappings for the "Websites" (the one that was added to in this article) and the Application mappings in "Default Web Site" (under Websites) were inconsistent.

I fixed this problem, by following the same instructions in this article on doing the application mapping, except, i right clicked "Default Web Site" and selected Properties->Home Directoiry tab -> Configuration button and did the mapping from there.

Brashquido's picture

That's correct. If you add

That's correct. If you add your PHP application mapping at the root, then only websites created after you add the mapping will inherit the mapping. If you have existing websites you wish to use PHP on then you'll need to add a PHP application mapping at the site level.

@Dennis

At a guess I would say that buttons are grayed out due to a permissions issue. Not sure though as it has been a few years now since I'e worked with IIS 5.0 in depth. Make sure you are using a user account with full administrator privileges. Second issue sounds like your execute permissions are not set to enable cgi. This setting is in your virtual directory properties under the virtual directory tab. Make sure it is set to scripts and executables. If you have any further issues I'd appreciate it if you could post them in the forums so it does not fragment the comments discussion on this article. I'll be more than happy to help you out.

Anonymous's picture

where is the popup menu?

Hi Brashquido,
I'm so new to PHP. Hope you can help me. I'm testing a php website in my computer 1st using IIS (running windows XP Pro). I already installed PHP 5.2.3 & pec, learned from ur tutorial, & also the configuration. The website i'm testing is begin with index.php contains popup menu from other directory i.e. /gbl/inc/menu_param.inc.php.
But i cannot let menu_param.inc.php go into index.php when typing "locahost" at Internet Explorer.
Or any settings i need set for that?

- William

Brashquido's picture

Hi William, I'm not 100%

Hi William,

I'm not 100% sure on exactly what you're asking, but from what I can gather is that you PHP app is not initialising a menu as it should. In the setup of some PHP apps you need to specify the URL the app will be served from, and if your app is accessed from any other URL then it might cause problems. Double check that you are accessing your app via the URL you specified during setup. You may need to add an entry to your hosts file so that the URL you specified resolves to your localhost, e.g www.yourdomain.com resolves to 127.0.0.1.