3

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.

Juergen Gutsch
  • 1,774
  • 2
  • 17
  • 28
  • which permission did you assign to the python and site folder? could you share your sample code? windows authentication only works for the AD domain. which steps did you follow to set the flask app in iis? enable the failed request tracing to capture the detail of the error 500. This link provides the steps to enable failed request tracing, please post the error log here and we may help you resolve the problem: https://www.iis.net/learn/troubleshoot/using-failed-request-tracing/troubleshooting-failed-requests-using-tracing-in-iis – Jalpa Panchal Dec 04 '19 at 07:45
  • Awesome thanks, Jalpa. I set up to modify permissions to the python and site folder for the user specified in the application pool. I followed the steps described here: https://pypi.org/project/wfastcgi/ I will have a look at the failed request tracing. Thanks a lot :-) – Juergen Gutsch Dec 04 '19 at 07:52
  • @JalpaPanchal please see my update – Juergen Gutsch Dec 04 '19 at 09:52
  • please remove extra " from the script path and assign the iusr and iis_iusrs permission to the c:\program files\python37\ and C:\inetpub\wwwroot\app_name folder and refresh site and iis server. – Jalpa Panchal Dec 05 '19 at 01:36
  • The quotes are needed because of the blank in the script path. The permission are already set. – Juergen Gutsch Dec 05 '19 at 03:30
  • did you try to remove the quote? – Jalpa Panchal Dec 05 '19 at 05:22
  • Yes. Also the quotes are added by wfastcgi-enable to the FastCgi settings and the entry there should match the entry in the web.config, otherwise I get a config error – Juergen Gutsch Dec 05 '19 at 05:36
  • is your application is running before hosting in iis. – Jalpa Panchal Dec 05 '19 at 05:38
  • Yes, and it is running on my local development machine using IIS – Juergen Gutsch Dec 05 '19 at 05:41
  • you could try to reinstall python at the c drive as shown in [image](https://i.imgur.com/FmhIzGI.png). – Jalpa Panchal Dec 05 '19 at 06:31
  • I did it yesterday. The invalid folder error is gone, but FastCgi still produces an 500 for an unknown reason. – Juergen Gutsch Dec 05 '19 at 06:34

4 Answers4

3

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

Jalpa Panchal
  • 8,251
  • 1
  • 11
  • 26
  • Hi Jalpa, thank you for the detailed documentation. It's all configured like this :-) And as I mentioned, it is running on the local development machine, but it is not running on the production server. All I need to know is, ho to get the error messages, to see the underlying error and to solve it :-) – Juergen Gutsch Dec 04 '19 at 08:36
  • @JuergenGutsch could you please share your detailed error message snapshot and web.config file? – Jalpa Panchal Dec 04 '19 at 08:54
  • Unfortunately I don't have a detailed error message yet. I'll try to get one, following the instructions on the page you posted above. – Juergen Gutsch Dec 04 '19 at 08:59
  • @JuergenGutsch try to remove the handler mapping and manually try to select the path of the python.exe from the by clicking three dots [img](https://i.imgur.com/JQ5xKdM.png) – Jalpa Panchal Dec 09 '19 at 01:53
  • 1
    Thank you for this. I have been looking at the issue of running a Flask app on IIS for days and this is the ONLY explanation that worked – cormacio100 Oct 21 '22 at 15:52
2

If your file permissions are correct, the problem might lie in your Application Pools settings.

  1. Go to IIS manager
  2. Click on your server's Application Pools tab
  3. Click Set Application Pool Defaults...
  4. Set the Identity under Process Model to LocalSystem
sharkk22
  • 353
  • 3
  • 7
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>
Matt Kemp
  • 2,742
  • 2
  • 28
  • 38
  • Thanks Matt, that might be an option :-) Even if didn't set the CustomErrors on my machine explicitly. I'm going to try it. – Juergen Gutsch Dec 03 '19 at 20:44
  • Yep, the local web.config file actually overrides settings in a higher, server level machine.config file. So even if you didn't set it explicitly, it'll be getting a value from there. – Matt Kemp Dec 03 '19 at 20:46
  • Unfortunately this is not the solution. I still get the default error page. Actually I know the IIS and ASP.NET quite well, but the Python and Flask stuff is pretty new for me. I guess the problem is more related to the FastCGI configuration. – Juergen Gutsch Dec 03 '19 at 21:06
  • Makes sense. Another quick idea, have you tried Remote Desktop to the server and run it from a browser there? If FastCGI has a similar configuration it might show you the real error when local like it did on your dev machine – Matt Kemp Dec 03 '19 at 21:10
  • Yes, I'm currently working remotely on that machine. Mainly to play around with the FastCGI settings, Python, Flask and access rights. – Juergen Gutsch Dec 03 '19 at 21:32
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

Epic Chen
  • 1,282
  • 12
  • 17