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

13,335

Solution 1

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.

Solution 2

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.

Share:
13,335
Utkarsh Sinha
Author by

Utkarsh Sinha

I am a coder, entrepreneur and science enthusiast. Currently I am building a new age technology consulting business - Betaflux, with a team of highly motivated and skilled developers, and aim to help businesses integrate the right set of technologies to achieve business goals. I also co-founded Clerro, in 2016, to build a dedicated platform for high quality written content on intellectual topics. Our team learnt and massively skilled up both in web-technologies and in business during the 2 years of running Clerro, at one point even interviewing with YCombinator in San Francisco. I also love good food, watching documentaries and dreaming about the future over coffee.

Updated on June 05, 2022

Comments

  • Utkarsh Sinha
    Utkarsh Sinha almost 2 years

    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.
  • Utkarsh Sinha
    Utkarsh Sinha about 8 years
    Well that makes sense. I am currently trying to figure out how to configure Daphne. Any resources around that might be really welcome. As I can see the server when started says "Starting server at 127.0.0.1:8000, channel layer MyProject.asgi:channel_layer". Since its listening on port-8000, my http requests to the server are being denied.
  • Lukasa
    Lukasa about 8 years
    You can change the way you invoke daphne. daphne takes a -p command-line argument that controls what port it binds to.
  • Aaron McMillin
    Aaron McMillin over 7 years
    Probably you don't want Daphne service static assets though, wouldn't you still want apache or nginx sitting in front for that? Do they need additional configuration to proxy the websocket traffic?
  • Lukasa
    Lukasa over 7 years
    Daphne can serve static assets just fine. It's a Twisted web server which is totally suitable for that. However, if you'd rather use an nginx or Apache that's totally fine: just configure them in their ordinary reverse proxy form, the same way you would if you were running them in front of a gunicorn or uwsgi application.