How to host Python 3.7 flask application on Windows Server?

Ishan picture Ishan · Oct 6, 2019 · Viewed 12.3k times · Source

As wfastcgi module is not compatible with Python 3.7, What is the best way to host a python flask application on a Windows Server?

Answer

Jalpa Panchal picture Jalpa Panchal · Oct 7, 2019

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/

after installing python download the wfastcgi:

pip install wfastcgi

run the command prompt as administrator and run this command.

wfastcgi-enable

run this command to enable wfastcgi.

below is my flask example:

app.py:

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

enter image description here

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 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” enter image description here
  • add below value:

enter image description here

enter image description here

enter image description here

executable path value:

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

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

enter image description here

  • Click “Yes” here:

enter image description here

  • now go back and again select the server name and select fast CGI setting from the middle pane.

enter image description here

  • Double click it, then click the “…” for the Environment Variables collection to launch the EnvironmentVariables Collection Editor:

enter image description here

  • Set the PYTHONPATH variable:

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

Note: Do not forget to assign the iusr and iis_iusrs user permission to the flask site folder and python folder.