How To Install PHP on IIS 6.0

Windows OS Specific Topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How To Install PHP on IIS 6.0

Post by Saman » Fri Mar 12, 2010 5:40 pm

First , download the latest stable version of PHP zip package and the Collection of PECL modules from http://www.php.net/downloads.php

Note: You can use the Windows Installer package but it only installs and configures the CGI version of PHP which is not the best solution for an IIS web server; you should try to use the ISAPI version - if you really want to know more about why this ISAPI is preferable to CGI then I suggest you watch this IIS webcast :

TechNet Webcast: Comparing CGI and ISAPI in IIS 6.0 - Level 300
http://msevents.microsoft.com/CUI/WebCa ... tryCode=US

Install and Configure PHP
Start by creating a directory into which you will extract the downloaded PHP files (php-5.1.4-Win32.zip). In this example I’m going to use C:\PHP as my installation directory. Then extract the files from php-5.1.4-Win32.zip into C:\PHP

If you need to use the additional PECL modules then extract the files from ‘pecl-5.1.4-Win32.zip’ into the C:\PHP\ext directory.
1.png
1.png (6.01 KiB) Viewed 4720 times
Next locate the file ‘php.ini-recommended’ in C:\PHP and rename it to ‘php.ini’ (without the quotes of course)
2.png
2.png (46.81 KiB) Viewed 4720 times
Open the ‘php.ini’ file and find the line which reads extension_dir = “./” and change it to extension_dir = “C:\PHP\ext”. This tells PHP where the various extensions are located and as you can see the default path in the ‘php.ini-recommended’ file which ships with PHP points to the wrong location, so you need to change it.

You also need to add the location of your PHP directory to the server’s PATH environment variable so that Windows knows where to look for any PHP related executables (such as the PHP extension DLL‘s). To do this Right-click on My Computer, click Properties and on the Advanced tab click Environment Variables. In the Environment Variables dialog box, under System variables highlight the Path variable and click Edit.
3.png
3.png (11.26 KiB) Viewed 4720 times
Add ‘;C:\PHP’ (be sure to include the semi-colon separator) as shown here and click OK. You need to re-boot the server for this change to take effect.
4.png
4.png (4.15 KiB) Viewed 4720 times
If you browse through the ‘php.ini’ file you will see an entry describing the ‘cgi.force_redirect’ property. You will also see a statement telling you that if you are using IIS you ‘MUST’ turn this off. However, this only applies if you are using the CGI version of PHP (i.e. php-cgi.exe) Since we are using the ISAPI version of PHP we can safely ignore this - more details here : http://www.php.net/release_4_1_2_win32.php

You may have also seen various IIS and PHP How To guides which suggest that you need to copy your ‘php.ini’ and some other PHP related files to the C:\Windows\System32 directory - this is not actually necessary, as I shall demonstrate later in this walk through.

Configuring IIS
There are a few simple steps you need to take in order to get PHP working under IIS 6.0

First we need to create and then enable an appropriate Web Service Extension so that IIS will both recognise and allow PHP files to be processed by the appropriate script engine.

You can use the Internet Information Services (IIS) Manager GUI method to perform this task (and most tutorials and guides you may find tend to use this method) but there is a much quicker way of doing this; namely using the ‘iisext.vbs’ Command-Line Administration Script, which you will find in C:\Windows\system32 by default.

Assuming you are using the same directory structure as I am in this walkthrough you can simply copy and paste the following line of text and execute it at a command prompt from C:\Windows\system32 :

cscript iisext.vbs /AddFile c:\PHP\php5isapi.dll 1 PHPISAPI 1 “PHP ISAPI”
5.png
5.png (11.76 KiB) Viewed 4720 times
As you can see, this script creates a new Web Service Extension named “PHP ISAPI” with a status of Allowed.
6.png
6.png (26.17 KiB) Viewed 4720 times
The IIS 6.0 Command-Line Administration Scripts are very powerful and flexible tools and I would recommend using them wherever possible. You can find out more about administering IIS from the command line here :

Administering Servers from the Command Line in IIS 6.0

http://www.microsoft.com/technet/prodte ... x?mfr=true

You can use the default web site for test purposes if you prefer but in this example I am going to use a dedicated PHP test web site. I have created a test web site which has a home directory of C:\test. The next step is to create an Application Extension Mapping at the web site level for PHP scripts. You can perform this step using the MMC GUI but I am going to use AdsUtil.vbs for this demonstration.

There is actually a good reason for doing this - as I intend to use this web site purely for PHP testing I can remove any unused application mappings on my PHP web site, such as .idc, .shtml and .asp, etc.

I can achieve this simply by executing the following script from a command prompt at C:\Inetpub\AdminScripts :

cscript adsutil.vbs set W3SVC/87257621/root/Scriptmaps “.php,C:\PHP\php5isapi.dll,5?
7.png
7.png (11.85 KiB) Viewed 4720 times
Running the above command has the effect of setting the Scriptmaps metabase property for Web Site ID 87257621 so that only the .php Application Extension Mapping is enabled.
8.png
8.png (10.73 KiB) Viewed 4720 times
However, as you can see, this script doesn’t just add the PHP script mapping to the web site - it removes any existing mappings as well - so only use the above command if you are sure that you want to remove any existing application mappings on your chosen web site. In other words, if you run the above script against a web site which hosts any other applications (such as ASP) then it will break that site’s functionality and you will need to repair the application mappings manually. So only do this if you are sure that you only want to run PHP on your chosen web site.

#Note - if you are using your web site for development and you need to retain any existing application script mappings then you can use an excellent script provided by IIS guru David Wang which provides far greater flexibility than Adsutil.vbs. You can find David’s script here :

http://blogs.msdn.com/david.wang/archiv ... 73681.aspx






OK, now we are ready to test our PHP installation. Start by creating a simple PHP test file. Open Notepad on the server and copy the following line into a new text file :

<?php phpinfo(); ?>

Save the file as index.php in the root of the test web site. Next create a new default document type of index.php on the test web site (this step is optional but it just makes browsing a bit easier)
9.png
9.png (12.58 KiB) Viewed 4720 times
Browse to http://localhost (or http://localhost/index.php) and you should see the standard PHP configuration details page.
10.png
10.png (40.09 KiB) Viewed 4720 times
However, if you look carefully at the above page you will notice it is indicating that my ‘php.ini’ file is actually located in ‘C:\WINDOWS’ even though there is no such file in my C:\WINDOWS directory. This is because the ‘php5isapi.dll’ file is actually compiled with this location as its default value. A number of existing PHP and IIS tutorials suggest that you should copy the ‘php.ini’ file to the C:\WINDOWS directory - but what if you don’t want to do that ?

Well, you don’t have to because PHP allows you to actually configure a custom value for the ‘php.ini’ file location. There are a number of ways to do this but perhaps the simplest is to configure the PHPRC environment variable. Further details of how this works can be found here : http://uk.php.net/configuration

To demonstrate how this works I am going to create a new folder called ‘C:\inifile’ and instruct PHP to read its configuration data from the ‘php.ini’ file in this location (in practice you may prefer to leave your ‘php.ini’ file in the C:\PHP directory)

In order to do this I need to create a new System environment variable named ‘PHPRC’ and provide the appropriate values. Right-click on My Computer, click Properties and on the Advanced tab click Environment Variables.
11.png
11.png (12.39 KiB) Viewed 4720 times
In the Environment Variables dialog box, under System variables click New. In the New System Variable dialog box type PHPRC for the variable name and then enter the desired path to your ‘php.ini’ file’s location.
12.png
12.png (3.94 KiB) Viewed 4720 times
Then click OK and you will see that a new System environment variable has been created. In order for this to take effect you need to re-boot the server at this stage.
13.png
13.png (11.51 KiB) Viewed 4720 times
Note: As an alternative, you can edit the registry and specify the location of your configuration file. The main benefit of the registry edit method is that it doesn’t require a re-boot - in testing this method I found that any changes made would take effect once the application pool serving the web site was recycled. However, choose whichever method you feel most comfortable with and which fits your requirements.

Now if we browse http://localhost we can see that PHP is indeed looking for its ‘php.ini’ file in the ‘C:\inifile’ location which we specified previously.
14.png
14.png (31.68 KiB) Viewed 4720 times
And that’s it. You should now have a working installation of PHP running on IIS 6.0.

See also:
How to run PHP on 64-Bit Windows
How To Install MySQL to work with PHP on IIS 6.0
Post Reply

Return to “Windows”