如何在IIS中部署Flask应用程序? [英] How do I deploy a Flask application in IIS?
问题描述
下面是我为isapi-wsgi编写的Python脚本。虚拟目录是在IIS管理器中完成的,并且一切看起来都正常,但是这个站点没有工作。
$ b $
from wof import app
导入os
$ b $ app.secret_key = os.urandom(24)
导入isapi_wsgi
def __ExtensionFactory __():
return isapi_wsgi.ISAPISimpleHandler应用程序)
if __name__ =='__main__':
from isapi.install import *
params = ISAPIParameters()
sm = [ScriptMapParams(Extension =* ,Flags = 0)]
vd = VirtualDirParameters(Name =WOFPy_Sondes,Description =ISAPI-WSGI for WOFPY Sondes test,ScriptMaps = sm,ScriptMapUpdate =replace)
params.VirtualDirs = [vd]
HandleCommandLine(params)
高级概述
HTTP - > IIS - > ISAPI - > FastCGI - > WSGI(Flask应用程序)
安装步骤
步骤1:安装所需的二进制文件
- 安装Python(2.7或3.x - 我用过3.3)
安装pip-Win(我使用1.6版本)
安装pywin32我使用了218版本) - 使用 fcgisetup 1.5安装IIS FastCGI扩展
步骤2:安装可选的二进制包
I使用此网站中的安装程序.exe安装 pyodbc
>。从源代码安装(例如,用于安装到虚拟环境中的pip)需要一个C / C ++编译器。
第三步:获取 wfastcgi .py
选择一个适合您的版本,最好是支持Python 3.3的版本(我使用 David Ebbo )。您可能需要从这里的官方版本。
<将
wfastcgi.py
脚本安装到 C:\ Inetpub\wwwroot
中,并确保将为您的应用程序提供服务(默认情况下为网络服务)具有读权限。 第四步:安装 virtualenv
进入系统站点包
C:\Python33\Scripts\pip.exe install virtualenv
(如果您使用Python 3.3并将所有内容安装在默认位置)
步骤5:安装你的Flask应用程序
-
你可以在系统的任何地方安装应用程序。您可能需要将它安装在
C:\ Inetpub
下。在本教程中,我们将调用应用程序的根文件夹安装%APPROOT%
。 (请勿在环境变量中加引号。)
-
请确保将为您的应用程序提供服务的帐户(默认为Network Service)读取所有脚本文件的访问权限。这个命令:
cacls%APPROOT%/ S:D:PAI(A; OICI; FA ;;; BA )(A; OICII; FA ;;; CO)(A; OICI; 0x1200a9 ;;; NS)(A; OICI; FA ;;; SY)
$ p
$ b会给你的应用程序目录下面的权限:
$ b $ ul
- BUILTIN \管理员:完全控制此文件夹,子文件夹和文件
- 创建者所有者:仅控制子文件夹和文件
- NT AUTHORITY \NETWORK SERVICE:Read文件夹,子文件夹和文件的权限
- NT AUTHORITY \SYSTEM:完全控制此文件夹,子文件夹和文件
添加必要的本地配置(我的应用程序使用版本控制系统忽略的local.cnf文件)数据库URL。
Web.config
>%APPROOT% - 有关文件格式的信息,请参阅以下部分。 >第6步:为您的应用程序创建一个virtualenv $ b $ pre code C:\Python33\Scripts\virtualenv.exe --system -site-packages%APPROOT%\env
如果您的应用程序已经使用了该目录,那么$ c> env 。)
第七步:安装你的应用程序所需的包到virtualenv h2>
cd%APPROOT%
env \Scripts\activate
pip install -r软件包
cd%APPROOT%
env \Scripts\activate
pip install -r软件包
(我的项目将需求规格保存在一个名为 使用<$> Packages $ c $ )步骤8:为你的应用程序创建一个Web站点或虚拟目录
(开始 - > 运行,然后在编辑框中输入
c $ c> inetmgr.msc
inetmgr
,然后按 ENTER )启动Internet信息服务(IIS) EM>。确保将创建的节点(网站或虚拟目录)的本地路径设置为Flask应用程序的根文件夹。 wfastcgi.py
使用本地路径来识别Flask应用程序来处理请求。
阅读和脚本(运行脚本)权限。
步骤9:配置 fcgiext。 ini
该文件与安装的 fcgiext.dll
位于同一目录在步骤1(默认情况下,%SYSTEMROOT%\system32\tstsvv
)。
,您需要几个参数:
- {网站ID} :您可以在详细信息中找到的数字网站ID当窗口左侧的树中选择网站时, Internet信息服务(IIS)管理器的右侧窗格(右侧)。
- {应用程序名称} :提供FastCGI(ISAPI)处理程序参数的
fcgiext.ini
内部分的名称。您选择此值 - 选择代表您的应用程序的内容。 - {应用程式路径} :对于虚拟目录,网站内的网址路径要处理的虚拟目录。
- {approot} :应用程序根目录的路径。
使用这些参数:
$ ul
- 对于整个网站,添加
*:< b> {site id}
- 对于虚拟目录,添加
*:/ lm / w3svc / {网站ID} / root / } = {application name}
到[Types]
部分。添加一个处理部分([ {application name} ]
>)与这个应用程序的参数( -
ExePath = {approot} -iis-60> full reference ): \env\python.exe
-
参数= C:\ Inetpub\wwwroot\wfastcgi。 py
(或安装wfastcgi.py
适配器脚本的地方) / li>
-
EnvironmentVars = ENV_VAR1:值,ENV_VAR2:值,等
(请参阅引用规则的完整参考)。这是设置 WSGI_LOG环境变量的好地方 - 确保为网站提供服务的帐户(网络服务默认情况下)对文件具有写权限(如果该文件不存在),则可以将文件添加到包含目录中。
将FastCGI请求映射到一个处理部分:
= {application name} 到
[Types]
部分。 / li>
步骤10:为目标URL配置FastCGI处理
使用Internet信息服务(IIS)管理器,从由Flask应用程序服务的节点(网站或虚拟目录)的上下文(右键单击)菜单中选择属性...。
$在主目录选项卡(网站)或虚拟目录选项卡(虚拟目录)中,单击配置...按钮。 在通配符应用程序映射部分,使用插入...按钮添加通配符映射:
- 可执行文件是在步骤1中安装的FastCGI扩展DLL。它的默认位置是
%SYSTEMROOT%\system32\inetsrv\fcgiext.dll
。 - 确保确认文件存在未选中 。 Flask应用程序执行自己的路由,与磁盘上的文件无关。
Web.config
这个文件(在这个设置中)由<$ c $
< / p> ?xml version =1.0encoding =UTF-8?>
<配置>
< applicationSettings>
< add key =PYTHONPATHvalue =/>
< add key =WSGI_HANDLERvalue =module.application/>
< / applicationSettings>
< / configuration>
-
< add>
元素在Python中添加环境变量(os.environ
)。 必须指定code> WSGI_HANDLER - 它告诉 -
PYTHONPATH
特别处理 -wfastcgi.py
执行(环境)变量扩展(使用Windows标准%VAR%
表示法)对PYTHONPATH
的值进行分割,然后以分号和附加形式在调用WSGI应用程序之前将条目添加到sys.path
中。由于wfastcgi.py
在导入包含WSGI应用程序对象的模块(包括空字符串)之前将当前目录更改为指定为Web站点或虚拟目录的本地路径的路径在PYTHONPATH中将导致搜索包含您的Flask应用程序目录作为起点。你也可以在fcgiext.ini
中设置PYTHONPATH(在这种情况下,解释器将它包含在sys.path
中)然后是wfastcgi.py
)。 -
WSGI_RESTART_FILE_REGEX 给出了一个Python正则表达式,用于过滤应该触发FastCGI处理程序进程重新启动的路径的文件更改通知。将其设置为在源文件或配置文件更改时触发。我使用 (?i)。* \。(py | cnf | config)$
。 -
WSGI_LOG
可以在这里设置,但是我认为最好在fcgiext.ini
中设置。
wfastcgi.py
如何找到WSGI应用程序对象。如果值以()结尾,那么 wfastcgi.py
将调用指定的对象,期望它返回一个WSGI应用程序对象。 对于IIS 7
FastCGI的一些事情随着IIS 7而发生了巨大的变化。从这个版本开始,FastCGI直接通过IIS提供支持,而不是通过扩展来配置(例如,步骤1.4是不必要的, fcgiext.ini
不能控制IIS 7+的FastCGI行为,并且不需要创建/编辑它)。请确保在控制面板>程序和功能>打开或关闭Windows功能中的 Internet信息服务下启用 CGI 。 p>
Web.config
IIS 7是第一个版本的IIS从 Web.config
文件读取与FastCGI相关的配置设置。您的 Web.config
文件将需要在< configuration>
元素中包含一个< system.webServer>
包含< handlers>
的元素包含< add>
元素与属性:
$ ul
*
*
FastCgiModule
未指定
$ b
$ b scriptProcessor: > scriptProcessor
属性
< add>
元素必须包含要使用的Python解释器 .exe
文件的完整路径( Scripts
你的Python virtualenv的子文件夹)后跟一个 |
,然后选择 wfastcgi.py
文件的完整路径正在使用。由于这些路径取决于运行应用程序的机器的设置,因此您可能希望将此属性设置为部署过程的一部分。
$ b IIS服务器在$ inetmgr
中,点击树中的服务器节点然后从中心窗格中选择 FastCGI设置。
为您的 python.exe
添加一个完整路径的条目,并将 wfastcgi.py
您正在使用。两者都应该按照它们在 Web.config $ c>中< handlers> /< add>
确保在新的FastCGI应用程序条目中设置 PYTHONPATH
环境变量,以包含应用程序的根目录代码库。关于在 Web的< code $ c>中添加一个空的 PYTHONPATH
条目的建议。 config
可能不适用于此版本的IIS。
Can anyone help me get a Flask application running on IIS 6? I have tried to use isapi-wsgi, but when I visit the Virtual Directory address I get a page that says "The specified module could not be found." Are there other options for this?
Below is the Python script I wrote for isapi-wsgi. The Virtual Directory was made and everything looked ok in IIS Manager, but the site did not work.
from wof import app
import os
app.secret_key=os.urandom(24)
import isapi_wsgi
def __ExtensionFactory__():
return isapi_wsgi.ISAPISimpleHandler(app)
if __name__ == '__main__':
from isapi.install import *
params = ISAPIParameters()
sm = [ScriptMapParams(Extension="*", Flags=0)]
vd = VirtualDirParameters(Name="WOFPy_Sondes", Description="ISAPI-WSGI for WOFPY Sondes test", ScriptMaps=sm, ScriptMapUpdate="replace")
params.VirtualDirs = [vd]
HandleCommandLine(params)
解决方案 High Level Overview
HTTP -> IIS -> ISAPI -> FastCGI -> WSGI (Flask application)
Setup Steps
Step 1: Install Required Binaries
- Install Python (2.7 or 3.x -- I used 3.3)
- Install pip-Win (I used version 1.6)
- Install pywin32 (I used version 218)
- Install the IIS FastCGI extension with fcgisetup 1.5
Step 2: Install Optional Binary Packages
I installed pyodbc
using the installer .exe from this site. Installing from source (e.g. pip, for installing into a virtual environment) requires a C/C++ compiler.
Step 3: Get a Copy of wfastcgi.py
Choose a version that will work for you, preferably one that supports Python 3.3 (I used David Ebbo's). You may want the "official" version from here.
Install the wfastcgi.py
script into C:\Inetpub\wwwroot
and make sure the account that will serve your application ("Network Service" by default) has read access to it.
Step 4: Install virtualenv
Into the System site-packages
C:\Python33\Scripts\pip.exe install virtualenv
(if you are using Python 3.3 and installed everything in the default location)
Step 5: Install Your Flask Application
You may install the application just about anywhere on the system. You may want to install it under C:\Inetpub
. For this tutorial, we'll call the root folder of your application install %APPROOT%
. (Don't put quotation marks in the environment variable.)
Make sure that the account that will serve your application ("Network Service" by default) has read access to all of the script files. This command:
cacls "%APPROOT%" /S:"D:PAI(A;OICI;FA;;;BA)(A;OICIIO;FA;;;CO)(A;OICI;0x1200a9;;;NS)(A;OICI;FA;;;SY)"
will give your application directory the following permissions:
- BUILTIN\Administrators: Full control of this folder, subfolders, and files
- CREATOR OWNER: Full control for subfolders and files only
- NT AUTHORITY\NETWORK SERVICE: Read permissions for this folder, subfolders, and files
- NT AUTHORITY\SYSTEM: Full control of this folder, subfolders, and files
Add any local configuration necessary (my application uses a local.cnf file that is ignored by the version control system) -- e.g. database URLs.
Make sure your application contains a Web.config
file in %APPROOT%
-- see the section below for information on the file format.
Step 6: Create a virtualenv For Your Application
C:\Python33\Scripts\virtualenv.exe --system-site-packages "%APPROOT%\env"
(Pick a name other than env
if your application already uses that directory.)
Step 7: Install The Packages Required By Your Application to the virtualenv
cd "%APPROOT%"
env\Scripts\activate
pip install -r Packages
(My project keeps the requirements spec in a file named Packages
.)
Step 8: Create a Web Site Or Virtual Directory For Your Application
Use inetmgr.msc
(Start -> Run…, then enter inetmgr
in the edit box and press ENTER) to launch Internet Information Services (IIS) Manager. Make sure to set the local path for the node (Web Site or Virtual Directory) you create to the root folder of your Flask application. wfastcgi.py
uses the local path to identify the Flask application to handle the requests.
Give both Read and Script (Run Scripts) permissions for the node.
Step 9: Configure fcgiext.ini
This file is located in the same directory as the fcgiext.dll
installed in Step 1 (by default, %SYSTEMROOT%\system32\inetsrv
).
In configuring this file, you need several parameters:
- {site id}: the numeric Site ID you can find in the detail (right-hand) pane of Internet Information Services (IIS) Manager when "Web Sites" is selected from the tree on the left side of the window.
- {application name}: the name of the section within
fcgiext.ini
that provides the parameters for the FastCGI (ISAPI) handler. You choose this value -- select something that represents your application.
- {path to app}: for a Virtual Directory, the URL path within the Web Site to the Virtual Directory to be handled.
- {approot}: the path to the root directory of your application.
Use those parameters to:
Map the FastCGI requests to a handling section:
- For a whole Web Site, add
*:{site id}={application name}
to the [Types]
section.
- For a Virtual Directory, add
*:/lm/w3svc/{site id}/root/{path to app}={application name}
to the [Types]
section.
Add a handling section ([{application name}]
) with parameters for this application (full reference):
ExePath={approot}\env\python.exe
Arguments=C:\Inetpub\wwwroot\wfastcgi.py
(or wherever the wfastcgi.py
adapter script is installed)
EnvironmentVars=ENV_VAR1:value,ENV_VAR2:value,etc.
(see the full reference for quoting rules). This is a good place to set your WSGI_LOG environment variable -- make sure the account serving the site ("Network Service" by default) has write permissions for the file and (if the file doesn’t exist) permission to add a file to the containing directory.
Step 10: Configure FastCGI Handling for the Target URLs
Using Internet Information Services (IIS) Manager, select "Properties..." from the context (right-click) menu of the node (Web Site or Virtual Directory) to be served by your Flask application and:
In the "Home Directory" tab (Web Site) or "Virtual Directory" tab (Virtual Directory), click the "Configuration..." button.
In the "Wildcard application maps" section, use the "Insert..." button to add a wildcard mapping:
- The executable is the FastCGI extension DLL installed in Step 1. Its default location is
%SYSTEMROOT%\system32\inetsrv\fcgiext.dll
.
- Make sure "Verify that file exists" is unchecked. Flask applications do their own routing that doesn’t necessarily have anything to do with the files on the disk.
Web.config
This file is (in this setup) read by wfastcgi.py
, not by IIS.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<applicationSettings>
<add key="PYTHONPATH" value=""/>
<add key="WSGI_HANDLER" value="module.application"/>
</applicationSettings>
</configuration>
<add>
elements add environment variables (os.environ
in Python).
WSGI_HANDLER
must be specified -- it tells wfastcgi.py
how to locate the WSGI application object. If the value ends in "()", wfastcgi.py
will call the named object, expecting it to return a WSGI application object.
PYTHONPATH
is handled specially -- wfastcgi.py
performs (environment) variable expansion (using the Windows standard %VAR%
notation) on the value of PYTHONPATH
, then splits the result at semicolons and appends the entries to sys.path
before invoking the WSGI application. Because wfastcgi.py
changes the current directory to the path specified as the local path of the Web Site or Virtual Directory before importing the module containing the WSGI application object, including an empty string in the PYTHONPATH will cause the search to include your Flask application directory as a starting point. You can also set PYTHONPATH in fcgiext.ini
(in which case it is included in sys.path
by the interpreter and then again by wfastcgi.py
).
WSGI_RESTART_FILE_REGEX
gives a Python regular expression used to filter file-change notifications for paths that should trigger FastCGI handler process restarts. Set this to trigger when source files or configuration files change. I use (?i).*\.(py|cnf|config)$
.
WSGI_LOG
may be set here, but I think it is better set in fcgiext.ini
.
For IIS 7
Some things with FastCGI changed dramatically with IIS 7. Beginning with this version, FastCGI has support directly through IIS and is not configured through an extension (i.e. Step 1.4 is not necessary and fcgiext.ini
does not control FastCGI behavior for IIS 7+ and there is no need to create/edit it). Instead, make sure that CGI is enable under Internet Information Services in Control Panel > Programs and Features > Turn Windows Features on or off.
Web.config
IIS 7 is the first version of IIS to read configuration settings related to FastCGI from the Web.config
file. Your Web.config
file will need to contain, within the <configuration>
element, a <system.webServer>
element containing a <handlers>
element containing an <add>
element with the attributes:
- path:
*
- verb:
*
- modules:
FastCgiModule
- resourceType:
Unspecified
- requireAccess:
Script
- scriptProcessor: the tricky one
The scriptProcessor
Attribute
This attribute of the <add>
element must contain the full path to the Python interpreter .exe
file you want to use (the one in the Scripts
subfolder of your Python virtualenv) followed by a |
and then the full path to the wfastcgi.py
file you are using. As these paths are dependent on the setup of the machine on which your app is running, you may want to have this attribute set as part of your deployment process.
IIS Server-wide Set Up
- In
inetmgr
, click on the server node in the tree and then choose FastCGI Settings from the center pane. A list of executable/argument pairs will come up.
- Add an entry for the full paths to your
python.exe
and the wfastcgi.py
you are using. Both should be given the same way they show up in the <handlers>/<add>
element in your Web.config
.
- Make sure to set up the
PYTHONPATH
environment variable in the new FastCGI application entry to include the root of your application codebase. The advice about adding an empty PYTHONPATH
entry in the <applicationSettings>
of your Web.config
may not apply to this version of IIS.
这篇关于如何在IIS中部署Flask应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!