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

nus*_*ara 4 django django-deployment django-channels

作为实践,我尝试在 DigitalOcean Ubuntu 16.04.4 上使用 Django Channels 2.1.1部署 Andrew Godwin 的聊天示例。但是,我不知道如何在不停止 Channels 的 Daphne 服务器的情况下退出 Ubuntu 服务器。

现在,几乎我对部署的全部熟悉都来自本教程,这就是我部署站点的方式。但是根据 Channels 的文档,我必须在生产中运行这三个中的一个才能启动 Daphne:

  • 达芙妮 myproject.asgi: 应用程序
  • daphne -p 8001 myproject.asgi:application
  • daphne -b 0.0.0.0 -p 8001 myproject.asgi:application

因此,除了遵循 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
Run Code Online (Sandbox Code Playgroud)

我做到了systemctl daemon-reloadsystemctl start daphne_seb.service它运行了几秒钟。然后,systemctl status daphne_seb.service说道Unknown command: 'daphne'

我尝试重新启动它。然后我重新启动了操作系统。现在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.
Run Code Online (Sandbox Code Playgroud)

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

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

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

这让我回到了Unknown command: 'daphne'. 这个解决方案告诉我,我不应该在我的 virtualenv 中指向 Python 而是指向 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'.
Run Code Online (Sandbox Code Playgroud)

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

kag*_*ick 5

我使用 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
Run Code Online (Sandbox Code Playgroud)

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

放置此文件后,您将运行

systemctl daemon-reload
systemctl start daphne.service
Run Code Online (Sandbox Code Playgroud)

  • 要使其在系统启动时自动启动`sudo systemctl enable daphne.service` (2认同)

nus*_*ara 2

我的问题其实和这个是一样。类似的问题通常会发送到此解决方案。我一开始并没有意识到这个问题与我的问题相同,因为(i.)我还不熟悉通道部署的词汇,并且(ii.)很多这些问题及其解决方案都提到了已经存在的事情已弃用 Channels 2。我丢失了该页面,但是,例如,Channels 2 不需要我们这样做python manage.py runworker

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

@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
Run Code Online (Sandbox Code Playgroud)

然后在外壳中:

systemctl daemon-reload
systemctl start daphne_seb.service
Run Code Online (Sandbox Code Playgroud)