IIS website deployment FLASK

In the Windows platform to deploy Python-based Web site is a very frustrating thing, there are many options under Linux / Unix platforms, we record the main steps Flask deployed to IIS, I hope for your help.

Involving tools and platforms

  • Windows 7 x64

  • Python 3.4+

  • Flask

Hello Flask complete website

This is one of the simplest Flask website:

# hello.py
from flask import Flask
app=Flask(__name__)

@app.route('/',methods=['GET'])
def index(): return "Hello Flask!" if __name__=='__main__': app.run(debug=True)

Run python hello.pyno error description after your Python environment everything is normal, we can continue to step back.

Install IIS, enable CGI

Found in the control panel to open or close Windows feature , and install IIS CGI, as shown below.

iis+cgi

URL rewriting component installation

IIS URL rewriting component needs to be installed, this can by Microsoft Web Platform Installer to install. Download the Microsoft Web Platform Installer runs, searching url, were installed.

wpi-url

Note: It is said IIS on Windows10 10 now does not support the url rewrite? To be verified

Installation wfastcgi

By pip can be installed:

pip install wfastcgi

Enable wfastcgi

Only a few remaining things configured. First Run as administrator wfastcgi-enableto enable on IIS wfastcgi, this command is located c:\python_dir\scripts, that is, you need to make sure this directory in the PATH system, or you need to cd to the directory and then executed.

# cd to python_dir\scripts if it is not in PATH wfastcgi-enable

Remember command information returned after successful:

C:\Python34\Scripts> wfastcgi-enable
Applied configuration changes to section "system.webServer/fastCgi" for "MACHINE/WEBROOT/APPHOST" at configuration commit path "MACHINE/WEBROOT/APPHOST" "C:\Python34\python.exe|C:\Python34\lib\site-packages\wfastcgi.py" can now be used as a FastCGI script processor

"C: Python34python.exe | C: Python34libsite-packageswfastcgi.py" need to use the following configuration file.

Tips: 使用命令 wfastcgi-disable 可以将其移除。

创建 web.config 文件

下面是一个web.config文件的例子,你只需要修改对应部分就可以使用。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer> <handlers> <!-- scriptProcessor 的值来自命令行工具 wfastcgi-enable --> <add name="FlaskFastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Python34\python.exe|C:\Python34\lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" /> </handlers> <security> <!-- URL 重写中的特殊字符,比如加号+等等 --> <requestFiltering allowDoubleEscaping="true"></requestFiltering> </security> </system.webServer> <appSettings> <!-- Required settings --> <!-- 在这里指定Falsk app在模块中的具体位置 --> <add key="WSGI_HANDLER" value="hello.app" /> <add key="PYTHONPATH" value="~/" /> <!-- Optional settings --> <!-- 需要先创建日志目录,否则报错 --> <add key="WSGI_LOG" value="C:\logs\oboeqa_web.log" /> <add key="WSGI_RESTART_FILE_REGEX" value="" /> </appSettings> </configuration>

配置 IIS 目录及权限

假设你的 Flask 程序将部署在 C:websitehello 下面,那么你的目录结构大致如此。

C:\WEBSITE
└───hello
        hello.py web.config

现在你需要让IIS用户拥有访问和执行你的网站脚本的权限,进入 C:website 目录,执行下面两条命令:

cd C:\website
icacls . /grant "NT AUTHORITY\IUSR:(OI)(CI)(RX)"
icacls . /grant "Builtin\IIS_IUSRS:(OI)(CI)(RX)"

创建并访问你的网站

现在你离成功只差一步之遥,打开 IIS 管理面板,新建一个网站。

iis-add-web-site

你只需要填上网站名称,物理地址和相应的端口号,点击确认。

iis-website-config

打开浏览器,就可以访问你配置好的网站。如果有错误,可以去检查 web.config 中配置的日志文件。

iis-web-site-ok

简单总结

写完之后发现其实要完成的步骤并不是特别复杂,但是从摸索到实践的过程确实不易。本文仅讨论了部署的主要步骤,其实真正的生产环境你要考虑的问题可能更多,比如使用virtualenv 对网站进行隔离,安全问题,静态文件解析等等。

最后的惊喜

据说部署Python 网站到 IIS 还有更简单的办法,那就是安装宇宙最强的IDE - Visual Studio 2015 (VS2017 暂不支持 Python 开发),个人开发者可以免授权使用社区版。在 VS 中你可以使用 PTVS 来快捷开发并部署Python 程序,真正让你一键无忧。

ptvs-web

PTVS 支持了常见的 Python Web 框架,比如 Flask,Django,Bottle,Jade 等等,调试的时候只需要按 F5,部署右键选择 publish,跟着向导一步两步你就可以完成魔鬼的步伐。

link: https://segmentfault.com/a/1190000008909201

IIS permission issues when you call COM components

solution:

In the Component Services DCOM components to MICROSOFT.EXCEL given operating authority ASP.NET specific steps:
 (1) Open the Start menu's Run dialog box, type dcomcnfg, and determined that the Component Services window will pop up
 (2) Expand Computer -> my computer -> DCOM configuration, locate Microsoft Excel application node
 (3) right-click -> properties, select the "safe" option, in the following three projects have chosen "custom", and click the edit button
 ( 4) in the launch permission dialog box, click the Add button to add the appropriate user (Note: If you are WIN2000, XP, then add "machine name / ASPNET" user, I am here is an example WIN2003,
 WIN2003 is to add "NETWORK Service" user), and give maximum permissions
 (5) select the "identity", then select "interactive user" to
the above components is set EXCEL, you need to find com components you use in your dcomcnfg in the

text: https: / /blog.csdn.net/BleuRever/article/details/50783133

Guess you like

Origin www.cnblogs.com/wfwup/p/10728557.html