Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy django-channels with Apache2 and Daphne

I'm trying to learn to use django-channels and have worked through both the tutorial and this multichat example. I am now trying to deploy it on a Digital Ocean droplet using Apache and Daphne. I would happily use Daphne by itself but I do not understand how to.

So this is my Apache conf file:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName multichat.mysite.co.uk
    ServerAlias www.multichat.mysite.co.uk
    DocumentRoot /var/www/multichat
    WSGIDaemonProcess multichat python-path=/var/www/multichat python-home=/var/www/multichat/env
    WSGIProcessGroup multichat
    WSGIScriptAlias / /var/www/multichat/multichat/wsgi.py

    Alias /robots.txt /var/www/multichat/static/robots.txt
    Alias /favicon.ico /var/www/multichat/static/favicon.ico

    Alias /media/ /var/www/multichat/media/
    Alias /static/ /var/www/multichat/static/

    <Directory /var/www/multichat/static>
        Require all granted
    </Directory>

    <Directory /var/www/multichat/media>
        Require all granted
    </Directory>

    WSGIScriptAlias / /var/www/multichat/multichat/wsgi.py

    <Directory /var/www/multichat/multichat>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

I've installed Redis and have it up and running.

I've included this file in /etc/systemd/system/daphne.service

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

[Service]
User=root
Group=www-data
WorkingDirectory=/var/www/multichat/multichat
ExecStart=/var/www/multichat/env/bin/daphne -b 0.0.0.0 -p 8001 multichat.asgi:application

# Not sure if should use 'on-failure' or 'always'. 
Restart=on-failure

[Install]
WantedBy=multi-user.target

Although the webpage comes up and I can login etc, when it comes to a chatroom I have the following error in console:

WebSocket connection to 'ws://multichat.mysite.co.uk/chat/stream/' failed: Error during WebSocket handshake: Unexpected response code: 404

I'm clearly not setting up something correctly but I don't know where to turn. I would happily scrape Apache if I can get a pointer on how to use just Daphne, but I've tried and got nowhere with that either

like image 739
HenryM Avatar asked Feb 21 '19 14:02

HenryM


1 Answers

You've configured Apache to serve Django content using WSGI protocol, but WSGI doesn't support web sockets. That is why Daphne is here. It doesn't use WSGI to serve Django content, so you can use it with web sockets.

To use Daphne instead, you should remove WSGI settings from apache file and put ProxyPass instead, which should point to your daphne server. The proper line should look like this:

ProxyPass http://127.0.0.1:8001/

As your daphne server is running on the same server, but on port 8001.

like image 181
GwynBleidD Avatar answered Sep 28 '22 08:09

GwynBleidD