I am working on this web application which is mostly .NET, but a part is written in PHP. In my development environment, IIS Express is my web server of choice, so I would prefer to get PHP and .NET work together in this environment.
First naive attempt: Just throw the PHP scripts into the web site, and load them in a browser. The result is an HTTP Error 404.21 - Not Found
with the following suggestions:
- Install PHP and configure handlers correctly.
- Install and enable PHP for this web site.
- If you are using WebMatrix, follow these steps to enable PHP:
- Open the web site in WebMatrix.
- Select the Site workspace and click on Settings.
- Check the "Enable PHP" checkbox to install and enable PHP.
Not using WebMatrix, this does nothing to help me. I then proceed to do a deep trawl of anything related to IIS Express and PHP on Google. From what I read, it should be doable, but everything I find about the subject seems to assume that my IDE is WebMatrix. It isn't.
Does anyone know if it is possible to install on IIS Express and configure it to work with a Visual Studio based web site, without having to install WebMatrix? If yes, how?
IIS Express supports PHP without Web matrix. You can install just IIS Express alone and make it work with Visual Studio. For this you need Visual Studio 2010 SP1.
Download IIS Express from this link: Internet Information Services (IIS) 7.5 Express
In Visual Studio configure your WebSite/WebApplication project to use IIS Express. Take a look at this thread How do I configure a website project to use IIS Express?
To enable PHP on IIS Express, install PHP and update applicationhost.config (%userprofile%\documents\iisexpress\config\applicationhost.config). You can use the version of appcmd.exe located in IIS Express installation folder for doing this. See this link for details.
If everything is installed in the default places for US English versions of Windows the commands will be:
"C:\Program Files (x86)\IIS Express\appcmd.exe" set config /section:system.webServer/fastCGI /+[fullPath='"C:\Program Files (x86)\PHP\php-cgi.exe"']
"C:\Program Files (x86)\IIS Express\appcmd.exe" set config /section:system.webServer/handlers /+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='"C:\Program Files (x86)\PHP\php-cgi.exe"',resourceType='Unspecified']
Note that if there are spaces in the full path of php-cgi.exe, you MUST enclose the path in single and double quotes as in the example above.
Finally, if you don't have VS 2010 SP1, probably you should take a look at this post: Debug Your .NET Web Project With IIS Express
Another option is to use Web Platform Installer.
Web Platform Installer will install PHP and configure IIS Express for you.
Newer versions of Visual Studio maintain a separate copy of IIS Express's configuration file "applicationhost.config". Most of the instructions online show you how to target the master applicationhost.config file which has no effect because VS2017 maintains a separate copy and uses this copy when starting IIS express. To enable PHP in this scenario, I had to edit VS2017's private copy.
Prerequisite - Use the web platform installer to install PHP. (You don't need the one for IIS Express, just the regular PHP download.) You can use x86 or x64. This will install PHP into either C:\Program Files\ or C:\Program Files (x86)\ depending on which one you choose. You could also simply grab PHP from their website.
Locate your project's applicationhost.config. It is located in a hidden .vs subfolder. For example C:\MyProject\.vs\applicationhost.config
Run the following commands from a command window, substituting your paths for the project folder and the PHP binaries you installed.
"C:\Program Files (x86)\IIS Express\appcmd.exe" set config /section:system.webServer/fastCGI /+[fullPath='"C:\Program Files (x86)\PHP\{{YOUR PHP VERSION}}\php-cgi.exe"'] /apphostconfig:"C:\{{YOUR PROJECT}}\.vs\config\applicationhost.config"
"C:\Program Files (x86)\IIS Express\appcmd.exe" set config /section:system.webServer/handlers /+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='"C:\Program Files (x86)\PHP\{{YOUR PHP VERSION}}\php-cgi.exe"',resourceType='Unspecified'] /apphostconfig:"C:\{{YOUR PROJECT}}\.vs\config\applicationhost.config"
Alternatively, you can simply edit the project's applicationhost.config in a text editor and add the following sections.
system.webServer/fastCGI
<fastCgi>
<application fullPath="C:\Program Files (x86)\PHP\v7.1\php-cgi.exe" />
</fastCgi>
system.webServer/handlers
<add name="PHP_via_FastCGI" path="*.php" verb="*" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\PHP\v7.1\php-cgi.exe" resourceType="Unspecified" />
To be clear, editing the IIS Express\AppServer\applicationhost.config or the one in your %appdata% folder will not help unless you're doing something advanced like starting IIS Express outside Visual Studio.
If you are running IIS through Visual Studio, you need to update the .vs\config\applicationhost.config in your solution directory with the php handler