Python Falcon 简明教程

Python Falcon - Deployment

可以使用启用 mod_wsgi 模块的 Apache 服务器部署 Falcon Web 应用程序,就像任何 WSGI 应用程序一样。另一种方法是使用 uWSGIgunicorn 进行部署。

It is possible to use Apache server enabled with the mod_wsgi module to deploy a Falcon web app, just as any WSGI app. Another alternative is to use uWSGI or gunicorn for deployment.

uWSGI 是一个快速且高度可配置的 WSGI 服务器。如果与 NGNIX 一起使用,它将以速度的形式在生产就绪环境中提供更好的性能。

The uWSGI is a fast and highly configurable WSGI server. If used along with NGNIX, it gives better performance in the form of speed in the production ready environment.

首先,在 Python 虚拟环境中使用 PIP 安装程序安装 Falcon 和 uWSGI,并使用 wsgi.py 将 Falcon 的应用程序对象公开给 uWSGI,如下所示 −

First, install Falcon and uWSGI in a Python virtual environment with PIP installer and expose the Falcon’s application object to uWSGI it with wsgi.py as below −

import os
import myapp
config = myproject.get_config(os.environ['MYAPP_CONFIG'])
application = myapp.create(config)

要配置 uWSGI,请准备一个 uwsgi.ini 脚本,如下所示 −

To configure uWSGI, prepare a uwsgi.ini script as below −

[uwsgi]
master = 1
vacuum = true
socket = 127.0.0.1:8080
enable-threads = true
thunder-lock = true
threads = 2
processes = 2
virtualenv = /path/to/venv
wsgi-file = venv/src/wsgi.py
chdir = venv/src
uid = myapp-runner
gid = myapp-runner

你现在可以像这样启动 uWSGI −

You can now start the uWSGI like this −

venv/bin/uwsgi -c uwsgi.ini

虽然 uWSGI 可以直接处理 HTTP 请求,但使用 NGINX 等反向代理可能会很有帮助。NGINX 本地支持 uWSGI 协议,以便有效地将请求代理到 uWSGI。

Although uWSGI may serve HTTP requests directly, it can be helpful to use a reverse proxy such as NGINX. NGINX natively supports the uwsgi protocol, for efficiently proxying requests to uWSGI.

安装 Ngnix,然后创建一个 NGINX conf 文件,如下所示 −

Install Ngnix and then create an NGINX conf file that looks something like this −

server {
   listen 80;
   server_name myproject.com;
   access_log /var/log/nginx/myproject-access.log;
   error_log /var/log/nginx/myproject-error.log warn;
   location / {
      uwsgi_pass 127.0.0.1:8080
      include uwsgi_params;
   }
}

终于启动了 Ngnix 服务器。您应该有正在运行的工作应用程序。

Finally start the Ngnix server. You should have a working application running.