Python Pyramid 简明教程

Python Pyramid - Deployment

本教程中到目前为止开发的 Pyramid 应用程序示例已在本地机器上执行。要使其公开访问,必须将其部署在能够满足 WSGI 标准的生产服务器上。

The examples of Pyramid applications developed so far in this tutorial have been executed on the local machine. To make it accessible publicly, it must be deployed on a Production server capable of WSGI standards.

为此提供了许多与 WSGI 兼容的 http 服务器。例如 -

Many WSGI compatible http servers are available for this purpose. For example −

  1. waitress

  2. paste.httpserver

  3. CherryPy

  4. uWSGI

  5. gevent

  6. mod_wsgi

我们已经讨论了如何使用 Waitress 服务器来托管 Pyramid 应用程序。它可以在具有公有 IP 地址的机器的 80 端口(HTTP)和 443 端口(HTTPS)上提供服务。

We have discussed how we can use Waitress server to host a Pyramid application. It can be served on ports 80 (HTTP) and 443 (HTTPS) of a machine having a public IP address.

mod_wsgi

Apache 服务器是 Apache Software Foundation 分发的流行开源 HTTP 服务器软件。它为互联网上的大多数 Web 服务器提供支持。 mod_wsgi (由 Graham Dumpleton 开发)是 Apache 模块,它为在 Apache 上部署基于 Python 的 Web 应用程序提供了 WSGI 接口。

Apache server is a popular open source HTTP server software, distributed by Apache Software Foundation. It powers most of the web servers across internet. The mod_wsgi (developed by Graham Dumpleton) is an Apache module that provides a WSGI interface for deploying Python based web applications on Apache.

本节解释了在 Apache 服务器上部署 Pyramid 应用程序的分步过程。在此,我们将使用 XAMPP,这是一个流行的开源 Apache 发行版。它可以从 https://www.apachefriends.org/download.html. 下载

In this section, the step by step procedure to deploy a Pyramid application on the Apache server is explained. Here, we’ll use XAMPP, a popular open source Apache distribution. It can be downloaded from https://www.apachefriends.org/download.html.

mod_wsgi 模块使用 PIP 安装程序安装。在安装之前,将 MOD_WSGI_APACHE_ROOTDIR 环境变量设置为 Apache 可执行文件所在的目录。

The mod_wsgi module is installed with PIP installer. Before installing, set the MOD_WSGI_APACHE_ROOTDIR environment variable to the directory in which Apache executable is located.

C:\Python310\Scripts>set MOD_WSGI_APACHE_ROOTDIR=C:/xampp/apache
C:\Python310\Scripts>pip install mod_wsgi

接下来,在命令终端中运行以下命令。

Next, run the following command in the command terminal.

C:\Python310\Scripts>mod_wsgi-express module-config
LoadFile "C:/Python310/python310.dll"
LoadModule wsgi_module "C:/Python310/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd"
WSGIPythonHome "C:/Python310"

这些是 mod_wsgi 模块设置,需纳入 Apache 的配置文件。打开 XAMPP 安装的 httpd.conf 文件,然后在其中复制上述命令行的输出。

These are mod_wsgi module settings to be incorporated Apache’s configuration file. Open httpd.conf file of your XAMPP installation and copy the output of the above command line in it.

接下来,为我们的应用程序创建一个虚拟主机配置。Apache 将虚拟主机信息存储在 httpd-vhosts.conf 文件中,该文件位于 C:\XAMPP\Apache\conf\extra\ 文件夹中。打开该文件,并在其中添加以下行 −

Next, create a virtual host configuration for our application. Apache stores virtual host information in httpd-vhosts.conf file which is found in C:\XAMPP\Apache\conf\extra\ folder. Open the file and add following lines in it −

<VirtualHost *>
   ServerName localhost:6543
   WSGIScriptAlias / e:/pyramid-env/hello/production.ini
   <Directory e:/pyramid-env/hello>
      Order deny,allow
      Allow from all
      Require all granted
   </Directory>
</VirtualHost>

此处假定使用 Cookiecutter 实用程序构建了 hello Pyramid 项目。此处使用要在生产环境中使用的 PasteDeploy 配置文件。

Here, it is assumed that a hello Pyramid project is built using the Cookiecutter utility. The PasteDeploy configuration file to be used in production environment is used here.

此虚拟主机配置需要纳入 Apache 的 httpd.conf 文件。方法是在其中添加以下行 −

This virtual host configuration needs to be incorporated in Apache’s httpd.conf file. This is done by adding following lines in it −

# Virtual hosts
   Include conf/extra/httpd-vhosts.conf

现在,我们必须将以下代码另存为 pyramid.wsgi 文件,该文件返回 Pyramid WSGI 应用程序对象。

We now have to save the following code as pyramid.wsgi file that returns the Pyramid WSGI application object.

from pyramid.paster import get_app, setup_logging
ini_path = 'e:/pyramid-env/hello/production.ini'
setup_logging(ini_path)
application = get_app(ini_path, 'main')

在执行上述过程后,重新启动 XAMPP 服务器,我们应该能够在 Apache 服务器上运行 Pyramid 应用程序。

After performing the above mentioned procedure, restart the XAMPP server and we should be able to run the Pyramid application on the Apache server.

Deploy on Uvicorn

Uvicorn 是 ASGI 兼容的服务器(ASGI 表示异步网关接口)。由于 Pyramid 是基于 WSGI 的 Web 框架,因此我们需要使用 asgiref.wsgi 模块中定义的 WsgiToAsgi() 函数将 WSGI 应用程序对象转换为 ASGI 对象。

Uvicorn is an ASGI compatible server (ASGI stands for Asynchronous Gateway Interface). Since Pyramid is a WSGI based web framework, we need to convert the WSGI application object to ASGI object, with the help of WsgiToAsgi() function defined in asgiref.wsgi module.

from asgiref.wsgi import WsgiToAsgi
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
   return Response("Hello")

with Configurator() as config:
   config.add_route("hello", "/")
   config.add_view(hello_world, route_name="hello")
   wsgi_app = config.make_wsgi_app()

app = WsgiToAsgi(wsgi_app)

将以上代码另存为 app.py。使用 pip 实用程序安装 Uvicorn。

Save the above code as app.py. Install Uvicorn with pip utility.

pip3 install uvicorn

在 ASGI 模式下运行 Pyramid 应用程序。

Run the Pyramid application in ASGI mode.

uvicorn app:app

类似地,它可以使用 daphne 服务器提供服务。

Similarly, it can be served using daphne server.

daphne app:app