How to configure Apache to run ASGI in Django Channels? Is Apache even required?

允我心安 提交于 2020-01-10 07:42:09

问题


I built a django-project and deployed it to production using Apache-WSGI combo. For that I had added the apache2.conf as shown below:

WSGIScriptAlias / /home/ubuntu/MyProject/MyProject/wsgi.py
WSGIPythonPath /home/ubuntu/MyProject

<Directory /home/ubuntu/MyProject/MyProject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

So this mean't all requests to my website first go to Apache, which then allows WSGI to come into play. So if I would switch off Apache, the website would not work.

I have now installed Django-Channels. As per the 'Deploying' section in the documentation (https://channels.readthedocs.io/en/latest/deploying.html), I have:

  1. Installed Redis (on my Django Project server)
  2. Run worker servers
  3. Run Daphne (interface server)
  4. I have stopped Apache at the moment and the website refuses to connect.

回答1:


Currently Apache does not have an ASGI server implementation. That means that you can continue to use Apache, but you will also need Daphne. In essence, Apache will change from being your primary web server to being a reverse proxy.

There is potentially some value in doing that: Python developers have been running nginx in reverse proxy mode for years. However, Daphne is a very capable web server, being built on top of Twisted's web server, and so Apache certainly isn't necessary.

For that moment, then, I recommend running just with Daphne: have Daphne listen on your primary ports and disable Apache altogether. If you find there are features of Apache you still want, you'll need to configure Apache as a reverse proxy: one suggested article for configuring that is this one from Digital Ocean.




回答2:


As mentioned by Lukasa, I stopped the Apache server, which at first stopped my django app getting delivered out to the world. Then I ran the following commands:

sudo daphne MyProject.asgi:channel_layer --port 80 --bind 0.0.0.0 -v2
sudo python manage.py runworker -v2

The two commands started delivering the app to http requests from outside the server. No other configurations were required other than mentioned in the question post.



来源:https://stackoverflow.com/questions/37098504/how-to-configure-apache-to-run-asgi-in-django-channels-is-apache-even-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!