1

as the title indicates I am using django-channels with daphne in the production server but when I show the status of daphne.service it says 8001 is already in use.

The interesting thing is that the socket connection is working perfectly with the frontend and easily exchanging messages.

here is my configurations

# /etc/systemd/system/daphne_seb.service

[Unit]
Description=daphne daemon
After=network.target

[Service]
User=simple
Group=www-data
WorkingDirectory=/home/simple/my_proj
ExecStart=/home/simple/my_proj/venv/bin/daphne -b 0.0.0.0 -p 8001 project.asgi:application
Restart=on-failure

[Install]
WantedBy=multi-user.target

daphne asgi in supervisor

# /etc/supervisor/conf.d/daphne_proj.conf
[fcgi-program:asgi] 
# TCP socket used by Nginx backend upstream 
socket=tcp://localhost:8001 
 
# Directory where your site's project files are located 
directory=/home/simple/my_proj 
 
# Each process needs to have a separate socket file, so we use process_num 
# Make sure to update "mysite.asgi" to match your project name 
command=/home/simple/my_proj/venv/bin/daphne -u /run/daphne/daphne%(process_num)d.sock --endpoint fd:fileno=0 --access-log - --proxy-headers project.asgi:application 
 
# Number of processes to startup, roughly the number of CPUs you have 
numprocs=4 
 
# Give each process a unique name so they can be told apart 
process_name=asgi%(process_num)d 
 
# Automatically start and recover processes 
autostart=true 
autorestart=true 
 
# Choose where you want your log to go 
stdout_logfile=/home/simple/my_bg/daphne.log 
redirect_stderr=true

finally, my nginx looks like this

upstream websocket {
    server 0.0.0.0:8001;
}

server {
    listen 80;
    server_name MY_SERVER_DOMAIN;
    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/simple/my_proj/myproject.sock;
    }

    location /ws/ {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection “upgrade”;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }

}

here is all I have as I mentioned above everything works but the status of daphne throws the error. For clarification, I am showing the usage of the 8001 port

enter image description here

Any help would be appreciated. Thanks in advance!

coder
  • 441
  • 1
  • 4
  • 19
  • supervisor as i know is outdated, it is recomended to start daphne and gunicorn with systemd, i can suggest this article, https://saurabhgujjar.medium.com/django-channels-with-daphne-gunicorn-and-nginx-on-digitalocean-all-in-one-guide-28625eead962 i configured daphne as it is in article, and it is working – oruchkin Jun 27 '22 at 08:21

0 Answers0