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

1

I try to get a Flask application running on an IIS on windows.

The reason is to get single-sign-on with windows authentication done using the IIS mechanisms.

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.

I saw the python error message ones during the setup, before I set the rights to write to the log file. It told me about the missing rights. That should mean FastCGI is configured the right way, I guess.

Now after I set the write access, I get a IIS error 500 page that tells me that something went wrong with the 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. 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:

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

python
iis
flask
wfastcgi
asked on Stack Overflow Dec 3, 2019 by Juergen Gutsch • edited Dec 4, 2019 by Juergen Gutsch

3 Answers

2

To configure python flask app in iis you could follow the below steps:

  • First, you need to install the python,wfastcgi, and flask at your server.
  • You can download the python from below link:

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

note: if possible please use python version above 3.6.

after installing python install the wfastcgi.run the command prompt as administrator and run below command:

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:

enter image description here

  • now open iis.
  • right-click on the server name and select add site.

enter image description here

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

enter image description here

  • Click “Add Module Mapping”

enter image description here

executable path value:

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

enter image description here

  • Click “Request Restrictions”. Make sure “Invoke handler only if the request is mapped to:” checkbox is unchecked:

enter image description here

  • Click “Yes” here:

enter image description here

  • now go back and select the application setting feature.

enter image description here

  • click add from the action pane.

enter image description here

  • Set the PYTHONPATH variable(which is your site folder path):

enter image description here

  • 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):

enter image description here

  • Click OK and browse to your site.

enter image description here

answered on Stack Overflow Dec 4, 2019 by Jalpa Panchal • edited Dec 5, 2019 by Jalpa Panchal
1

All IIS sites have a web.config file in the root which holds all the settings. You can edit this manually with a text editor (it's just XML) or you can use the 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.

Look in your web.config for <customErrors> inside the system.web element (see below). Set it to mode="Off", this should hopefully mean you can see the actual error message.

<configuration>
    <system.web>
        <customErrors mode="Off">
        </customErrors>
    </system.web>
</configuration>
answered on Stack Overflow Dec 3, 2019 by Matt Kemp
0

I got the same error and have solved it by add access rights to the folder of 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.

enter image description here

The other option is that changing AppPool identity of the web application to LocalSystem as IIS manager.

enter image description here

answered on Stack Overflow Apr 14, 2021 by Epic Chen • edited Apr 14, 2021 by Epic Chen

User contributions licensed under CC BY-SA 3.0