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

部署Django通道:如何在web服务器上退出shell后保持Daphne运行,django,django-channels,django-deployment,Django,Django Channels,Django Deployment,作为实践,我尝试在DigitalOcean Ubuntu 16.04.4上部署Andrew Godwin的Django Channel 2.1.1。然而,我不知道如何在没有频道Daphne服务器停止的情况下退出Ubuntu服务器 现在,我对部署的全部熟悉都来自于此,这就是我部署站点的方式。但据《频道》报道,我必须在生产中运行这三个中的一个来启动达芙妮: daphne myproject.asgi:应用程序 daphne-P8001 myproject.asgi:应用程序 daphne-b 0.

作为实践,我尝试在DigitalOcean Ubuntu 16.04.4上部署Andrew Godwin的Django Channel 2.1.1。然而,我不知道如何在没有频道Daphne服务器停止的情况下退出Ubuntu服务器

现在,我对部署的全部熟悉都来自于此,这就是我部署站点的方式。但据《频道》报道,我必须在生产中运行这三个中的一个来启动达芙妮:

  • daphne myproject.asgi:应用程序
  • daphne-P8001 myproject.asgi:应用程序
  • daphne-b 0.0.0.0-p 8001 myproject.asgi:应用程序
所以,除了遵循DigitalOcean教程,我还运行了这些。第三个适合我,网站运行良好。然而,如果我在Ubuntu上退出shell,Daphne也会停止

本教程让gunicorn访问一个sock文件(
--bind unix:/home/sammy/myproject/myproject.sock
),在我的研究中,到目前为止,我在2018年之前发布的一些网站上看到,不知怎的,在某个地方生成了一个daphne.sock文件。那么,我猜通道的部署也差不多?但我还没有看到关于如何做到这一点的任何细节

如何部署multichat示例,以便在不停止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守护进程重新加载
systemctl启动daphne_seb.service
,它运行了几秒钟。然后,
systemctl status daphne_seb.service
未知命令:“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.
我检查了文件路径。他们是对的。我还尝试添加我看到的
Environment=DJANGO\u SETTINGS\u MODULE=multichat.SETTINGS
。但这也没用

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

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

这让我回到了
未知命令:“daphne”
。这向我建议,我不应该指向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
,发现它在正确的位置。所以我不明白为什么它会说
错误:无法识别的参数: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

我的问题其实与此相同。像这样的问题通常会被发送到这里。一开始我没有意识到这个问题与我的问题是一样的,因为(I.)我还不熟悉渠道部署的词汇表,(ii.)这些问题及其解决方案中有很多提到了渠道2已经不推荐的内容。我丢失了页面,但是,例如,通道2不要求我们执行
python manage.py runworker

还有一个问题是upstart和systemd。有几种解决方案使用upstart脚本。但是,我有一种模糊的感觉,也许如果我在Ubuntu 16.04.4上,我应该使用systemd。根据类似的文章,我在谷歌上搜索了一下,发现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
然后在shell中:

systemctl daemon-reload
systemctl start daphne_seb.service

嘿,谢谢你的解决方案<代码>系统CTL状态达芙妮。服务显示“未知命令:'达芙妮'”。我检查了我的文件路径,它们是正确的。知道怎么回事吗?我已经更新了我的问题。现在它停止了,因为
启动请求重复得太快。
我解决了这个问题,部分要感谢您的帮助。晚饭后我会写出一份完整的解决方案。非常感谢。使其在系统启动时自动启动
sudo systemctl enable daphne.service