Code


Version 41 (modified by bs, 6 years ago) (diff)

Fixed improper ticket link in Devserver chapter (should be ticket 4996, not 2996)

Server arrangements

TOC?

Because Django uses WSGI, it can run on any WSGI-compatible Web server. Here's how to run Django on various server arrangements.

Apache

Apache with mod_python

Apache with FastCGI

Apache with SCGI

  • SCGI can be used with Apache like this. It builds on stuff from the Apache+FCGI documentation above.

The material at simon.bofh.ms has been hard to get - server sometimes responds, sometimes not IME. In any event, #3047 has patches, and seems to be on track to become the official way to do SCGI with Django.

Apache with mod_wsgi

lighttpd

lighttpd with Apache

lighttpd with FastCGI

lighttpd with SCGI

FastCGI

Twisted.web2

nginx

I've been serving up my django site via nginx for a short while now and wanted to report my setup so that others may give it a shot. My performance over the previous setup with lighttpd is a bit better. This setup uses much less cpu and memory to achieve much better throughput and response time. The simple syntax of the nginx.conf allows you to setup a cluster of backend processes quite easily to handle load. Currently this is my nginx.conf relevant to my django site. You can reference the above links for more information about additional options.

upstream djangoserv {
server 127.0.0.1:8801;
}

server {
listen 80;
root /path/to/app;
server_name test.local.domain;
access_log /path/to/logs/appname-access.log main;
error_log /path/to/logs/appname-error.log;

location /styles  {
	root /path/to/styles;
}

location /javascripts  {
	root /path/to/javascripts;
}

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
        access_log   off;
        expires      30d; 
}

location / {
                        # host and port to fastcgi server
                        fastcgi_pass 127.0.0.1:8801;
                        fastcgi_param PATH_INFO $fastcgi_script_name;
                        fastcgi_param REQUEST_METHOD $request_method;
                        fastcgi_param QUERY_STRING $query_string;
                        fastcgi_param SERVER_NAME $server_name;
                        fastcgi_param SERVER_PORT $server_port;
                        fastcgi_param SERVER_PROTOCOL $server_protocol;
                        fastcgi_param CONTENT_TYPE $content_type;
                        fastcgi_param CONTENT_LENGTH $content_length;
                        fastcgi_pass_header Authorization;
                        fastcgi_intercept_errors off;
}
}

Once you fire up nginx you will need to start your nginx fastcgi processes. I simply use:

python2.4 manage.py runfcgi method=threaded host=127.0.0.1 port=8801 

Django behind/inside Zope

It's possible to query a Django site from Zope or Plone and return the result. This allows you to include a Django site inside a pre-existing Zope/Plone site. It's good for custom content that you don't want to use existing Zope technologies to develop. For the code, see this partly documented file. It's in a temporary location for the time being; for more info, e-mail jeff (at) bitprophet (dot) org.

CGI

Running Django as a traditional CGI is possible and would work the same as running any other sort of Python CGI script, but is generally not recommended.

With traditional CGI, the program which will be run -- in this case, Django plus a Django-powered application -- is loaded from disk into memory each time a request is served, which results in a significant amount of processing overhead and much slower responses. FastCGI and SCGI, in contrast, load the code only once -- when the server starts up -- and keep it in memory as long as the server is running, resulting in much faster responses.

If that hasn't put you off and you still need CGI, take a look at #2407.

CherryPy and WSGI

Devserver - Django built-in development server

Differences between Devserver and production