简体   繁体   English

在 IIS 上运行 Python Flask 应用程序时出现错误 500。 如何获得实际错误

[英]Error 500 while running a Python Flask application on IIS. How to get the actual error

I try to get a Flask application running on an IIS on windows.我尝试在 Windows 上的 IIS 上运行 Flask 应用程序。

The reason is to get single-sign-on with windows authentication done using the IIS mechanisms.原因是使用 IIS 机制完成 Windows 身份验证的单点登录。

I got it running on my development machine.我让它在我的开发机器上运行。 But it doesn't work in the actual production environment.但是在实际生产环境中是行不通的。 I get a error 500 for some reasons, but I don't see the actual Flask error message.由于某些原因,我收到错误 500,但我没有看到实际的 Flask 错误消息。

I saw the python error message ones during the setup, before I set the rights to write to the log file.在设置写入日志文件的权限之前,我在设置过程中看到了 python 错误消息。 It told me about the missing rights.它告诉我缺少的权利。 That should mean FastCGI is configured the right way, I guess.我猜这应该意味着 FastCGI 以正确的方式配置。

Now after I set the write access, I get a IIS error 500 page that tells me that something went wrong with the FastCGI.现在,在我设置写访问权限后,我收到一个 IIS 错误 500 页面,告诉我 FastCGI 出现问题。 But I don't get any log entry, even if I set the rights to write them.但是我没有得到任何日志条目,即使我设置了写入它们的权限。 No log files and no entries in the windows event logs. Windows 事件日志中没有日志文件和条目。 Nothing.没有。

Do you know a way to get the actual error message?您知道获取实际错误消息的方法吗?

[Update] After enabling the failed request trace, I get the following error: [更新] 启用失败的请求跟踪后,出现以下错误:

<RenderingInfo Culture="en-US">
  <Opcode>FASTCGI_UNKNOWN_ERROR</Opcode>
  <Keywords>
   <Keyword>FastCGI</Keyword>
  </Keywords>
  <freb:Description Data="ErrorCode">The directory name is invalid.
(0x8007010b)</freb:Description>
</RenderingInfo>

The web.config looks like this: web.config 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="PYTHONPATH" value="C:\inetpub\wwwroot\app_name" />
    <!-- The handler here is specific to Bottle; see the next section. -->
    <add key="WSGI_HANDLER" value="main.app" />
    <add key="WSGI_LOG" value="C:\inetpub\wwwroot\app_name\LogFiles\wfastcgi.log" />
  </appSettings>
  <system.web>
            <customErrors mode="Off" />
  </system.web>
  <system.webServer>
    <handlers>
      <clear />
      <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" 
                          scriptProcessor="&quot;c:\program files\python37\python.exe&quot;|&quot;c:\program files\python37\lib\site-packages\wfastcgi.py&quot;" 
                          resourceType="Unspecified" requireAccess="Script" />
    </handlers>
    <security>
      <authentication>
        <anonymousAuthentication enabled="false" />                       
                        <windowsAuthentication enabled="true" />
      </authentication>
    </security>
  </system.webServer>
</configuration>

If I would guess, the path to the "program files" folder with the blank inside would cause the error.如果我猜想,内部空白的“程序文件”文件夹的路径会导致错误。

To configure python flask app in iis you could follow the below steps:要在 iis 中配置 python flask 应用程序,您可以按照以下步骤操作:

  • First, you need to install the python,wfastcgi, and flask at your server.首先,您需要在您的服务器上安装 python、wfastcgi 和 Flask。
  • You can download the python from below link:您可以从以下链接下载python:

https://www.python.org/downloads/ https://www.python.org/downloads/

note: if possible please use python version above 3.6.注意:如果可能,请使用 3.6 以上的 python 版本。

after installing python install the wfastcgi.run the command prompt as administrator and run below command:安装 python 后安装 wfastcgi.run 以管理员身份运行命令提示符并运行以下命令:

pip install wfastcgi

wfastcgi-enable

below is my flask example:下面是我的烧瓶示例:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello from FastCGI via IIS!"
if __name__ == "__main__":
    app.run()

after creating an application to run it use below command:创建应用程序以运行它后,请使用以下命令:

python app.py

now enable the cgi feature of iis:现在启用 iis 的 cgi 功能:

在此处输入图片说明

  • now open iis.现在打开iis。
  • right-click on the server name and select add site.右键单击服务器名称并选择添加站点。

在此处输入图片说明

  • enter the site name physical path and the site binding.输入站点名称物理路径和站点绑定。
  • after adding site select the site name and select the handler mapping feature from the middle pane.添加站点后,选择站点名称并从中间窗格中选择处理程序映射功能。

在此处输入图片说明

  • Click “Add Module Mapping”点击“添加模块映射”

在此处输入图片说明

executable path value:可执行路径值:

C:\\Python37-32\\python.exe|C:\\Python37-32\\Lib\\site-packages\\wfastcgi.py C:\\Python37-32\\python.exe|C:\\Python37-32\\Lib\\site-packages\\wfastcgi.py

在此处输入图片说明

  • Click “Request Restrictions”.单击“请求限制”。 Make sure “Invoke handler only if the request is mapped to:” checkbox is unchecked:确保“仅当请求映射到时才调用处理程序:”复选框未选中:

在此处输入图片说明

  • Click “Yes” here:在此处单击“是”:

在此处输入图片说明

  • now go back and select the application setting feature.现在返回并选择应用程序设置功能。

在此处输入图片说明

  • click add from the action pane.单击操作窗格中的添加。

在此处输入图片说明

  • Set the PYTHONPATH variable(which is your site folder path):设置 PYTHONPATH 变量(这是您的站点文件夹路径):

在此处输入图片说明

  • And the WSGI_HANDLER (my Flask app is named app.py so the value is app.app — if yours is named site.py it would be site.app or similar):还有 WSGI_HANDLER(我的 Flask 应用程序名为 app.py,因此值为 app.app — 如果您的名为 site.py,它将是 site.app 或类似的):

在此处输入图片说明

  • Click OK and browse to your site.单击“确定”并浏览到您的站点。

在此处输入图片说明

All IIS sites have a web.config file in the root which holds all the settings.所有 IIS 站点的根目录中都有一个 web.config 文件,其中包含所有设置。 You can edit this manually with a text editor (it's just XML) or you can use the IIS GUI.您可以使用文本编辑器(它只是 XML)手动编辑它,也可以使用 IIS GUI。

It sounds to me like the default <customErrors> setting is On or RemoteOnly which means it shows actual errors on your local dev machine, but hides them with "friendly" (but unhelpful) error pages on production when you view the site remotely.在我看来,默认的<customErrors>设置是OnRemoteOnly ,这意味着它会在您的本地开发机器上显示实际错误,但是当您远程查看站点时,它会在生产环境中使用“友好”(但无用)错误页面隐藏它们。

Look in your web.config for <customErrors> inside the system.web element (see below).在您的 web.config 中查找system.web元素中的<customErrors> (见下文)。 Set it to mode="Off" , this should hopefully mean you can see the actual error message.将其设置为mode="Off" ,这应该意味着您可以看到实际的错误消息。

<configuration>
    <system.web>
        <customErrors mode="Off">
        </customErrors>
    </system.web>
</configuration>

I got the same error and have solved it by add access rights to the folder of python.我遇到了同样的错误,并通过向 python 文件夹添加访问权限来解决它。 In your case, the python folder path is c:\\program files\\python37 , so try to add IIS_IUSRS with read and execution access rights .在您的情况下,python 文件夹路径是c:\\program files\\python37 ,因此请尝试添加IIS_IUSRS具有读取和执行访问权限

在此处输入图片说明

The other option is that changing AppPool identity of the web application to LocalSystem as IIS manager.另一种选择是将 Web 应用程序的AppPool标识更改为LocalSystem作为 IIS 管理器。

在此处输入图片说明

If your file permissions are correct, the problem might lie in your Application Pools settings.如果您的文件权限正确,则问题可能出在您的应用程序池设置中。

  1. Go to IIS manager转到 IIS 管理器
  2. Click on your server's Application Pools tab单击您服务器的Application Pools选项卡
  3. Click Set Application Pool Defaults ...单击Set Application Pool Defaults ...
  4. Set the Identity under Process Model to LocalSystemProcess Model下的Identity设置为LocalSystem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM