部署 Django 频道:如何在 Web 服务器上退出 shell 后保留 Daphne 运行

Deploying Django Channels: how to keep Daphne running after exiting shell on web server

作为练习,我正在尝试在 DigitalOcean Ubuntu 16.04.4 上使用 Django Channels 2.1.1 部署 Andrew Godwin 的 multichat example。但是,我不知道如何在不停止 Channels 的 Daphne 服务器的情况下退出 Ubuntu 服务器。

现在,我对部署的全部熟悉程度几乎都来自于此 tutorial, and that's how I've deployed the site. But according to Channels' documentation,我必须 运行 生产中的这三个之一才能启动 Daphne:

所以,除了遵循 DigitalOcean 教程外,我还 运行 这些。第三个对我和站点 运行 都很有效。但是,如果我在 Ubuntu 退出 shell,Daphne 也会停止。

教程让 gunicorn 访问了一个 sock 文件 (--bind unix:/home/sammy/myproject/myproject.sock),在我目前的研究中,我在 2018 年之前发布的一些网站上看到不知何故在某处生成了一个 daphne.sock 文件.所以,我想 Channels 的部署方式类似吗?但是我还没有看到关于这是如何完成的任何细节。

如何部署多聊天示例,以便我可以在 Daphne 不停止的情况下退出 Ubuntu Web 服务器?

2018 年 5 月 6 日,欧洲中部时间晚上 8 点更新:

我在下面尝试了 kagronick 的解决方案并在 /etc/systemd/system/daphne_seb.service:

创建了一个 systemd 文件
[Unit]
Description=daphne daemon for seb
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/seb/seb
ExecStart=/home/seb/env_seb/bin/python /home/seb/seb/manage.py daphne -b 0.0.0.0 -p 8001 multichat.asgi:application
Restart=on-failure

[Install]
WantedBy=multi-user.target

我做了 systemctl daemon-reloadsystemctl start daphne_seb.service 并且 运行 几秒钟。然后,systemctl status daphne_seb.service表示Unknown command: 'daphne'

我试过重新启动它。然后我重新启动了 OS。现在 status 说:

daphne_seb.service - daphne daemon for seb
   Loaded: loaded (/etc/systemd/system/daphne_seb.service; enabled; vendor preset: enabled)
   Active: inactive (dead) (Result: exit-code) since Sun 2018-05-06 19:33:31 UTC; 1s ago
  Process: 2459 ExecStart=/home/seb/env_seb/bin/python /home/seb/seb/manage.py daphne -b 0.0.0.0 -p 8001 multichat.asgi:application (code=exited, status=1
 Main PID: 2459 (code=exited, status=1/FAILURE)

May 06 19:33:31 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: daphne_seb.service: Main process exited, code=exited, status=1/FAILURE
May 06 19:33:31 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: daphne_seb.service: Unit entered failed state.
May 06 19:33:31 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: daphne_seb.service: Failed with result 'exit-code'.
May 06 19:33:31 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: daphne_seb.service: Service hold-off time over, scheduling restart.
May 06 19:33:31 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: Stopped daphne daemon for seb.
May 06 19:33:31 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: daphne_seb.service: Start request repeated too quickly.
May 06 19:33:31 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: Failed to start daphne daemon for seb.

我检查了文件路径。他们是对的。我还尝试添加 Environment=DJANGO_SETTINGS_MODULE=multichat.settings,我看到了 here。但这也无济于事。

欧洲中部时间 2018 年 5 月 6 日晚上 10 点更新:

然后,我读到 here 10 秒内只允许重新启动 5 次。所以我删除了 Restart=on-failure 来自己启动服务。

这让我回到了 Unknown command: 'daphne'。这个 solution 向我建议我不应该指向 Python 而应该指向我的 virtualenv 中的 Daphne,所以我更改了它:ExecStart=/home/seb/env_seb/bin/daphne。我还根据相同的解决方案删除了​​ /home/seb/seb/manage.py。这给了我一个新问题:

daphne_seb.service - daphne daemon for seb
   Loaded: loaded (/etc/systemd/system/daphne_seb.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2018-05-06 19:55:24 UTC; 5s ago
  Process: 2903 ExecStart=/home/seb/env_seb/bin/daphne daphne -b 0.0.0.0 -p 8001 multichat.asgi:application (code=exited, status=2)
 Main PID: 2903 (code=exited, status=2)

May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 daphne[2903]:               [-v VERBOSITY] [-t HTTP_TIMEOUT] [--access-log ACCESS_LOG]
May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 daphne[2903]:               [--ping-interval PING_INTERVAL] [--ping-timeout PING_TIMEOUT]
May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 daphne[2903]:               [--application-close-timeout APPLICATION_CLOSE_TIMEOUT]
May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 daphne[2903]:               [--ws-protocol [WS_PROTOCOLS [WS_PROTOCOLS ...]]]
May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 daphne[2903]:               [--root-path ROOT_PATH] [--proxy-headers]
May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 daphne[2903]:               application
May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 daphne[2903]: daphne: error: unrecognized arguments: multichat.asgi:application
May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: daphne_seb.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: daphne_seb.service: Unit entered failed state.
May 06 19:55:24 ubuntu-s-1vcpu-1gb-ams3-01 systemd[1]: daphne_seb.service: Failed with result 'exit-code'.

我检查了我的 multichat.asgi:application,它在正确的位置。所以我不明白为什么它说 error: unrecognized arguments: multichat.asgi:application.

我使用 systemd。您可以在 /etc/systemd/system/daphne.service

中设置一个单元文件

内容如下:

[Unit]
Description=My Daphne Service
After=network.target

[Service]
Type=simple
User=wwwrunn
WorkingDirectory=/srv/myapp
ExecStart=/path/to/my/virtualenv/bin/python /path/to/daphne -b 0.0.0.0 -p 8001 myproject.asgi:application
Restart=on-failure

[Install]
WantedBy=multi-user.target

我的应用程序的所有部分都是这样配置的。它允许他们在崩溃时恢复。 Systemd 可以处理所有日志记录。它们都以正确的顺序启动。使用 Type=simple 这样您就不需要对 PID 文件进行任何分叉或写入。它只会管理一个过程。我运行其中一些来分散负载。

放置此文件后,您将 运行

systemctl daemon-reload
systemctl start daphne.service

其实我的问题和这个. Questions like that usually get sent to this solution是一样的。一开始我没有意识到这个问题和我的问题一样,因为 (i.) 我还不熟悉使用 Channels 部署的词汇,并且 (ii.) 许多这些问题及其解决方案都提到了已经存在的事情已弃用频道 2。我丢失了页面,但是,例如,频道 2 不需要我们做 python manage.py runworker

还有upstart和systemd的问题。一些解决方案使用 upstart 脚本。但是,我有一种模糊的感觉,也许如果我在 Ubuntu 16.04.4 上,我应该使用 systemd。我做了一个 google,发现 systemd 确实取代了 upstart,根据这样的文章 one

@kagronick 提供了正确的 systemd 解决方案,但我必须对其进行编辑才能使其适用于我:

# /etc/systemd/system/daphne_seb.service

[Unit]
Description=daphne daemon for seb
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/seb/seb
ExecStart=/home/seb/env_seb/bin/daphne -b 0.0.0.0 -p 8001 multichat.asgi:application

# I turned this off for testing purposes.
# Still not sure if should use 'on-failure' or 'always'. 
# Restart=on-failure

[Install]
WantedBy=multi-user.target

然后在shell:

systemctl daemon-reload
systemctl start daphne_seb.service