The cleanest and fastest server setup for Django

I am going to deploy a Django-enabled environment. I have a dedicated Ubuntu server.

I am really confused about which server applications to use. So I thought: why not ask stackoverflow.

I'm looking for:

  • Easy setup
  • Quick and easy access to resources
  • Can serve media files
  • Ability to serve multiple djangosites on one server
  • I would prefer not to install PHP or anything else that sucks resources and for which I cannot use.

I heard about mod_wsgi and mod_python on Apache, nginx and lighty. What are the pros and cons of these and I missed someone?

@Barry : Somehow I feel like Apache is bloating for me. What about alternatives?

@BrianLy : Okay, I'll do mod_wsgi again. But why do I need Apache if I serve static files using lighty? I also managed to serve the django application using lighty. Is it really that bad? Sorry for being so stupid :-)

UPDATE : What about lighty and nginx - what are some use cases when it's the perfect choice?

+42
python django apache hosting
Aug 25 '08 at 13:28
source share
13 answers

Since I was looking for more detailed answers, I decided to study the problem in detail. Please let me know if I do not understand something.

Some general guidelines are to use a separate web server for handling media. By separate, I mean the web server that does not start Django. This server can be, for example:

  • Lighttpd (Lighty)
  • Nginx (EngineX)
  • Or some other light server

Then for Django you can go in different ways. You can:

  • Feed Django through Apache and:

    • mod_python

      This is a stable and recommended / well-documented way. Cons: uses a lot of memory.

    • mod_wsgi

      From what I understand, mod_wsgi is a newer alternative. Resources seem to be faster and easier.

    • mod_fastcgi

      When using FastCGI, you delegate the Django service to another process. Since mod_python includes a python interpreter in every request, it uses a lot of memory. This is a way around this problem. There are also some security issues.

      What you do is that you start your Django FastCGI server in a separate process, and then configure apache through rewriting to invoke this process when necessary.

Or you can:

  • Serve Django without using Apache , but with another server supporting FastCGI natively:

    (The documentation mentions that you can do this if you don't have specific Apache needs. I think the reason should be to save memory.)

    • Lighttpd

    This is the server that runs Youtube. It seems quick and easy to use, however I have seen memory loss reports.

    • Nginx

    I have seen tests claiming that this server is even faster than lighttpd. This is mainly documented in Russian.

Another thing, due to limitations in Python, your server should run in branched mode, and not in a thread.

So these are my current studies, but I want more opinions and impressions.

+26
Aug 27 '08 at 8:41
source share

I am using Cherokee .

According to his tests (a grain of salt with them), it handles the load better than both Lighttpd and nginx ... But this is not why I use it.

I use it because if you type cherokee-admin , it will launch a new server that you can log into (with a one-time password) and configure the entire server through a beautifully executed webmin. This is a killer function. This has already saved me a lot of time. And it saves my server a lot of resources!

As for django, I run it as a threaded SCGI process. Works good. Cherokee may also continue to work. Again, a very nice feature.

The current version of Ubuntu repo is very old, so I would advise you to use your PPA . Good luck.

+9
Dec 09 '08 at 17:08
source share

As @Barry said, the documentation uses mod_python . I did not use Ubuntu as a server, but had good experience with mod_wsgi on Solaris. You can find the documentation for mod_wsgi and Django on the mod_wsgi site.

Quick analysis of your requirements:

  • Easy setup . I found apache 2.2 pretty easy to install and install.
  • Fast and easy on resources . I would say that it depends on your use and traffic. * You may not want to download all files using Apache and use LightTPD (lighty) for static server files.
  • Can serve multimedia files. I assume that you mean images, flash files? Apache can do this.
  • Several sites on one server . Virtual server hosting on Apache.
  • Rather than installing other extensions. Comment on everything you don't need in your Apache configuration.
+6
Aug 25 '08 at 2:00
source share

The officially recommended way to deploy a django project is to use mod_python with apache. This is described in the documentation. The primary professional in this is that it is the most documented, most supported, and most common deployment method. Cohn, that he is probably not the fastest.

+5
Aug 25 '08 at 13:49
source share

The best configuration is not so famous, I think. But here:

  • Use nginx to serve requests (dynamic application, static content directly).
  • Use the python web server to serve dynamic content.

Two fastest python web server solutions:

You need to look at google to find the current best configuration for django (still in development).

+2
Sep 20 '08 at 18:06
source share

Im uses nginx (0.6.32 taken from Sid ) with mod_wsgi . It works very well, although I canโ€™t say if this is better than the alternatives because I have never tried it. Nginx has built-in memcached support that can interact with the Django caching middleware (I don't actually use it, instead I populate the cache manually using python-memcache and invalidate it when I make changes), so the cache bypasses Django completely (my development machine can serve about 3000 requests per second).

Disclaimer: nginx mod_wsgi strongly dislikes named locations (he tries to pass them to SCRIPT_NAME ), so the obvious " error_page 404 = @django will lead to many unclear errors. I had to fix the source of mod_wsgi to fix this.

+2
Sep 24 '08 at 11:58
source share

I am also trying to understand all the options. In this blog post, I found some advantages of mod_wsgi compared to mod_python.

Several sites with low traffic on a small VPS make RAM consume the main problem, and mod_python seems like a good option. Using lighttpd and FastCGI, I was able to get minimal memory usage on a simple Django site up to 58MiB virtual and 6.5MiB resident (after restarting and servicing a single request that does not require RAM).

I noticed that upgrading from Python 2.4 to 2.5 on Debian Etch increased the minimum memory for Python processes by a few percent. On the other hand, improved memory management can have a greater opposite effect on long-running processes.

+2
Oct 06 '08 at 7:56
source share

Keep it simple: Django recommends Apache and mod_wsgi (or mod_python) . If media usage is a very important part of your service, consider Amazon S3 or Rackspace CloudFiles.

+2
Jun 15 '09 at 2:09
source share

In my opinion, the best / fastest stack is varnish-nginx-uwsgi-django. And I successfully use it.

+2
May 09 '11 at 21:01
source share

If you use lighthttpd, you can also use FastCGI to serve Django. I'm not sure how the speed compares with mod_wsgi, but if the memory works correctly, you get a couple of advantages that you will get with mod_wsgi that you wonโ€™t get with mod_python. The main thing is that you can provide each application with its own process (which really helps to store the memory of different applications, as well as use multi-core computers.

Edit: just add regarding your nginix update, if the memory serves correctly again, nginix uses green to handle concurrency. This means that you may need to be a little more careful to make sure that one application does not eat all the time on the server.

+1
Aug 25 '08 at 15:06
source share

We use nginx and FastCGI for all of our Django deployments. This is mainly because we usually deploy to Slicehost and donโ€™t want to sacrifice all of our Apache memory. I assume this will be our โ€œuse caseโ€.

Regarding comments about documentation mainly in Russian, I found most of the information on the English wiki very useful and accurate. This site also has sample configurations for Django, from which you can configure your own nginx configuration.

+1
Sep 04 '08 at 13:53
source share

There are many ways to approach this. For this reason, I recommend that you carefully read the article related to the deployment process on DjangoAdvent.com: Eric Florenzano - Deploying Django with FastCGI: http://djangoadvent.com/1.2/deploying-django-site-using-fastcgi/ Read also: Mike Malone - Django Scaling Stochastictechnologies Blog: Perfect Tuning Django Mikkel Hoegh Blog: 35% Response Time-Time-Switch-uwsgi-nginx

Hello

+1
Oct 28 '10 at 4:05
source share

I have a warning for using Cherokee. When you make changes to Django Cherokee, it supports the OLD process, instead of killing it and launching a new one.

In Apache, I highly recommend this article.

http://www.djangofoo.com/17/django-mod_wsgi-deploy-exampl

It is easy to configure, easy to kill or reset after making changes.

Just enter the terminal

 sudo /etc/init.d/apache2 restart 

and changes are visible instantly.

+1
Feb 12 '13 at 23:55
source share



All Articles