As practice, I'm trying to deploy Andrew Godwin's multichat example with Django Channels 2.1.1 on DigitalOcean Ubuntu 16.04.4. However, I don't know how to exit the Ubuntu server without Channels' Daphne server stopping.
Right now, almost my entire familiarity with deployment comes from this tutorial, and that's how I've deployed the site. But according to Channels' documentation, I have to run one of these three in production to start Daphne:
- daphne myproject.asgi:application
- daphne -p 8001 myproject.asgi:application
- daphne -b 0.0.0.0 -p 8001 myproject.asgi:application
So, in addition to following the DigitalOcean tutorial, I ran these also. The third one worked for me and the site ran well. However, if I exit shell on Ubuntu, Daphne stops too.
The tutorial has gunicorn access a sock file (--bind unix:/home/sammy/myproject/myproject.sock
), and in my research so far I've seen on a few sites published before 2018 that somehow somewhere a daphne.sock file is generated. So, I guess Channels deploys similarly? But I haven't seen any details about how this is done.
How do I deploy the multichat example so I can exit the Ubuntu web server without Daphne stopping?
Update 6 May 2018, 8pm CET:
I tried kagronick's solution below and created a systemd file at /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/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
I did systemctl daemon-reload
and systemctl start daphne_seb.service
and it ran for a couple of seconds. Then, systemctl status daphne_seb.service
said Unknown command: 'daphne'
.
I tried restarting it. Then I rebooted the OS. Now status
says:
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.
I checked the filepaths. They're correct. I also tried adding Environment=DJANGO_SETTINGS_MODULE=multichat.settings
, which I saw here. But that didn't help either.
Update 6 May 2018, 10pm CET:
Then, I read here that only 5 restarts are allowed within a 10-second period. So I removed Restart=on-failure
to start the service myself.
This brought me back to Unknown command: 'daphne'
. This solution suggested to me that I shouldn't be pointing to Python but to Daphne in my virtualenv, so I changed it: ExecStart=/home/seb/env_seb/bin/daphne
. I also removed /home/seb/seb/manage.py
based on the same solution. This gave me a new problem:
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'.
I check my multichat.asgi:application
and it's in the right place. So I can't figure out why it says error: unrecognized arguments: multichat.asgi:application
.