title: Deploying Django Channels: how to keep Daphne running after exiting shell on web server
body: <p>As practice, I'm trying to deploy Andrew Godwin's <a href="https://github.com/andrewgodwin/channels-examples/tree/master/multichat" rel="noreferrer">multichat example</a> 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.</p>
  
  <p>Right now, almost my entire familiarity with deployment comes from this <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04" rel="noreferrer">tutorial</a>, and that's how I've deployed the site. But according to Channels' <a href="http://channels.readthedocs.io/en/latest/deploying.html" rel="noreferrer">documentation</a>, I have to run one of these three in production to start Daphne:</p>
  
  <ul>
  <li>daphne myproject.asgi:application</li>
  <li>daphne -p 8001 myproject.asgi:application</li>
  <li>daphne -b 0.0.0.0 -p 8001 myproject.asgi:application</li>
  </ul>
  
  <p>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.</p>
  
  <p>The tutorial has gunicorn access a sock file (<code>--bind unix:/home/sammy/myproject/myproject.sock</code>), 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.</p>
  
  <p>How do I deploy the multichat example so I can exit the Ubuntu web server without Daphne stopping?</p>
  
  <p><em>Update 6 May 2018, 8pm CET:</em></p>
  
  <p>I tried kagronick's solution below and created a systemd file at /etc/systemd/system/daphne_seb.service:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>I did <code>systemctl daemon-reload</code> and <code>systemctl start daphne_seb.service</code> and it ran for a couple of seconds. Then, <code>systemctl status daphne_seb.service</code> said <code>Unknown command: 'daphne'</code>.</p>
  
  <p>I tried restarting it. Then I rebooted the OS. Now <code>status</code> says:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>I checked the filepaths. They're correct. I also tried adding <code>Environment=DJANGO_SETTINGS_MODULE=multichat.settings</code>, which I saw <a href="https://stackoverflow.com/questions/46655488/running-django-channels-with-daphne-on-systemd">here</a>. But that didn't help either.</p>
  
  <p><em>Update 6 May 2018, 10pm CET:</em></p>
  
  <p>Then, I read <a href="https://serverfault.com/questions/845471/service-start-request-repeated-too-quickly-refusing-to-start-limit">here</a> that only 5 restarts are allowed within a 10-second period. So I removed <code>Restart=on-failure</code> to start the service myself.</p>
  
  <p>This brought me back to <code>Unknown command: 'daphne'</code>. This <a href="https://stackoverflow.com/questions/46655488/running-django-channels-with-daphne-on-systemd">solution</a> suggested to me that I shouldn't be pointing to Python but to Daphne in my virtualenv, so I changed it: <code>ExecStart=/home/seb/env_seb/bin/daphne</code>. I also removed <code>/home/seb/seb/manage.py</code> based on the same solution. This gave me a new problem:</p>
  
  <pre><code>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'.
  </code></pre>
  
  <p>I check my <code>multichat.asgi:application</code> and it's in the right place. So I can't figure out why it says <code>error: unrecognized arguments: multichat.asgi:application</code>.</p>