The usual virtualenv/Django/gunicorn/nginx howto ... from my perspective
sudo apt-get install nginx
Install distribute (the new hotness!) Merged with setuptools
curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
sudo apt-get install python-pip
* will also install the new python-setuptools
Install virtualenv (all time hotness)
Create Virtual Environment
virtualenv --no-site-packages env_my_application
Activate Virtual Environment
source /home/user/env_my_application/bin/activate
Install required packages
or if you already have a requirements.txt file
pip install -r requirements.txt
Change to your Django application folder
cd /home/user/my_application/
Test Django Application with Gunicorn
gunicorn my_application.wsgi:application -b 0.0.0.0:8000
vim /home/user/guni_my_application.sh
LOGFILE=/home/user/logs/gunicorn.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=9 #1 + 2 * CPUs
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
sudo vim /etc/init/your_service_name.conf
description "Your Service Name"
exec /home/user/guni_my_application.sh
create link to /etc/init.d
sudo ln -s /lib/init/upstart-job /etc/init.d/your_service_name
sudo service your_service_name start
Create nginx conf file for application
sudo vim /etc/nginx/sites-available/my_application.conf
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 /favicon.ico { # favicon.ico
alias /home/user/my_storage/my_application_static/media/favicon.ico;
location /static/ { # STATIC_URL
alias /home/user/my_storage/my_application_static/static/; # STATIC_ROOT
location /media/ { # MEDIA_URL
alias /home/user/my_storage/my_application_static/media/; # MEDIA_ROOT
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_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 service my_application start
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
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/gunicorn/
https://code.djangoproject.com/wiki/DjangoAndNginx
http://docs.gunicorn.org/en/latest/run.html#gunicorn
http://senko.net/en/django-nginx-gunicorn/
Update 07/Jun/2013: Added the favicon alias on nginx and added the forgotten upstart job create step.
Update 12/Nov/2013: Removed references to distribute since it is now merged with setuptools.