Skip to content
Alexia Montavon edited this page Apr 14, 2022 · 5 revisions

⚠️ If you aren't using the server provided by the HE-Arc, you might need to change thing. Good luck ! ⚠️

How to create a runit service for daphne

  1. Install daphne via your package manager
sudo apt-get install daphne
  1. Create a run service in /etc/service
sudo mkdir /etc/service/daphne
sudo touch /etc/service/daphne/run
  1. Add the content below in the run file, you might need to change thing in it 😉
#!/bin/bash

set -e

DJANGO_NAME="sousmot"
PROJET_NAME="SousMot"

LOG_FILE="/home/poweruser/www/logs/daphne.log"
SOCKET_FILE="/tmp/daphne.sock"

ENV_FILE="/etc/container_environment"
COMMAND="/var/www/$PROJET_NAME/shared/env/bin/daphne -u $SOCKET_FILE $DJANGO_NAME.asgi:application --access-log $LOG_FILE"

cd /var/www/$PROJET_NAME/current/$DJANGO_NAME
source /var/www/$PROJET_NAME/shared/env/bin/activate

exec chpst -e $ENV_FILE $COMMAND
  1. Chmod the script so it can be executed
sudo chmod +x /etc/service/daphne/run
  1. Start the service
sudo sv restart daphne
  1. Voilà ! It should works now

Notes

  • You might need to run daphne in the folder and virtual env at least one to create the daphne in the env folder (I don't know, maybe you can skip this)
  • There is also a daphne in /usr/bin but I saw someone having problem using it with virtualenv...

See also

Configure Nginx to work with daphne

For nginx to work with daphne, it's really simple ! You just need to change the configuration 😃

Specifically, two things :

  1. Create an upstream for the daphne socket
  2. Change the proxy_pass setting to point to the daphne upstream

You can see the full nginx configuration file below :

upstream uwsgi {
    server unix:/tmp/uwsgi.sock fail_timeout=0;
}

# Add this upstream 
upstream daphne {
    server unix:/tmp/daphne.sock fail_timeout=0;
    #server 127.0.0.1:8001 fail_timeout=0;
}


server {
    listen 80;
    server_name _;

    client_max_body_size 4G;

    root /var/www/app/public;

    access_log /var/www/logs/access.log;
    error_log /var/www/logs/error.log;

    index index.html index.htm;

    # WebSocket
    location /ws {
        #proxy_pass http://uwsgi;
        proxy_pass http://daphne;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        # WebSocket proxy.
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        #proxy_set_header Connection $http_connection;
        proxy_set_header Connection "upgrade";
    }

    location / {
        try_files $uri @wsgi;
    }

    location @wsgi {
        uwsgi_pass uwsgi;
        include uwsgi_params;
    }
}

Don't forget to reload the nginx server 😉

sudo sv reload nginx
Clone this wiki locally