how to setup pinax with nginx

Tonight I spent sometime getting PinaxDjango swiss army knife – to work on nginx via fastcgi on my Ubuntu 9.10 box. Here is a step by step guide.

First of course we need to install nginx, pretty easy with Ubuntu:

sudo apt-get install nginx

Create a new file called <appname> in /etc/nginx/sites-available/ like the following:

upstream djangoserv {
    server 127.0.0.1:8801;
}

server {
    listen   80;
    server_name  alkemic;

    access_log  /var/log/nginx/appname.access.log;

    location ^~ /site_media/  {
        alias /home/nick/dev/projects/pinax07/appname/site_media/;
        autoindex on;
    }
    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;
    }
}

Link it to sites-enabled so that we know it’s going to be started up (meanwhile you want to remove the default one):

sudo ln -s /etc/nginx/sites-available/appname /etc/nginx/sites-enabled/

Startup nginx with:

sudo /etc/init.d/nginx start

Activate your pinax installation (change the path to where you have installed your Pinax virtual environment):

source /home/nick/dev/projects/pinax07/bin/activate

Install flup (required to have manage.py run as fastcgi) in your virtual env:

pip install flup

Symlink all the static assets into the site_media folder:

css -> /home/nick/dev/projects/pinax07/appname/media/css/
img -> /home/nick/dev/projects/pinax07/appname/media/img
js -> /home/nick/dev/projects/pinax07/appname/media/js
pinax -> ../../lib/python2.6/site-packages/pinax/media/default/pinax/
snd -> /home/nick/dev/projects/pinax07/appname/media/snd
swf -> /home/nick/dev/projects/pinax07/appname/media/swf

Launch the python fastcgi process:

python ./manage.py runfcgi method=threaded host=127.0.0.1 port=8801

Or:

python ./manage.py runfcgi method=prefork host=127.0.0.1 port=8801

Note that you might need to move your development environment to use a proper db like MySQL because otherwise the app will have problems finding your sqlite db file.

You should have now your app running on the blazing fast nginx.

3 comments ↓

#1 Pedro Assuncao on 10.07.09 at 6:09 am

This means you basically ditch apache to serve anything, right? Is nginx more efficient (or at least as much as apache) in terms of serving stuff?

#2 nick on 10.07.09 at 8:41 am

There are tons of possible setups. Some involve still using Apache and mod_wsgi to serve Django requests. Some don’t. I wanted to try a full setup without Apache so that’s what the configuration is about. You’re free to experiment :D Here is a step by step guide keeping Apache around.

#3 Tweets that mention how to setup pinax with nginx — be present now -- Topsy.com on 10.31.09 at 9:06 pm

[...] This post was mentioned on Twitter by robataka, robataka. robataka said: Gnolia Link how to setup pinax with nginx — be present now: Saved By: robataka | View Detail.. http://bit.ly/12HtwY [...]

Leave a Comment