简体   繁体   中英

How can i run Django channels on production?

I deployed a simple Django app to a VPS, here is my environment:

virtualenv
gunicorn
nginx
systemd

Everything works perfectly, i can see my template loading. I also added a small Django Channels feature, but that part doesn't work; so while i can use it on WSGI without any problem, if i try to reach a Consumer, i will get an error. So my question is: how can i run Channels too in production?

Here is what i currently did:

/etc/nginx/sites-available/myproject

server {
        listen 80;
        server_name 54.39.20.155;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /WaitingRoomVenv/WaitingRoom/WaitingRoom/static;
        }

        location / {
            include proxy_params;
            proxy_pass http://unix:/WaitingRoomVenv/WaitingRoomEnv.sock;
        }
    }

/etc/systemd/system/gunicorn.service

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

[Service]
User=root
Group=www-data
WorkingDirectory=/WaitingRoomVenv/WaitingRoom
ExecStart=/WaitingRoomVenv/WaitingRoomEnv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/WaitingRoomVenv/WaitingRoomEnv.sock WR.wsgi:application

[Install]
WantedBy=multi-user.target

To start gunicorn: sudo systemctl start gunicorn To start nginx: sudo systemctl restart nginx

To access channesl you need to run through a ASGI server like daphne (that ships with channels) instead of WSGI geunicorn you are using, see:

https://channels.readthedocs.io/en/latest/deploying.html

to start the server daphne -p 8001 myproject.asgi:application then in your nginx you need to proxy pass to port 8001

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM