Windows

Windows有四种可能的部署选项:

  1. 以Windows服务的形式运行,使用基于python的Web服务器,如cherrypy或twisted

  2. 使用反向代理在另一个Web服务器(IIS或Apache)后面作为Windows服务运行

  3. 在IIS内部使用带有isapi-wsgi的wsgi桥

  4. 在IIS中使用wsgi桥和pyisapie

选项1和2:作为Windows服务运行

两个选项1和2与运行开发服务器非常相似,除了关闭调试信息,并且希望将进程运行为Windows服务。

安装依赖项

Running as a Windows service depends on the PyWin32 项目。您需要下载与您的Python版本相匹配的预构建二进制文件。

如果运行virtualenv,则可以直接安装到virtualenv中。 easy_install 在下载的安装程序上。例如::

easy_install pywin32-217.win32-py2.7.exe

因为Web服务器 CherryPy 具有良好的Windows支持,可用于python 2和3,并且可以根据服务的需要优雅地启动和停止,我们将使用它作为Web服务器。您还可以替换另一个Web服务器,如 Twisted .

安装Cherrypy管路:

pip install cherrypy

Create a Windows service

创建一个名为 pyramidsvc.py 使用以下代码定义您的服务:

 1# uncomment the next import line to get print to show up or see early
 2# exceptions if there are errors then run
 3#   python -m win32traceutil
 4# to see the output
 5#import win32traceutil
 6import win32serviceutil
 7
 8PORT_TO_BIND = 80
 9CONFIG_FILE = 'production.ini'
10SERVER_NAME = 'www.pyramid.example'
11
12SERVICE_NAME = "PyramidWebService"
13SERVICE_DISPLAY_NAME = "Pyramid Web Service"
14SERVICE_DESCRIPTION = """This will be displayed as a description \
15of the serivice in the Services snap-in for the Microsoft \
16Management Console."""
17
18class PyWebService(win32serviceutil.ServiceFramework):
19    """Python Web Service."""
20
21    _svc_name_ = SERVICE_NAME
22    _svc_display_name_ = SERVICE_DISPLAY_NAME
23    _svc_deps_ = None        # sequence of service names on which this depends
24    # Only exists on Windows 2000 or later, ignored on Windows NT
25    _svc_description_ = SERVICE_DESCRIPTION
26
27    def SvcDoRun(self):
28        from cheroot import wsgi
29        from pyramid.paster import get_app
30        import os, sys
31
32        path = os.path.dirname(os.path.abspath(__file__))
33
34        os.chdir(path)
35
36        app = get_app(CONFIG_FILE)
37
38        self.server = wsgi.Server(
39                ('0.0.0.0', PORT_TO_BIND), app,
40                server_name=SERVER_NAME)
41
42        self.server.start()
43
44
45    def SvcStop(self):
46        self.server.stop()
47
48
49if __name__ == '__main__':
50    win32serviceutil.HandleCommandLine(PyWebService)

这个 if __name__ == '__main__' 块提供了一个接口来注册服务。您可以通过运行以下命令在系统中注册服务:

python pyramidsvc.py install

您的服务现在可以启动了,您可以通过Microsoft管理控制台的“常规服务”管理单元或运行以下命令来执行此操作:

python pyramidsvc.py start

如果希望服务自动启动,可以运行:

python pyramidsvc.py update --start=auto

反向代理(可选)

如果要在同一台机器上运行多个 Pyramid 应用程序,则需要在不同的端口和单独的服务中运行它们中的每一个。如果您希望能够通过端口80上的不同主机名访问每个服务器,那么您需要在前面运行另一个Web服务器(IIS或Apache),并代理返回到适当的服务。

有几个选项可用于使用IIS的反向代理。在从IIS 7开始的版本中,可以安装和使用 Application Request Routing 如果要使用Microsoft提供的解决方案。另一种选择是 Helicon Tech . Helicon Ape is available without cost for up to 3 sites.

如果您还没有使用IIS,那么Apache对Windows是可用的,并且工作得很好。Apache提供了许多反向代理教程,它们都适用于Windows。

选项3和4:在IIS中使用带有isapi-wsgi的wsgi桥

IIS配置

Turn on Windows feature for IIS.

控制面板->“关闭Windows功能”并选择:

  • 互联网信息服务(全部)

  • World Wide Web Services (all)

创建网站

转到Internet信息服务管理器并添加网站。

  • Site name (your choice)

  • 物理路径(指向 Pyramid 项目的目录)

  • 选择端口

  • 选择网站名称

Python

创建桥接脚本

创建文件 install_website.py 并将其放置在 Pyramid 项目中:

 1# path to your site packages in your environment
 2# needs to be put in here
 3import site
 4site.addsitedir('/path/to/your/site-packages')
 5
 6# this is used for debugging
 7# after everything was installed and is ready to meka a http request
 8# run this from the command line:
 9# python -m python -m win32traceutil
10# It will give you debug output from this script
11# (remove the 3 lines for production use)
12import sys
13if hasattr(sys, "isapidllhandle"):
14    import win32traceutil
15
16
17# this is for setting up a path to a temporary
18# directory for egg cache.
19import os
20os.environ['PYTHON_EGG_CACHE'] = '/path/to/writable/dir'
21
22# The entry point for the ISAPI extension.
23def __ExtensionFactory__():
24    from paste.deploy import loadapp
25    import isapi_wsgi
26    from logging.config import fileConfig
27
28    appdir = '/path/to/your/pyramid/project'
29    configfile = 'production.ini'
30    con = appdir + configfile
31
32    fileConfig(con)
33    application = loadapp('config:' + configfile, relative_to=appdir)
34    return isapi_wsgi.ISAPIThreadPoolHandler(application)
35
36# ISAPI installation
37if __name__ == '__main__':
38    from isapi.install import ISAPIParameters, ScriptMapParams, VirtualDirParameters, HandleCommandLine
39
40    params = ISAPIParameters()
41    sm = [
42        ScriptMapParams(Extension="*", Flags=0)
43    ]
44
45    # if name = "/" then it will install on root
46    # if any other name then it will install on virtual host for that name
47    vd = VirtualDirParameters(Name="/",
48                              Description="Description of your proj",
49                              ScriptMaps=sm,
50                              ScriptMapUpdate="replace"
51    )
52
53    params.VirtualDirs = [vd]
54    HandleCommandLine(params)

将Pyramid项目安装为虚拟主机或根功能

激活虚拟环境并运行stript::

python install_website.py install --server=<name of your website>

Restart your website from IIS.