(ve):Django with Nginx on Ubuntu

  • This page was last modified on October 25, 2010, at 11:50.
The (mt) Community Wiki is a collaborative project. Any (mt) Media Temple customer or employee may contribute. Not all articles and/or content have been tested for accuracy by (mt) Media Temple.

For officially moderated and tested articles, be sure to visit our KnowledgeBase.

From (mt) Community Wiki

This is an INCOMPLETE article. Use at your own risk!

First thing is to install our necessary software:

aptitude update
aptitude install nginx python-flup python-django
Please note that your application may require additional packages. You can run the following to find all available Django packages available in the repository:
aptitude search python-django

Now, lets make a directory to house our Django projects.

cd /var/www/
mkdir django; cd django

For this example, we're going to create a sample_project, and start it up:

django-admin startproject sample_project
cd sample_project
python manage.py runfcgi host=127.0.0.1 port=8080 --settings=settings

Now that it's running, we have to tell Nginx to use FastCGI for our site.

Please backup your configuration files!
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.bak

Fire up your favorite editor and make your default configuration file look similar to the following (/etc/nginx/sites-available/default).

server {
listen 80;
server_name example.com;

access_log        /var/log/nginx/example.com.access_log main;
error_log        /var/log/nginx/example.com.error_log;

location / {
fastcgi_pass 127.0.0.1:8080;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
If you are using a custom location for your media directory, then add this location block to the server code:
        location /media {
root /path/to/my/media/;
}

Finally, we need to have Nginx reload it's configuration settings.

service nginx reload

et voilà!