Freigeben über


HOWTO: Install and Run PHP on IIS7, Part 2

A couple of months ago, I wrote a quick and dirty entry on how to install PHP on IIS7. The main purpose of that entry was to explain the details of what was going on as well as cookie-cutter instructions of one way to successfully install PHP on IIS7.

Well, the responses that I have received from that blog entry made me realize that I need to provide something a little more shrink-wapped which does a few more things other than just run the bare minimum... because I was seeing way too many broken custom modifications coming from mistaken assumptions about PHP or IIS7.

Here it is, V2. Just copy/paste the following into a .bat file, right-click run it as elevated Administrator on Vista, and follow the prompted instructions. You should have PHP extracted into a directory of your choice before-hand (I favor and default to %SYSTEMDRIVE%\Inetpub\PHP for many aforementioned reasons; in my examples, I chose "C:\Program Files\P H P" to show it working with long pathnames).

The batch script:

  • Works with PHP installed wherever, including pathnames with spaces. Just tell it where you installed PHP (sans double quotes and trailing backslash)
  • Gives choice of whether to use the CGI or ISAPI version of PHP. You do have to give the correct binary name (php5isapi.dll or php-cgi.exe, assuming PHP5), but there are checks for that
  • Gives warnings and errors if the directory/file does not exist, mismatched binary types, and additional steps you need to do to have a minimally functioning PHP

Now, I am no PHP expert, so I can only give instructions for how to get PHP configured and running on IIS7. Questions about all other PHP-related features (like PHP extensions, integration with mySQL, etc) and how to get them working really belong on a PHP support forum.

Sample Execution Results:

  • Setting up PHP5 installed at "C:\Program Files\P H P", ISAPI version. Notice the defaults and verification of provided inputs.
 David.Wang's Sample PHP/IIS7 Configurator
Version: June 2006

------------------------------ Summary ------------------------------
PHP Binaries Dir : C:\Inetpub\PHP
PHP Binary Type  : ISAPI
PHP Binary Name  : php5isapi.dll
---------------------------------------------------------------------

Validating inputs...

ERROR: PHP Binary "C:\Inetpub\PHP\php5isapi.dll" does not exist!
Please first completely extract PHP to "C:\Inetpub\PHP"

Remember to tweak PHP.INI for security and functionality per php.net
Finished input validation.

Press 1 to EDIT choices, or ENTER to start IIS modifications:1

Press ENTER to accept [C:\Inetpub\PHP], or provide new value (folder path)
PHP Binaries Dir:C:\Program Files\P H P
Press ENTER to accept [ISAPI], or provide new value (CGI or ISAPI)
PHP Binary Type:
Press ENTER to accept [php5isapi.dll], or provide new value (filename)
PHP Binary Name:

David.Wang's Sample PHP/IIS7 Configurator
Version: June 2006

------------------------------ Summary ------------------------------
PHP Binaries Dir : C:\Program Files\P H P
PHP Binary Type  : ISAPI
PHP Binary Name  : php5isapi.dll
---------------------------------------------------------------------

Validating inputs...

Remember to tweak PHP.INI for security and functionality per php.net
Finished input validation.

Press 1 to EDIT choices, or ENTER to start IIS modifications:

Starting IIS7 Configuration...

Copying "C:\Program Files\P H P\PHP.INI-Recommended" to PHP.INI...
Setting PHP Handler...
CONFIG object "system.webServer/handlers" changed
Adding and Enabling PHP in ISAPI/CGI Restriction List...
CONFIG object "system.webServer/security/isapiCgiRestriction" changed

Finished IIS7 Configuration.

Test installation using PHP file content of:  <?php phpinfo();?>
  • Setting up PHP5 installed at "C:\Program Files\P H P", CGI version. Notice the defaults and reminders as you change to CGI.
 David.Wang's Sample PHP/IIS7 Configurator
Version: June 2006

------------------------------ Summary ------------------------------
PHP Binaries Dir : C:\Inetpub\PHP
PHP Binary Type  : ISAPI
PHP Binary Name  : php5isapi.dll
---------------------------------------------------------------------

Validating inputs...

ERROR: PHP Binary "C:\Inetpub\PHP\php5isapi.dll" does not exist!
Please first completely extract PHP to "C:\Inetpub\PHP"

Remember to tweak PHP.INI for security and functionality per php.net
Finished input validation.

Press 1 to EDIT choices, or ENTER to start IIS modifications:1

Press ENTER to accept [C:\Inetpub\PHP], or provide new value (folder path)
PHP Binaries Dir:C:\Program Files\P H P
Press ENTER to accept [ISAPI], or provide new value (CGI or ISAPI)
PHP Binary Type:cgi
Press ENTER to accept [php5isapi.dll], or provide new value (filename)
PHP Binary Name:php-cgi.exe

David.Wang's Sample PHP/IIS7 Configurator
Version: June 2006

------------------------------ Summary ------------------------------
PHP Binaries Dir : C:\Program Files\P H P
PHP Binary Type  : cgi
PHP Binary Name  : php-cgi.exe
---------------------------------------------------------------------

Validating inputs...

ERROR: PHP CGI requires modifying cgi.force_redirect to 0 in "C:\Program Files\P H P\PHP.INI"

Remember to tweak PHP.INI for security and functionality per php.net
Finished input validation.

Press 1 to EDIT choices, or ENTER to start IIS modifications:

Starting IIS7 Configuration...

Copying "C:\Program Files\P H P\PHP.INI-Recommended" to PHP.INI...
Setting PHP Handler...
CONFIG object "system.webServer/handlers" changed
Adding and Enabling PHP in ISAPI/CGI Restriction List...
CONFIG object "system.webServer/security/isapiCgiRestriction" changed

Finished IIS7 Configuration.

Test installation using PHP file content of:  <?php phpinfo();?>

Enjoy.

//David

 @IF ?%_ECHO%?==?? ECHO OFF

SETLOCAL
SET DIR_PHP_FROM=%SYSTEMDRIVE%\Inetpub\PHP
SET PHP_TYPE=ISAPI
SET PHP_MODULE=IsapiModule
SET PHP_BINARY=php5isapi.dll

:Menu
ECHO.
ECHO David.Wang's Sample PHP/IIS7 Configurator
ECHO Version: June 2006
ECHO.
ECHO ------------------------------ Summary ------------------------------
ECHO PHP Binaries Dir : %DIR_PHP_FROM%
ECHO PHP Binary Type  : %PHP_TYPE%
ECHO PHP Binary Name  : %PHP_BINARY%
ECHO ---------------------------------------------------------------------

REM
REM Do some basic validations
REM
ECHO.
ECHO Validating inputs...
IF /I ?%PHP_TYPE%? NEQ ?CGI? IF /I ?%PHP_TYPE%? NEQ ?ISAPI? ECHO.&ECHO ERROR: Binary Type MUST be either CGI or ISAPI
FOR %%I IN ( %PHP_BINARY% ) DO (
    IF /I ?%PHP_TYPE%? EQU ?CGI? IF /I ?%%~xI? NEQ ?.exe? ECHO.&ECHO WARNING: Binary Type %PHP_TYPE% requires a CGI EXE binary
    IF /I ?%PHP_TYPE%? EQU ?ISAPI? IF /I ?%%~xI? NEQ ?.dll? ECHO.&ECHO WARNING: Binary Type %PHP_TYPE% requires an ISAPI DLL binary
)
IF NOT EXIST "%DIR_PHP_FROM%\%PHP_BINARY%" (
    ECHO.
    ECHO ERROR: PHP Binary "%DIR_PHP_FROM%\%PHP_BINARY%" does not exist!
    ECHO Please first completely extract PHP to "%DIR_PHP_FROM%"
)
IF /I ?%PHP_TYPE%? EQU ?CGI? SET PHP_MODULE=CgiModule
IF /I ?%PHP_TYPE%? EQU ?CGI? ECHO.&ECHO ERROR: PHP CGI requires modifying cgi.force_redirect to 0 in "%DIR_PHP_FROM%\PHP.INI"
IF /I ?%PHP_BINARY%? NEQ ?php5isapi.dll? IF /I ?%PHP_BINARY%? NEQ ?php-cgi.exe? ECHO.&ECHO WARNING: Unrecognized PHP binary %PHP_BINARY%
ECHO.
ECHO Remember to tweak PHP.INI for security and functionality per php.net
ECHO Finished input validation.
ECHO.

SET GO=
SET /P GO=Press 1 to EDIT choices, or ENTER to start IIS modifications:
IF ?%GO%? EQU ?? GOTO :Start

ECHO.
ECHO Press ENTER to accept [%DIR_PHP_FROM%], or provide new value (folder path)
SET /P DIR_PHP_FROM=PHP Binaries Dir:
ECHO Press ENTER to accept [%PHP_TYPE%], or provide new value (CGI or ISAPI)
SET /P PHP_TYPE=PHP Binary Type:
ECHO Press ENTER to accept [%PHP_BINARY%], or provide new value (filename)
SET /P PHP_BINARY=PHP Binary Name:

GOTO :Menu

:Start
REM
REM Start Configuration
REM
IF NOT EXIST "%DIR_PHP_FROM%\%PHP_BINARY%" (
    ECHO.
    ECHO ERROR: PHP Binary "%DIR_PHP_FROM%\%PHP_BINARY%" does not exist!
    ECHO Please first completely extract PHP to "%DIR_PHP_FROM%"
    GOTO :EOF
)

ECHO.
ECHO Starting IIS7 Configuration...
ECHO.
ECHO Copying "%DIR_PHP_FROM%\PHP.INI-Recommended" to PHP.INI...
COPY /Y "%DIR_PHP_FROM%\PHP.INI-Recommended" "%DIR_PHP_FROM%\PHP.INI" >NUL

PUSHD %SYSTEMROOT%\System32\inetsrv

ECHO Setting PHP Handler...
APPCMD SET CONFIG -section:handlers "-+[name='PHP-%PHP_TYPE%',path='*.php',verb='GET,HEAD,POST',modules='%PHP_MODULE%',scriptProcessor='%DIR_PHP_FROM%\%PHP_BINARY%',resourceType='File']"

ECHO Adding and Enabling PHP in ISAPI/CGI Restriction List...
APPCMD SET CONFIG -section:isapiCgiRestriction "-+[path='%DIR_PHP_FROM%\%PHP_BINARY%',allowed='true',groupId='PHP',description='PHP']"

POPD

ECHO.
ECHO Finished IIS7 Configuration.
ECHO.
ECHO Test installation using PHP file content of:  ^<?php phpinfo();?^>

ENDLOCAL

Comments

  • Anonymous
    June 21, 2006
    thanks for posting! be patient with us :)

  • Anonymous
    June 22, 2006
    David,

    This is excellent, thank you!  Sadly, though, I must be the pest to ask if it is possible to come up with something similar for IIS6 since I do not have access to Vista/IIS7 and occassionally have to support PHP on W2k3.

    Any help or insight is greatly appreciated.

    Thanks,

    Harris

  • Anonymous
    June 22, 2006
    The comment has been removed

  • Anonymous
    June 23, 2006
    A short while ago, I came out with a small script to properly configure PHP for IIS7. Sure enough, I...

  • Anonymous
    August 02, 2006
    Este script es fenomenal, te agradezco mucho por esto, ahora me puedo mover a Vista en mi ambiente de desarrollo con toda confianza!

  • Anonymous
    August 02, 2006
    Daniel - de nada. :-)

    //David

  • Anonymous
    September 16, 2006
    Hi David,

    could you write something about how to set up mysql with php and IIS7? Because i already installed them succesfully but can't make them work on IIS7. Don't you know any solution for this, too?

    Thanks a lot;
    Attila

  • Anonymous
    December 07, 2006
    As far as I'm aware, the PHP 5.2.0 Windows installer doesn't work with Vista [I'm assuming; haven't actually...

  • Anonymous
    April 07, 2007
    Thanks a million on this.  I couldn't get the PHP installer to work so I just d/l the binaries and did what you instructed and works like magic!

  • Anonymous
    April 10, 2007
    Hi from Russian. i need help. IIS7 + PHP ERROR 503 in russian Не удается найти точку входа RegisterModule в модуле библиотеки DLL C:PHPphp5isapi.dll.  Данные представляют собой код ошибки. in english I can't find a'point of entrance RegisterModule in library module DLL C:PHPphp5isapi.dll Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">

  • <System>  <Provider Name="Microsoft-Windows-IIS-W3SVC-WP" Guid="{670080D9-742A-4187-8D16-41143D1290BD}" EventSourceName="W3SVC-WP" />  <EventID Qualifiers="49152">2295</EventID>  <Version>0</Version>  <Level>2</Level>  <Task>0</Task>  <Opcode>0</Opcode>  <Keywords>0x80000000000000</Keywords>  <TimeCreated SystemTime="2007-04-11T04:51:06.000Z" />  <EventRecordID>2967</EventRecordID>  <Correlation />  <Execution ProcessID="0" ThreadID="0" />  <Channel>Application</Channel>  <Computer>stepanet-PC</Computer>  <Security />  </System>
  • <EventData>  <Data Name="ModuleDll">C:PHPphp5isapi.dll</Data>  <Binary>7F000000</Binary>  </EventData>  </Event>
  • Anonymous
    April 11, 2007
    Ummm, when I run the .bat file, it closes itself after I get to the "IIS Modifications" part??? Help!

  • Anonymous
    April 16, 2007
    The comment has been removed

  • Anonymous
    May 30, 2007
    David, I had been struggling to get MySQL, PHP and IIS 7 working all day. However, you script has managed to resolve all my issues within minutes. Thanks for all your help.

  • Anonymous
    June 17, 2007
    Good day, I have Vista Ultimate with IIS7 I downloaded the latest PHP package off their website and ran your script above. But I keep getting: Starting IIS7 Configuration... Copying "C:inetpubwwwrootphpPHP.INI-Recommended" to PHP.INI... Setting PHP Handler... ERROR ( message:New add object missing required attributes. Cannot add duplicate collection entry . ) Adding and Enabling PHP in ISAPI/CGI Restriction List... ERROR ( message:New add object missing required attributes. Cannot add duplicate collection entry . ) Finished IIS7 Configuration. The ISAPI filter can be seen in the handlers section of the IIS server and when I try to access a php site i get Service Unavailable error. I would appreciate the help Thanks Carlos

  • Anonymous
    June 23, 2007
    Carlos - it appears that you already ran some prior installation routine which added the handler for PHP and its Web Service Extension. You also say you have the ISAPI filter placed as a handler, which is improper configuration and will result in problems. I suggest that you clean install IIS7 and make sure ISAPI Extension, unzip PHP, and run my batch file again to install the PHP ISAPI Extension DLL as the handler. My batch file does not try to fix any IIS7 configuration problems to make PHP work, so it will only work if you clean install IIS7, unzip PHP, and run it once. //David

  • Anonymous
    June 24, 2007
    Hi, I have encouter the following error after setting teh handler and php.


HTTP Error 404.0 - Not Found Description: The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. Error Code: 0x80070002 Notification: MapRequestHandler Module: IIS Web Core Requested URL: http://localhost:80/testing.php Physical Path: C:inetpubwwwroottesting.php Logon User: Anonymous Logon Method: Anonymous Handler: PHP

During my installation of IIS under Application Development Features, I have checked all the extensible options (.Net Extensibility, ASP, ASP.NET etc.) instead of just ISAPI Extensions. Any one can hep me?

  • Anonymous
    July 01, 2007
    yZ - this is a real 404 "file not found". Web server says that C:inetpubwwwroottesting.php does not exist. //David

  • Anonymous
    July 21, 2007
    I tried your procedure, (additionaly: newly installed Vista Business, turn the windows feature, IIS7, on, with CGI module activated), extracted PHP 5.2.2 in C:PHP and ran your BAT file. Now it said everything was successful. No errors at all... But still my php server doesn't works. Detailed error when the script is called is as below: Server Error in Application "Default Web Site"


HTTP Error 500.0 - Internal Server Error Description: The page cannot be displayed because an internal server error has occurred. Error Code: 0x800736b1 Notification: ExecuteRequestHandler Module: CgiModule Requested URL: http://localhost:80/info.php Physical Path: C:inetpubwwwrootinfo.php Logon User: Anonymous Logon Method: Anonymous Handler: PHP-CGI

Previously, I did the same thing in Vista Ultimate with IIS7, but I got the same error... Can you please suggest me a solution to this problem.

  • Anonymous
    July 24, 2007
    Ali - the error code suggests that something is wrong with either the PHP script or PHP configuration itself. IIS has launched PHP to process C:inetpubwwwrootinfo.php, and PHP failed. You will have to look at the installation correctness of PHP. If I remember correctly, there are several additional steps that one needs to do to get the CGI version of PHP to work, outside of just extracting PHP files, and my batch file suggests them to you. If they are insufficient, I suggest you consult PHP-related support forums to determine how to get PHP functional because I cannot support PHP's internal configuration, only its configuration related to IIS7. //David

  • Anonymous
    August 02, 2007
    The comment has been removed

  • Anonymous
    August 03, 2007
    The comment has been removed

  • Anonymous
    August 24, 2007
    After running the script, I get the HTTP Error 500.0 error - Handler "PHP" has a bad module "IsapiModule" in its module list.  Could someone please give me detailed instructions for fixing this?

  • Anonymous
    August 24, 2007
    I figured out my problem with the 500.0 error.  Apparently IIS7 defaults to none of the ISAPI or CGI scripting features.  You have to turn on these features.  Go to Control Panel->Programs and Features.  Click on "Turn Windows features on or off".  Under Internet Information Services->World Wide Web Services->Application Development Features, check the needed boxes.  I just checked them all.  This fixed my problem.

  • Anonymous
    November 16, 2007
    I have installed php as isapy modult. All feautures all wokr,but i got problem with fopen function. How make access write for site folders in IIS7(Vista). Thanks.

  • Anonymous
    November 16, 2007
    It too simply.I'm sorry. Just make access for IIS_IUSRS for folder in inetpub.

  • Anonymous
    December 03, 2007
    The comment has been removed

  • Anonymous
    December 19, 2007
    I have used the BAT file to configure IIS7 with PHP 5 as CGI. I am getting following error: HTTP Error 502.2 - Bad Gateway

  • Anonymous
    December 27, 2007
    I am on Vista, has anyone figured out "HTTP Error 502.2 - Bad Gateway" error?

  • Anonymous
    December 27, 2007
    answer to: "bad module 'IsapiModule' in its module list" Read the anwer already stated above:

re: HOWTO: Install and Run PHP on IIS7, Part 2

Friday, August 24, 2007 4:58 PM by Rick I figured out my problem with the 500.0 error.  Apparently IIS7 defaults to none of the ISAPI or CGI scripting features.  You have to turn on these features.  Go to Control Panel->Programs and Features.  Click on "Turn Windows features on or off".  Under Internet Information Services->World Wide Web Services->Application Development Features, check the needed boxes.  I just checked them all.  This fixed my problem

  • Anonymous
    January 10, 2008
    The comment has been removed
  • Anonymous
    January 20, 2008
    The comment has been removed
  • Anonymous
    January 20, 2008
    David: I ran your routine in my Vista Ultimate after a clean (re)install of IIS7 and the BAT file ends with no feedback. When I try to run a PHP file in a browser it opens up my Dreamweaver, since that was the default app for PHP before. This leads me to think that that IIS was not fully configured. What can I send you to help you diagnose? Thanks.
  • Nathan
  • Anonymous
    January 26, 2008
    Martin - you did not do what I have said. You configured PHP5ISAPI.DLL as a <globalModule> so the 503 is by-design due to misconfiguration. You cannot just configure an ISAPI DLL to be an IIS Module DLL - it does not work that way. Please follow the blog entry exactly. //David.

  • Anonymous
    January 26, 2008
    Nantech1 - The batch file is interactive and requires user input, so I am pretty certain you did not run configuration correctly. Since it needs to edit applicationHost.config, the batch file must run with elevated administrator privileges. If you try to run a PHP file in the browser and it opens up Dreamweaver, it means that you have downloaded PHP file from IIS, which is another misconfiguration. PHP files are not downloadable by default As I stated in the blog entry - my directions start from a clean installation of IIS7 with no special configurations. It will not correct any misconfigurations you may have already made. I am sorry, but I am only one person and simply do not have the capacity to support diagnosing anyone's configuration, even if you can send it to me. You really need to find support from Open Source groups for PHP since this is their "responsibility." Remember, Open Source really means you take responsibility for your own support. //David

  • Anonymous
    February 01, 2008
    Well all I can say is THANK YOU..... great bat file..... followed your steps and hey presto... now for mysql

  • Anonymous
    February 01, 2008
    Hi again David! Please come down to my level. I didn't do any of that, I just copied files and ran a script. Tell me what to type. Martin

  • Anonymous
    February 02, 2008
    Martin - I'm sorry, but I already told you exactly what to type, and you could not follow those instructions. Based on the error you disclosed earlier, I know you did not follow my instructions and "just copied files and ran a script", so how can I believe this time will be any different? Please show me you can follow instructions by doing what Rob did right before you and get PHP working. //David

  • Anonymous
    February 03, 2008
    THANK YOU... great script... presto... So much for, Rob. Tell me what I should do. You're condescending me because your instructions are so easy. For the same reason I'm sure I've followed them.

  • Anonymous
    February 28, 2008
    Thanks for the script , however, I keep getting this error when I run the file : Setting PHP Handler... ERROR message: New add object missing required attributes. Cannot add duplicate collection entry Adding and enabling php in iSAPI CGI restriction list... ERROR message: new add object missing required attributes. Cannot add duplicate collection entry


what does that mean?

  • Anonymous
    March 07, 2008
    The comment has been removed

  • Anonymous
    April 03, 2008
    The comment has been removed

  • Anonymous
    April 18, 2008
    dev loy - Unfortunately, the script only works if you start from a clean install of IIS7 with all necessary features, and known location of PHP files. The script only does what is correct to setup PHP. It doesn't fix what is incorrect. Thus, using it after you run some other setup process will not be helpful. The script isn't going to fix bad configuration and make everything magically work. Without giving me your applicationHost.config file or a FREB trace of the request with an error, there is no way to tell what is missing or misconfigured. //David

  • Anonymous
    May 10, 2008
    Thank you for this. I struggled with this for three days, following a lot of wrong advice and wasting a lot of time.

  • Anonymous
    May 12, 2008
    The comment has been removed

  • Anonymous
    May 12, 2008
    In order to get PHP running under IIS on Vista Home Premium, it was necessary to uncheck the ISAPI filters AND the ASP.NET boxes.  Whether it is ASP.NET or filters that were causing the problem, I haven't a clue but you can't uncheck one without unchecking the other so it is a moot point.

  • Anonymous
    May 12, 2008
    Fred - That sounds like a problem specific to your installation. The installation state of ISAPI Filter and ASP.Net are not related to PHP because it does not use any of those modules. My suspicion is that you (or some external program) mis-installed some ISAPI Filter on IIS7. That mis-instnallation will prevent anything else from working, not just PHP, so disabling ISAPI Filter turns off that misconfiguration and thus allows whath the batch file did to install IIS7 function. This batch file only works the first time it runs. As stated at the beginning, I am providing illustration of how to correctly configure PHP to run -- NOT fix any setup and make PHP run successfully. The latter is beyond the scope of this blog. //David

  • Anonymous
    May 13, 2008
    Wonderful to find the help you need. Thank you. One issue: the PHP script was not being interpreted it was just appearing in the output. I went back to the previous blog and found the the user permissions to the PHP location were not set for IIS_IUSRS - setting this fixed it. Thank you again.

  • Anonymous
    July 17, 2008
    Hi David, I have configured the IIS 7.0 with the .bat script provided by you. My PHP and MySQL server are already installed. When I try to open the test.php file containing the phpinfo() function, the cursor keeps rotating and no error or page is displayed. Yesterday, I was able to open my URL and work on my URL without any issues on the same setup. But today it is not working again. Please guide me in resolving this issue. Regards, Guncha

  • Anonymous
    August 08, 2008
    Hi David ,, IIS is working great with php5isapi  but when using zendcfg  every thing stop working >>   How can we resolve it Best Regards

  • Anonymous
    August 09, 2008
    sinbadkan - please contact the support personel for zendcfg regarding your question. //David

  • Anonymous
    November 28, 2013
    where is the .bat file content ? i can't find that please help