Version 12 (modified by anonymous, 7 years ago) (diff)

Fix static location to work out of the box. Add a note on security.

I just recently grabbed a hosting setup on and wanted to get Django up and running. I'd heard good things about nginx, but had never set it up or configured it before, so I figured I'd give it a try. There were a few little bumps I ran into, but in the end the setup worked pretty well so I wanted to share my setup and configuration experience with everyone here.

This setup is running Ubuntu Hardy 8.04 as a xen instance on VPSLink, this walks through starting everything up from scratch, but assumes you are running as root (type su to switch to the root user)

The first thing I had to do was install python:

aptitude install python

Next, I needed Django, which I got the latest SVN release (due to a ManyToMany bug I saw in the past, I'm sticking to a release I didn't have trouble with):

aptitude install subversion
cd /
mkdir django
cd django
svn co
cd trunk
python install

Next, to run Django through fastcgi, we need python-flup:

aptitude install python-flup

Let's start a sample project just to make things easy: (Note: *should be* in your path and executable after a proper install, if this is not the case you may need to fix your installation or environment)

cd /
mkdir projects
cd projects/ startproject sample_project

Also, create a media directory:

cd /
mkdir media

Let's start up the fastcgi setup: (Note: I didn't need any extra stuff on my pythonpath, but if you do, just add --pythonpath=/path/to/add) (Note: Nothing should print here)

cd /projects/sample_project/
python runfcgi host= port=8080 --settings=settings

Now let's setup Nginx:

First, create a user for nginx to run as, and then remove a password from it so no one can log in as the user:

useradd nginx
passwd -d nginx

Let's install nginx from aptitude:

aptitude install nginx

Next, we need to change the configuration (and back up the default one just in case):

cd /etc/nginx/
mv nginx.conf nginx-backup.conf
touch nginx.conf

Now we need to put some new stuff into the default nginx configuration, here is the template I used:

user  nginx nginx;

worker_processes  2;

error_log /var/log/nginx/error_log info;

events {
	worker_connections  1024;
	use epoll;

http {
	include		/etc/nginx/mime.types;
	default_type	application/octet-stream;

	log_format main
		'$remote_addr - $remote_user [$time_local] '
        	'"$request" $status $bytes_sent '
		'"$http_referer" "$http_user_agent" '

	client_header_timeout	10m;
	client_body_timeout	10m;
	send_timeout		10m;

	connection_pool_size		256;
	client_header_buffer_size	1k;
	large_client_header_buffers	4 2k;
	request_pool_size		4k;

	gzip on;
	gzip_min_length	1100;
	gzip_buffers	4 8k;
	gzip_types	text/plain;

	output_buffers	1 32k;
	postpone_output	1460;

	sendfile	on;
	tcp_nopush	on;
	tcp_nodelay	on;

	keepalive_timeout	75 20;

	ignore_invalid_headers	on;
	index index.html;

	server {
		listen 80;
		server_name localhost;
		location /site_media  {
			root /media/; # Notice this is the /media folder that we create above

			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_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;
			fastcgi_param REMOTE_ADDR $remote_addr;
		access_log	/var/log/nginx/localhost.access_log main;
		error_log	/var/log/nginx/localhost.error_log;

Now all we need to do is start up nginx:

/etc/init.d/nginx start

Anonymous says there is a lot cleaner way to install Nginx+Django on a Debian-based system. See this post on stackoverflow for details

Back to Top