使用 Visual Studio (sans WebMatrix) 在 IIS Express 上启用 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6099143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:21:11  来源:igfitidea点击:

Enabling PHP on IIS Express with Visual Studio (sans WebMatrix)

phpvisual-studioiis-express

提问by J?rn Schou-Rode

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.

我正在开发这个 Web 应用程序,它主要是 .NET,但有一部分是用 PHP 编写的。在我的开发环境中,IIS Express 是我选择的 Web 服务器,因此我更愿意在这种环境中让 PHP 和 .NET 一起工作。

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 Foundwith the following suggestions:

第一次天真的尝试:只需将 PHP 脚本放入网站,然后在浏览器中加载它们。结果是HTTP Error 404.21 - Not Found带有以下建议:

  • 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.
  • 安装 PHP 并正确配置处理程序。
  • 为该网站安装并启用 PHP。
  • 如果您使用的是 WebMatrix,请按照以下步骤启用 PHP:
    • 在 WebMatrix 中打开网站。
    • 选择站点工作区并单击设置。
    • 选中“启用 PHP”复选框以安装和启用 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.

不使用 WebMatrix,这对我没有帮助。然后,我继续在 Google 上深入研究与 IIS Express 和 PHP 相关的任何内容。从我读到的内容来看,它应该是可行的,但是我发现的关于该主题的所有内容似乎都假设我的 IDE 是 WebMatrix。不是。

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 上安装并将其配置为与基于 Visual Studio 的网站一起使用,而无需安装 WebMatrix?如果是,如何?

回答by vikomall

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.

IIS Express 支持没有 Web 矩阵的 PHP。您可以单独安装 IIS Express 并使其与 Visual Studio 一起使用。为此,您需要 Visual Studio 2010 SP1。

  1. Download IIS Express from this link: Internet Information Services (IIS) 7.5 Express

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

  3. 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 linkfor details.

  1. 从此链接下载 IIS Express:Internet Information Services (IIS) 7.5 Express

  2. 在 Visual Studio 中配置您的 WebSite/WebApplication 项目以使用 IIS Express。看看这个线程如何配置网站项目以使用 IIS Express?

  3. 要在 IIS Express 上启用 PHP,请安装 PHP 并更新 applicationhost.config (%userprofile%\documents\iisexpress\config\applicationhost.config)。您可以使用位于 IIS Express 安装文件夹中的 appcmd.exe 版本来执行此操作。有关详细信息,请参阅此链接

If everything is installed in the default places for US English versions of Windows the commands will be:

如果所有内容都安装在美国英语版 Windows 的默认位置,则命令将是:

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

请注意,如果 php-cgi.exe 的完整路径中有空格,则您必须将路径括在单引号和双引号中,如上例所示。

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

最后,如果您没有 VS 2010 SP1,可能您应该看看这篇文章:使用 IIS Express 调试您的 .NET Web 项目

回答by Lukas Kabrt

Another option is to use Web Platform Installer.

另一种选择是使用 Web 平台安装程序。

Web Platform Installer will install PHP and configure IIS Express for you.

Web 平台安装程序将为您安装 PHP 并配置 IIS Express。

回答by Matt

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.

较新版本的 Visual Studio 维护 IIS Express 配置文件“applicationhost.config”的单独副本。大多数在线说明都向您展示了如何定位 master applicationhost.config 文件,该文件无效,因为 VS2017 维护一个单独的副本并在启动 IIS express 时使用此副本。为了在这种情况下启用 PHP,我必须编辑 VS2017 的私有副本。

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

  • 先决条件 - 使用 Web 平台安装程序安装 PHP。(您不需要 IIS Express 的那个,只需要常规的 PHP 下载。)您可以使用 x86 或 x64。这会将 PHP 安装到 C:\Program Files\ 或 C:\Program Files (x86)\ 中,具体取决于您选择的那个。您也可以简单地从他们的网站上获取 PHP。

  • 找到您项目的 applicationhost.config。它位于隐藏的 .vs 子文件夹中。例如 C:\MyProject\.vs\applicationhost.config

  • 从命令窗口运行以下命令,用您的路径替换您安装的项目文件夹和 PHP 二进制文件。

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

或者,您可以简单地在文本编辑器中编辑项目的 applicationhost.config 并添加以下部分。

system.webServer/fastCGI

system.webServer/fastCGI

<fastCgi>
    <application fullPath="C:\Program Files (x86)\PHP\v7.1\php-cgi.exe" />
</fastCgi>

system.webServer/handlers

system.webServer/处理程序

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

需要明确的是,编辑 IIS Express\AppServer\applicationhost.config 或 %appdata% 文件夹中的配置将无济于事,除非您正在执行一些高级操作,例如在 Visual Studio 之外启动 IIS Express。

回答by JohnnyBT

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

如果您通过 Visual Studio 运行 IIS,则需要使用 php 处理程序更新解决方案目录中的 .vs\config\applicationhost.config