Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static files not working when not in DEBUG=True mode #6

Open
timurjavid opened this issue Nov 6, 2019 · 11 comments
Open

Static files not working when not in DEBUG=True mode #6

timurjavid opened this issue Nov 6, 2019 · 11 comments
Assignees
Labels
bug Something isn't working

Comments

@timurjavid
Copy link
Contributor

We need to figure out how to serve static files correctly in production. Running in DEBUG=True mode is insecure. Might have to tweak views.py and settings.py.

@lpulley lpulley added the bug Something isn't working label Nov 6, 2019
@mgale456
Copy link
Member

mgale456 commented Nov 7, 2019

Only serving static files while DEBUG=true seems to be an intended feature. Once we start thinking about moving to production it should be no big deal to put the static files in the correct directory to be served by the server, and not through Django.

Django's built in testserver is for convenient development testing, and not intended for deployment.

I'll leave this issue open, since it is still an unsolved issue, but not an urgent issue

@lpulley
Copy link
Member

lpulley commented Nov 7, 2019

I'm not sure what that means precisely. What will be serving the files in this case? You mean like a static file server in Nginx or similar? I could whip that together really, really quickly. Let's aim to get this solved by the end of Saturday's meeting, at the latest.

@mgale456
Copy link
Member

mgale456 commented Nov 7, 2019

Yes. According to the Django tutorial and Django's deployment documentation, Django is run in production through Apache, Nginx, or your favorite webserver compatible with WSGI. The built in server we're running is for development and testing and is not secure or efficient.

It is important to note that Django Channels necessitates a server compatible with ASGI, and there are a lot fewer options. Primarily Daphne which is developed specifically for Channels.

@lpulley
Copy link
Member

lpulley commented Nov 7, 2019

Interesting... I'll look into Daphne.

@mgale456
Copy link
Member

Reviving this Issue. Very relevant.

I have made a deployment branch. Daphne works, and handles the HTTP and Websocket connections, however it does not serve any static files (css, icons, etc.). This is by design for Daphne, not a bug.

We need to set up something else like nginx to serve those, while Daphne handles the Django requests. More details are available, as well as an example at the bottom that doesn't use Docker on the Channels Docs for Deployment.

Other notes:
Django has a command collectstatic that copies all of the static files into a single directory from all of the apps and such. It is configured at the bottom of settings.py to put everything in cogs/django/nginx/ on the host.

@lpulley
Copy link
Member

lpulley commented Feb 29, 2020

That sounds not too hard. I'm busy the next few days but I could do this.

@lpulley
Copy link
Member

lpulley commented Jul 16, 2020

Hello again 👋

I've got a lot of experience with this now. I'm confident I can get this sorted out. Drop Daphne, grab Uvicorn and Gunicorn and set the container command to gunicorn example.asgi:application --bind 0.0.0.0:8000 -w 4 -k uvicorn.workers.UvicornWorker. For automatic code reloading in development, override that command (in docker-compose.yml) to uvicorn example.asgi:application --host 0.0.0.0 --reload (doesn't do multiple workers, but does do code reloading).

When I have time to work on this, I can take care of this.

@mgale456
Copy link
Member

mgale456 commented Jul 16, 2020 via email

@mgale456
Copy link
Member

Another note supporting Uvicorn/Gunicorn. If they work identically easily in dev (with reloading) and production, that is awesome. It is a bit annoying to switch to daphne, and then sometimes not get the same results if you're not serving the files quite right in nginx and there are some edge cases my nginx implementation does not cover that I wouldn't expect others to know about so much.

@lpulley
Copy link
Member

lpulley commented Jul 21, 2020

For dev, just change the command in compose. The Dockerfile command should be gunicorn (which doesn't really do good reloading, but does do workers) and then dev should use plain uvicorn (which does do good reloading)

@lpulley
Copy link
Member

lpulley commented Jul 21, 2020

I have a whole setup for this that I learned from research. It's very nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants