如何在没有 WSGI 的情况下为 Gunicorn 配置 ExecStart?

How to configure ExecStart for Gunicorn without WSGI?

Systemd 和 Gunicorn 需要某种 wsgi 文件作为 ExecStart 的最后一个参数:http://docs.gunicorn.org/en/latest/deploy.html?highlight=ExecStart#systemd

对于 Django,这在主模块中为 wsgi.py:

ExecStart=/home/admin/django/bin/gunicorn --config /home/admin/src/gunicorn.py --bind unix:/tmp/api.sock myapp.wsgi

但是在使用Sanic和uvloop时这个文件显然不存在(我相信新的协议叫做ASGI)。我试着用它代替 app.py 不出所料,它不起作用:

ExecStart=/home/admin/sanic/bin/gunicorn --config /home/admin/src/gunicorn.py --bind unix:/tmp/api.sock myapp.app

使用Sanic时该参数应该如何配置?

我自己还没有用 Systend 和 gunicorn 部署它。但是,documentation 似乎在这方面做得很好。

In order to run Sanic application with Gunicorn, you need to use the special sanic.worker.GunicornWorker for Gunicorn worker-class argument:

gunicorn myapp:app --bind 0.0.0.0:1337 --worker-class sanic.worker.GunicornWorker

考虑到这一点,这个怎么样:

ExecStart=/home/admin/sanic/bin/gunicorn --config /home/admin/src/gunicorn.py myapp:app --bind 0.0.0.0:1337 --worker-class sanic.worker.GunicornWorker

我认为你缺少的最大部分是 GunicornWorker 工人 class。

如果你想用 systemd 启动 sanic,为什么不使用 supervisrod:Supervisord

启动 -> Systemd -> supervisord -> gunicorn -> sanic

[unix_http_server]
file=/tmp/supervisor.sock                       ; path to your socket file

[supervisord]
logfile=/var/log/supervisord/supervisord.log    ; supervisord log file
logfile_maxbytes=50MB                           ; maximum size of logfile before rotation
logfile_backups=10                              ; number of backed up logfiles
loglevel=error                                  ; info, debug, warn, trace
pidfile=/var/run/supervisord.pid                ; pidfile location
nodaemon=false                                  ; run supervisord as a daemon
minfds=1024                                     ; number of startup file descriptors
minprocs=200                                    ; number of process descriptors
user=root                                       ; default user
childlogdir=/var/log/supervisord/               ; where child log files will live

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock         ; use a unix:// URL  for a unix socket

[program:ctrlapi]
directory=/home/ubuntu/api
command=/home/ubuntu/api/venv3/bin/gunicorn api:app --bind 0.0.0.0:8000 --worker-class sanic.worker.GunicornWorker -w 2
stderr_logfile = log/api_stderr.log
stdout_logfile = log/api_stdout.log