18

I'm going to setup a simple Django app running in a production environment on a Linux box. The app will have very little traffic - less that 100 page loads per day. Is it okay to use the builtin Django webserver for this or should I install Apache and mod_wsgi? If so, what are the reasons for this? Security perhaps?

UPDATE

OK it is clear I shouldn't be using the builtin server. Some of the alternatives to Apache look interesting. Is there one that is more popular/more frequently used with Django perhaps?

paperplane
  • 183
  • 1
  • 1
  • 5

4 Answers4

29

Is it okay to use the builtin Django webserver for this

No.

Should I install Apache and mod_wsgi?

Yes.

If so, what are the reasons for this? Security perhaps?

Partly.

More importantly, the little toy Django server is single-threaded and any hangup in your code hangs the server. This means that when two users click almost at the same time, user one's query must go all the way through Django before user two's query can even starts.

And this will have to include the insanely slow download speed to the desktop.

Apache (like all the alternatives, lighttpd or nginx) is multi-threaded. The slowest part of the transaction is the download from Apache to the desktop. You don't want Python code (and Django) handling this in a single-threaded manner. Even for just a few users.

Also, you don't what Django serving static media (i.e., CSS and JS library files.)

A single slow spot in your application won't effect the overall system throughput if Apache and mod_wsgi are in place. One request 's output page can be slowly downloading to a PC desktop in parallel with another user's output.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
22

DO NOT USE THIS (the builtin Django webserver) SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests.

http://docs.djangoproject.com/en/dev/ref/django-admin/#runserver-port-or-address-port

But you don't have use Apache if you don't want to. You could directly use Spawning, Gunicorn etc.

Cherokee is also easy to setup.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
esamatti
  • 18,293
  • 11
  • 75
  • 82
  • 1
    You definitely don't want to use the built-in webserver. If you don't want to mess with a "heavy" Apache install, you might look at lighttpd (http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/?from=olddocs#lighttpd-setup) or nginx (http://code.djangoproject.com/wiki/DjangoAndNginx) – bhamby Feb 01 '11 at 20:57
14

Use nginx + gunicorn.

Nginx: five lines of configuration. Gunicorn: two lines of configuration. That's easy and efficient. For better control you can spawn the gunicorn process using supervisord.

Both gunicorn and supervisord are available to install with pip, and nginx is available in almost any distribution in the default package pool.

rewritten
  • 16,280
  • 2
  • 47
  • 50
  • do you by change know, if Gunicorn setup is faster than fast-cgi setup? – Nick Feb 10 '15 at 10:05
  • could you show those 7 lines of configuration please? – temirbek Jan 19 '22 at 06:28
  • @temirbek this was 11 years ago, some things may have changed (usually for the better) but you can start here https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/gunicorn/ and https://docs.gunicorn.org/en/latest/deploy.html#nginx-configuration. Of course these guides show more than 7 lines, my point probably was that most of the nginx config file is for nginx itself, not for your django application. – rewritten Jan 19 '22 at 08:39
  • I'll take a look, thank you – temirbek Jan 19 '22 at 09:41
3

The built in Django server was not built for production. There are many reasons why, mainly security and efficiency. The recommended way is to use mod_wsgi which is covered in the docs here

sdk900
  • 493
  • 3
  • 15