how to setup pinax with nginx
Tonight I spent sometime getting Pinax – Django 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.


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?
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
Here is a step by step guide keeping Apache around.
What is the memory usage you are getting for the entire stack? I wonder if nginx serves the static media directly or does it get parsed by fastcgi first, and if so could you configure nginx to directly serve the static media. Anything that cuts pinax heavy memory demand on any kind of host is awesome.
Oh and one last thing, I personally, would not call MySQL “proper db”. I would go with PostgreSQL
“python ./manage.py runfcgi method=threaded host=127.0.0.1 port=8801″
HOW TO STOP IT?
I’ve changed config at settings.py but process still running..
“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″
I can start this process via ssh..but when I close ssh client process ends.. how to strat it from server side?
One more issue:
Pinax need to know if its running under proxy:
add BEHIND_PROXY = True in settings.py
and nginx must send real addres of client:
fastcgi_param HTTP_X_FORWARDED_FOR $proxy_add_x_forwarded_for;