带 cookiecutter 的UWSGI Pyramid 应用第2部分:添加 Emperor 和系统¶
This guide will outline broad steps that can be used to add the uWSGI Emperor 和 systemd 到我们的CookieCutter应用程序 uWSGI
.
This is Part 2 of a two-part tutorial, and assumes that you have already completed Part 1: uWSGI with cookiecutter Pyramid 应用第1部分:基本UWSgi+nginx .
本教程是在Ubuntu18.04下开发的,但是所有系统的说明都应该大致相同,您可以在其中调整命令和文件的特定路径信息。
Uwsgi的常规调用¶
在第一部分,我们使用 --init-paste-logged
which got us two things almost for free: logging and an implicit WSGI entry point.
为了运行我们的 cookiecutter 应用程序 uWSGI Emperor ,我们需要遵循提供(显式)WSGi入口点的常规路线。
Within the project directory (
~/myproject
)创建名为wsgi.py
with the following code. This script is our WSGI entry point.1# Adapted from PServeCommand.run in site-packages/pyramid/scripts/pserve.py 2from pyramid.scripts.common import get_config_loader 3app_name = 'main' 4config_vars = {} 5config_uri = 'production.ini' 6 7loader = get_config_loader(config_uri) 8loader.setup_logging(config_vars) 9app = loader.get_wsgi_app(app_name, config_vars)
config_uri
是项目配置文件名。最好使用production.ini
file provided by your cookiecutter, as it contains settings appropriate for production.app_name
is the name of the section within the.ini
file that should be loaded byuWSGI
. 变量的赋值app
重要的是:我们将参考app
and the name of the file,wsgi.py
呼唤
loader.setup_logging
initializes the standard library'slogging
模块通过pyramid.paster.setup_logging()
允许在应用程序中登录。见 Logging Configuration .为项目的日志文件创建一个目录,并在该目录上设置所有权。
$ cd /var/log $ sudo mkdir uwsgi $ sudo chown ubuntu:www-data uwsgi
Uncomment these three lines of your
production.ini
文件。1[uwsgi] 2# Uncomment `wsgi-file`, `callable`, and `logto` during Part 2 of this tutorial 3wsgi-file = wsgi.py 4callable = app 5logto = /var/log/uwsgi/%(proj).log
wsgi-file
points to the explicit entry point that we created in the previous step.callable
is the name of the callable symbol (the variableapp
暴露于wsgi.py
.logto
指定应用程序日志的写入位置,这意味着日志将不再写入STDOUT
.Invoke uWSGI with
--ini
.使用调用Uwsgi
--ini
然后通过它.ini
文件是调用uwsgi的常规方法。(uwsgi也可以使用指定为命令行参数的所有配置选项来调用,但该方法本身不适合与 Emperor 轻松配置,因此我们不会在此处显示该方法。)$ cd ~/myproject $ sudo uwsgi --ini production.ini
一定要调用给
sudo
, or your application will not be able to masquerade as the users we specified foruid
和gid
.Also note that since we specified the
logto
parameter to be in/var/log/uwsgi
在这个终端窗口中,我们只能看到有限的输出。如果启动正确,您将看到:$ sudo uwsgi --ini production.ini [uWSGI] getting INI configuration from production.ini
Tail the log file at
var/log/uwsgi/myproject.log
.$ tail -f /var/log/uwsgi/myproject.log
并验证上一步的输出是否包含一条大致如下所示的行:
WSGI app 0 (mountpoint='/') ready in 1 seconds on interpreter 0x5615894a69a0 pid: 8827 (default app)
If any errors occurred, you will need to correct them. 如果你得到了
callable not found or import error
, make sure that yourproduction.ini
适当集wsgi-file
到wsgi.py
那~/myproject/wsgi.py
exists and contains the contents provided in a previous step. Also make sure that yourproduction.ini
适当集callable
到app
那app
中可调用符号的名称wsgi.py
.An import error that looks like
ImportError: No module named 'wsgi'
可能表明你的wsgi-file
指定在production.ini
不匹配wsgi.py
file that you actually created.对于任何其他导入错误,这可能意味着该包未安装或用户无法访问。这就是为什么我们选择伪装成您登录时的普通用户,所以您一定可以访问已安装的软件包。
在浏览器中访问http://localhost。交替呼叫
curl localhost
从一个终端。您应该看到呈现的示例应用程序。If the application does not render, follow the same steps you followed in uWSGI with cookiecutter Pyramid 应用第1部分:基本UWSgi+nginx
停止您的应用程序。现在我们已经证明了您的应用程序可以运行一个显式的WSGI入口点,您的应用程序已经准备好由UWSGI Emperor 管理。
Running Your application via the Emperor¶
Create two new directories in
/etc
.$ sudo mkdir /etc/uwsgi/ $ sudo mkdir /etc/uwsgi/vassals
创建一个
.ini
为Uwsgi Emperor 归档并放入/etc/uwsgi/emperor.ini
.1# /etc/uwsgi/emperor.ini 2[uwsgi] 3emperor = /etc/uwsgi/vassals 4limit-as = 1024 5logto = /var/log/uwsgi/emperor.log 6uid = ubuntu 7gid = www-data
您的应用程序将作为附庸运行。这个
emperor
行在emperor.ini
指定 Emperor 查找Vassal配置文件的目录。也就是说,对于任何Vassal配置文件.ini
文件)显示在/etc/uwsgi/vassals
, the Emperor will attempt to start and manage that vassal.召唤Uwsgi Emperor 。
$ cd /etc/uwsgi $ sudo uwsgi --ini emperor.ini
因为我们指定了
logto
在里面emperor.ini
,成功启动将只显示此输出:$ sudo uwsgi --ini emperor.ini [uWSGI] getting INI configuration from emperor.ini
在一个新的终端窗口中,开始跟踪 Emperor 的日志。
$ sudo tail -f /var/log/uwsgi/emperor.log
确认您在 Emperor 的输出中看到这一行:
*** starting uWSGI Emperor ***
保持此窗口打开,以便在接下来的步骤中在 Emperor 日志中看到新条目。
From the vassals directory, create a symbolic link that points to your applications's
production.ini
.$ cd /etc/uwsgi/vassals $ sudo ln -s ~/myproject/production.ini
一旦您创建了这个符号链接,您就会在 Emperor 日志中看到如下的流量:
[uWSGI] getting INI configuration from production.ini Sun Jul 15 13:34:15 2018 - [emperor] vassal production.ini has been spawned Sun Jul 15 13:34:15 2018 - [emperor] vassal production.ini is ready to accept requests
尾随附庸的日志以确保它正确启动。
$ tail -f /var/log/uwsgi/myproject.log
与此类似的一行表示成功:
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x563aa0193bf0 pid: 14984 (default app)
通过nginx验证您的附庸是否可用。与第1部分一样,您可以通过在浏览器中打开http://localhost或在终端窗口中卷曲localhost来实现这一点。
$ curl localhost
停止Uwsgi Emperor ,因为现在我们将通过systemd启动它。
通过系统运行 Emperor¶
使用以下代码为 Emperor 创建一个systemd单元文件,并将其放入
/lib/systemd/system/emperor.uwsgi.service
.1# /lib/systemd/system/emperor.uwsgi.service 2[Unit] 3Description=uWSGI Emperor 4After=syslog.target 5 6[Service] 7ExecStart=/usr/bin/uwsgi --ini /etc/uwsgi/emperor.ini 8# Requires systemd version 211 or newer 9RuntimeDirectory=uwsgi 10Restart=always 11KillSignal=SIGQUIT 12Type=notify 13StandardError=syslog 14NotifyAccess=all 15 16[Install] 17WantedBy=multi-user.target
Start and enable the systemd unit.
$ sudo systemctl start emperor.uwsgi.service $ sudo systemctl enable emperor.uwsgi.service
验证uwsgi Emperor 正在运行,并且您的应用程序正在本地主机上运行和可用。以下是一些可用于验证的命令:
1$ sudo journalctl -u emperor.uwsgi.service # System logs for emperor 2 3$ tail -f /var/log/nginx/access.log /var/log/nginx/error.log 4 5$ tail -f /var/log/uwsgi/myproject.log 6 7$ sudo tail -f /var/log/uwsgi/emperor.log
确认当您重新启动机器时, Emperor 启动。
$ sudo reboot
重新启动后:
$ curl localhost
祝贺你!您刚刚以健壮的方式部署了应用程序。
uWSGI has many knobs and a great variety of deployment modes. This is just one representation of how you might use it to serve up a cookiecutter Pyramid application. See the uWSGI documentation 有关更深入的配置信息。
此教程是从原始教程修改而来的 Running a Pyramid Application under mod_wsgi .