0

Hi there previously I have a django uwsgi app deployed on a ubuntu server. now new features added: channels, daphne. hence we need to deploy the new project using

  • nginx to serve static files
  • daphne to serve dynamic django views
  • supervisord to manage all nginx and daphne

daphne.conf under /etc/supervisor/conf.d from django official site

[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
# socket=tcp://localhost:8001
socket=tcp://0.0.0.0:8001

# Directory where your site's project files are located
directory=/var/www/example/example
# activate virtual environment
environment=PATH="/var/www/example/bin:%(ENV_PATH)s"

# 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=daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers example.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=/var/www/example/example/asgi.log
redirect_stderr=true

nginx.conf under \etc\nginx

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream channels-backend {
        server localhost:8001;  # Change Daphne's port to 8001
    }

    server {
        listen          8000;
        server_name     www.example.com example.com;
        charset         utf-8;

        location /static {
            alias       /var/www/example/example/static;
        }

        location /media {
            alias       /var/www/example/example/media;
        }

        # For all other requests, pass to Daphne
        location / {
            try_files $uri @proxy_to_app;
        }

        location @proxy_to_app {
            proxy_pass http://channels-backend;

            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;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    tcp_nopush on;
    tcp_nodelay on;
    types_hash_max_size 2048;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    
    gzip on;
    
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
    include servers/*;
}

I intended to use port 8000 for nginx and 8001 for daphne, is there any thing wrong with the configs above?

everytime I access example.com all I see is the nginx welcome page. I have collectstatic, and when I access a static or media file that works on localmachine, it reports 404

I successfully started supervisord by supervisord -c /etc/supervisor/supervisord.conf

I have verified:

  1. no firewall blocking port 8000 or 8001
  2. no duplicate processes running on 8000 or 8001
  3. no error logs in nginx error logging records

there is mainly one thing I worry about in my daphne config: I have to enter the venv manually, so that I have daphne cli access, otherwise daphne is not installed globally with apt, hence I added the line environment=PATH="/var/www/example/bin:%(ENV_PATH)s" gpt told me to, but I am not certain it is the correct way of let supervisord nows where to find daphne.


in addition, redis is needed for this project as well, but I reckon I need got app running first, then fix the redis and docker deployment, I am not sure I am on the right track, I will be more than glad if u give me something to look at.

Weilory
  • 2,621
  • 19
  • 35

0 Answers0