I am trying to serve Flask app from IIS Web Application under Default Web Site, but am not able to get it working. Here are the details:
OS: Windows Server 2016 DataCenter Edition
IIS: Installed with all options including CGI
IIS Rewrite Module: Version 2 installed
Steps I've taken:
- Install IIS with all components (with CGI)
- Install Python 2.7.14
- Add Python in the
PATH
variable pip install wfastcgi
wfastcgi-enable
pip install flask
iisreset
- Create Directory:
C:\inetpub\wwwroot\flask-demo
- Click
Convert to Application
in IIS for above directory. - Create
C:\inetpub\wwwroot\flask-demo\myapp.py
with following content:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == "__main__": app.run()
- Create
C:\inetpub\wwwroot\flask-demo\web.config
with following content:
<configuration> <system.webServer> <handlers> <remove name="Python FastCGI" /> <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Python27\python.exe|C:\Python27\Lib\site-packages\wfastcgi.pyc" resourceType="Unspecified" requireAccess="Script" /> </handlers> <rewrite> <rules> <rule name="asset-url-rewrite" stopProcessing="true"> <match url="static" /> <conditions> </conditions> <action type="Rewrite" url="flask-demo/{R:0}" /> </rule> <rule name="app-url-rewrite" stopProcessing="true"> <match url="[a-zA-Z]+" /> <conditions> </conditions> <action type="Rewrite" url="flask-demo/" /> </rule> </rules> </rewrite> </system.webServer> <appSettings> <add key="WSGI_HANDLER" value="myapp.app" /> <add key="PYTHONPATH" value="C:\inetpub\wwwroot\flask-demo" /> </appSettings> </configuration>
Problem
When I try to access page as http://localhost/flask-demo
, am getting 404. But if I change 2nd line in myapp.py
from @app.route('/')
to @app.route('/flask-demo')
where flask-demo
is the name of the IIS web application where Python files are placed, it works.
I wish to serve Python web page without writing flask-demo
/ name of IIS web application where Python flask app is placed. Am not able to do that.
I tried to use app.config['APPLICATION_ROOT'] = '/flask-demo'
, but it didn't worked.
What is the best way to get this working? Followed article https://medium.com/@bilalbayasut/deploying-python-web-app-flask-in-windows-server-iis-using-fastcgi-6c1873ae0ad8 but it didn't helped.