预备:
- 假定IIS已经安装
- 安装 Python
1. 安装web版 ( Python 3.7.0).
2. 按管理员权限运行
3. 自定义安装,并适用于所有人
4. 自定义文件夹 "D:\python\python3.7"
5. 添加到系统路径.
PATH = D:\Python\Python37;D:\Python\Python37\Scripts
- 安装Wfastcgi,并是能.
- 管理员权限打开命令终端
- 运行: pip install wfastcgi
如上指令安装 wfastcgi. 该包在 IIS 和 Python之间使用 WSGI 和 FastCGI建立桥梁 - 终端中继续执行: wfastcgi-enable
该命令将会产生相关信息如下:
"""
Applied configuration changes to section "system.webserver/fastcgi" for "MACHINE/WEBROOT/APPHOST" at configuration commit path "MACHINE/WEBROOT/APPHOST".
" D:\python\python37\python.exe| D:\python\python37\lib\site-packages\wfastcgi.py" can now be used as a FastCGI script processor.
Below tag is created in the app host config.
<fastCgi> <application fullPath="D:\python\python37\python.exe" arguments="D:\python\python37\lib\site-packages\wfastcgi.py" signalBeforeTerminateSeconds="30" /> </fastCgi> |
"""
该步骤执行结果配置可以放在web.config中。
设置 Website
==================
- IIS中添加Website
- 添加必要信息 (此处端口设为 8001).
- 物理路径为网页所在位置及web.config和myapp.py (Web.config and myapp.py):
- web.config: 包含web配置,如下内容(注意红色字体必须为app):
<configuration> <system.webServer> <handlers> <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\Python\Python37\python.exe|D:\Python\Python37\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" /> </handlers> </system.webServer> <appSettings> <!-- Required settings --> <add key="WSGI_HANDLER" value="myapp.app" /> <add key="PYTHONPATH" value="D:\inetpub\wwwroot\MyPython" /> </appSettings> </configuration> |
- myapp.py: contains Flask application
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello welcome to the website!" if __name__ == '__main__': app.run() |
IIS安装好CGI后在FastCGI 设置中设置好Handler Mappings.
Under FastCGI settings, I have the following:
- Under Handler Mappings, you'll see different names. Based on web.config, you'll see "Python FastCGI".
- NOTE : 应用程序池设置 (D:\Python\Python37) for the to user “IIS AppPool\MyPython” .
- 浏览器打开 "http://localhost:8001"
测试时发现web重启不能立刻更新内容,需要在应用程序线程池里点击回收才能立刻更新。
参考
https://segmentfault.com/a/1190000008909201
windows IIS部署python Flask网站_david_lee13的博客-CSDN博客_flask iis部署