The usual virtualenv/Django/gunicorn/nginx howto … from my perspective
Install nginx
sudo apt-get install nginx
Install distribute (the new hotness!)
curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
Install pip (the new hotness!)
easy_install pip
Install virtualenv (all time hotness)
pip install virtualenv
Create Virtual Environment
virtualenv —no-site-packages —distribute env_my_application
Activate Virtual Environment
source /home/user/env_my_application/bin/activate
Install required packages
pip install Django
pip install gunicorn
or if you already have a requirements.txt file
pip install -r requirements.txt
Change to you Django application folder
cd /home/user/my_application/
Test Django Application with Gunicorn
gunicorn my_application.wsgi:application -b 0.0.0.0:8000
Create gunicorn script
vim /home/user/guni_my_application.sh
add the following code
#!/bin/bash
set -e
LOGFILE=/home/user/logs/gunicorn.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=9 #1 + 2 * CPUs
# user/group to run as
USER= user
GROUP= user
ADDRESS=127.0.0.1:8000
cd /home/user/my_application
source /home/user/env_my_application/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec gunicorn -w $NUM_WORKERS —bind=$ADDRESS \
—user=$USER —group=$GROUP —log-level=debug \
—log-file=$LOGFILE 2»$LOGFILE
my_application.wsgi:application
give proper privileges with
sudo chmod ug+x /home/user/guni_my_application.sh
Create nginx conf file for application
sudo vim /etc/nginx/sites-available/my_application.conf
add the following code
server {
listen 80 default_server;
#server_name my_application.mydomain.gr;
access_log /home/user/logs/nginx.access.log;
error_log /home/user/logs/nginx.error.log;
location /static/ { # STATIC_URL
alias /home/user/my_storage/my_application_static/static/; # STATIC_ROOT
expires 30d;
}
location /media/ { # MEDIA_URL
alias /home/user/my_storage/my_application_static/media/; # MEDIA_ROOT
expires 30d;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://localhost:8000/;
}
}
Create symlink to sites-enabled folder
ln -s /etc/nginx/sites-available/my_application.conf /etc/nginx/sites-enabled/my_application.conf
Make sure that nginx has access to your folders (that is if you get an error 13: Permission denied). Check the user is running under at /etc/nginx/nginx.conf, line 1!
Reload nginx configuration
sudo nginx -s reload
Start your service
sudo service my_application start
Enjoy!!!
The above steps were executed on an Ubuntu 12.04 LTS x64 with a user account with sudo privileges. It should run without any problem in any system
Information gathered by the following resources and tutorials


