Django: Difference between using server through manage.py and other servers like gunicorn etc. Which is better?
Asked Answered
D

2

42

I have been running my beginner Django projects with manage.py runserver. I see suggestions to use gunicorn instead. What is the difference ?

Detect answered 26/2, 2016 at 16:49 Comment(2)
This is something you shouldn't put a lot of thought into now since you are very new. Get your project going then when you are ready to push it to live then focus on this. You can use Heroku and this would still allow you to easily deploy using your manage.py file. Best of luck to you.Basion
There's a huge warning in the docs saying don't use runserver for productionJacobsohn
S
97

nginx and gunicorn are probably the most popular configuration for production deployments. Before detailing why gunicorn is recommended over runserver, let's quickly clarify the difference between nginx and gunicorn, because both state that they are web servers.

NGINX should be your entrance point to the public, it's the server listening to port 80 (http) and 443 (https). Its main purpose is handling HTTP requests, that is applying redirects, HTTP Auth if required, managing TSL/SSL Certificates and - among other things - decides where your requests is finally going to. E.g. there maybe a node.js app living on localhost:3000 that awaits requests on/foo/api while gunicorn is waiting at localhost:8000 to serve your awesome app. This functionality of proxying incoming requests to so called upstream services (in this case node.js and gunicorn) is called reverse-proxy.

GUNICORN is a server that translates HTTP requests into Python. WSGI is one of the interfaces/implementations that does that (e.g., the text parts of http headers are transformed into key-value dicts).

Django's built-in development web server (what you get when you run manage.py runserver) provides that functionality also, but it targets a development environment (e.g., restart when the code changes), whereas Gunicorn targets production.

Gunicorn has many features that Django's built-in server is lacking:

  • gunicorn can spawn multiple worker processes to parallelize incoming requests to multiple CPU cores
  • gunicorn has better logging
  • gunicorn is generally optimized for speed
  • gunicorn can be configured to fine grades depending on your setup
  • gunicorn is actively designed and maintained with security in mind

There are web servers other than gunicorn, but gunicorn (inspired by ruby's unicorn) is very popular and easy to setup, and hence is not only a good starting point, but a professional solution that is used by large projects.

Spector answered 26/2, 2016 at 20:0 Comment(4)
You should use runserver for development and gunicorn for production, that's unrelated to your skill level.Spector
Offer some references/confirmation, e.g. on 'Gunicorn has many features that Django's built-in server is lacking'.Odawa
I use Gunicorn and Uvicorn for production and runserver for dev, but if I am using NGINX to serve the app to the public and then run manage.py runserver locally on the server which is then picked up by NGINX, is that still wrong (and I should keep gunicorn) or does it not matter since NGINX is handling the requests?Hanukkah
It is still wrong. Both are handling requests, just in different stages and ways and runserver should never be used in ProductionSpector
M
8

manage.py runserver is only a development server, it is not meant for production under any circumstance. You need to use something like Apache, uWSGI, NGINX or some other server to serve your django project once it's ready for deployment.

Marketa answered 26/2, 2016 at 17:4 Comment(1)
It would be nice to see more detailed answer with details whyGermaun

© 2022 - 2024 — McMap. All rights reserved.