0

Following the steps in https://learn.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2019 to configure flask app to run behind IIS and searching online I couldn't find a solution that fix my problem.

I have my web.config as:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.webServer>
        <handlers>
          <add name="PythonHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
        </handlers>
        <httpPlatform processPath="C:\envs\Scripts\python.exe"
                      arguments="-m flask run --port %HTTP_PLATFORM_PORT%"
                      stdoutLogEnabled="true"
                      stdoutLogFile="C:\logs\python.log"
                      startupTimeLimit="60"
                      processesPerApplication="16">
          <environmentVariables>
            <environmentVariable name="FLASK_APP" value="app.py" />
          </environmentVariables>
        </httpPlatform>
      </system.webServer>
    </configuration>

A visit to the site just spit the following. I tried all possible solutions I came across which include giving IIS_IUSRS access. Running the app on command prompt runs just fine. The IIS error message is not helping.

enter image description here

edit:

after installing the http platformhandler and now on a different dev box, I can see the handler at work but just a different monster:

502.3 Bad Gateway

Detailed Error Information:

**Module**        httpPlatformHandler              **Requested URL**  http://127.0.0.1:5007/about

**Notification**  ExecuteRequestHandler         **Physical Path**  C:\inetpub\wwwroot\app\about

**Handler**       PythonHandler                 **Logon Method**   Anonymous

**Error Code**    0x8007042b                    **Logon User**     Anonymous

Which tells me the handler is just handling the url as just folders in my app root because http://127.0.0.1:5007/about is nothing but a route to:

myapp_about.py:

from flask import Blueprint, jsonify

myapp_about = Blueprint('about', __name__)


@myapp_about.route('/about')
def get_about():
    return jsonify({"wow": "We really are routed to here. maybe not"})
5
  • did you add a handler mapping for python and fast cgi in iis?medium.com/@bilalbayasut/… Commented Apr 12, 2019 at 9:10
  • 1
    @JalpaPanchal I'm using HttpPlatform handler not FastCGI handler. Commented Apr 12, 2019 at 9:16
  • You may need to add requireAccess: <add name="PythonHandler" path="" verb="" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script"/> Commented Apr 12, 2019 at 9:27
  • @JalpaPanchal I'll try that one but I'm now on a different box and made an edit to the question. Commented Apr 12, 2019 at 9:42
  • 1) The initial 500.19 error is well known and too many duplicates on this site. 2) You edited the question but actually was asking another question irrelevant to the first one. Your extra myapp_about.py won't be picked up even if you run python -m flask run without IIS. So I wonder from where you found the code snippet and wanted to give that a try. 3) This question in its current form should be closed as duplicate I think. Commented Apr 9, 2024 at 2:42

3 Answers 3

5

This worked for me on Windows 10 and Windows Server 2019, both using IIS 10.0. You must have the HttpPlatformHandler installed, as mentioned in the other answers. (Download it via Windows Platform Installer.)

The <handlers> section of my web.config is locked, so instead of editing the web.config directly, I used the Add Module Mapping... action on the IIS Handler Mappings GUI to add the Python handler:

Image of Python handler

You can also edit the website configuration directly via the IIS Manager's GUI:

IIS Configuration Editor

I use an absolute path for the FLASK_APP environment variable, but it seems to work without it too. (In fact, you can leave the environment variable out completely if you are using the default Flask app name of app.py.)

web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpPlatform processPath="C:\Python37\python.exe"
                      arguments="-m flask run --port %HTTP_PLATFORM_PORT%"
                      stdoutLogEnabled="true"
                      stdoutLogFile="C:\inetpub\logs\LogFiles\python.log"
                      startupTimeLimit="60"
                      processesPerApplication="16">
          <environmentVariables>
              <environmentVariable name="FLASK_APP" value="C:\inetpub\wwwroot\hello.py" />
          </environmentVariables>
        </httpPlatform>
    </system.webServer>
</configuration>

This works with the minimal Flask app below. (Maybe try this before adding Blueprint and different port numbers.)

hello.py:

from flask import Flask

app = Flask(__name__)

@app.route('/hi')
def hello_world():
    return 'Hello, World!'

Image of browser output

Notes about Windows Security permissions:

  • It is not necessary for the Handler Mapping in IIS Manager to have Execute permissions checked. (Script permission is the default, and works as-is.)
  • It is not necessary for IIS_IUSR to have execute permissions on python.exe or flask.exe.
  • It is not necessary for IIS_IUSR to have execute permissions on app.py or hello.py.
  • If logging is enabled, IIS_IUSR must have write permissions on the target folder (e.g. C:\inetpub\logs\LogFiles) - not just on the python.log file, since it will likely add an additional suffix to the specified log file name.
Sign up to request clarification or add additional context in comments.

4 Comments

@Murugan: I got it working on Windows 10 too. Added some notes at the bottom with permissions to check. (The last point had me frustrated for a while!)
Could you please send any video tutorial?
@Dirco i've just added an entry in hosts file and everything seems to work fine. BUT i'm completely confused what's the difference between edinting bindings and editing hosts file ? even in official MS doc there's no mention for hosts file.
@Dirco tnx. it did work for me and it was so helpful.
0

I didn't see anywhere in those instructions, but I think your problem is the HttpPlatformHandler. Try removing that and see if you get a better/different error. HttpPlatformHandler is not a default configuration element of IIS and need to be added, most likely by installing something like https://www.iis.net/downloads/microsoft/httpplatformhandler.

5 Comments

installing the http platformhndler was one of the solution i came across and it didn't help. and removing the modules entry doesn't basically change the error.
What do you mean by "doesn't basically change the error"? I built a brand new server, installed HttpPlatformHandler and slapped on your Web.config. It fixed the configuration error and then choked on the fact that I don't have Python installed
Then I have no idea where I’m messing things up. I even tried to install the x86 of handler as well but unfortunately my x64 windows 10 rejected it.
Did the error change at all thru any of your attempts?
Did a trial on a different box and made an update. I guess now I've uncovered a totally different problem.
0

Had same issue and worked doing below. "Took a lot of effort but I got it to work a last

Must Install HttpPlatform https://www.iis.net/downloads/microsoft/httpplatformhandler Configuration Editor system.webServer/httpPlatform (Section is not locked) Make sure app has permissions for log path stdoutLogFile="c:\home\LogFiles\python.log" Had to setup up app.run(port=PORT) PORT = int(environ.get('SERVER_PORT', '5555')) Setup firewall to allow http-incoming requests"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.