使用Apache和Daphne服务静态文件

时间:2019-02-22 07:55:15

标签: apache django-channels daphne

我正在尝试学习使用Django频道,并且已经完成了本教程和this multichat example的学习。我现在正尝试使用Apache和Daphne将其部署在Digital Ocean Droplet上。

我有2个问题:

  1. 我的静态文件没有被使用(我已运行collectstatic)

  2. 我只能在输入public class YourActivity extends Activity { private Menu menu; private String DateTitle = "Date'"; private String MonthTitle = "Month"; private boolean inBed = false; @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); // Create your menu... this.menu = menu; return true; } private void updateMenuTitles() { MenuItem bedMenuItem = menu.findItem(R.id.action_filter); >//Here i am doing by using string you can check current Fragment so it will change according to Fragment if (inBed) { bedMenuItem.setTitle(MonthTitle); } else { bedMenuItem.setTitle(DateTitle); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_filter: if(item.getTitle().toString() == "Date"){ Toast.makeText(this,"Date",Toast.LENGTH_LONG).show(); } if(item.getTitle().toString() == "Month") Toast.makeText(this,"Month",Toast.LENGTH_LONG).show(); break; default: break; } return true; } } 时才能使Daphne工作 比使用下面的daphne multichat.asgi:application --port 8001 --bind 0.0.0.0 -v2文件

这是我的daphne.service conf文件,我希望该文件可以提供静态文件:

Apache

这是我的<VirtualHost *:80> ServerAdmin webmaster@hexiawebservices.co.uk ServerName multichat.hexiawebservices.co.uk ServerAlias www.multichat.hexiawebservices.co.uk DocumentRoot /var/www/multichat ProxyPreserveHost On ProxyPass / http://0.0.0.0:8001/ ProxyPassReverse / http://0.0.0.0:8001/ 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> <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> 文件

/etc/systemd/system/daphne.service

编辑1

根据要求,这里是[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

routing.py

一个from django.urls import path from channels.http import AsgiHandler from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from chat.consumers import ChatConsumer application = ProtocolTypeRouter({ "websocket": AuthMiddlewareStack( URLRouter([ path("chat/stream/", ChatConsumer), ]), ), })

consumers.py

1 个答案:

答案 0 :(得分:1)

这是为我排序的conf文件:

<VirtualHost *:80>
    ServerAdmin webmaster@hexiawebservices.co.uk
    ServerName multichat.hexiawebservices.co.uk
    ServerAlias www.multichat.hexiawebservices.co.uk
    DocumentRoot /var/www/multichat

    RewriteEngine on
    RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
    RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
    RewriteRule .* ws://0.0.0.0:8001%{REQUEST_URI} [P,QSA,L]

    ProxyPreserveHost On
    ProxyRequests Off
    ProxyPassMatch ^/(ws(/.*)?)$ ws://0.0.0.0:8001/$1
    ProxyPass / http://0.0.0.0:8001/
    ProxyPassReverse / http://0.0.0.0:8001/

    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>

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