The usual virtualenv/Django/gunicorn/nginx howto … from my perspective
Install nginx
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
Install pip *
sudo apt-get install python-pip
* will also install the new python-setuptools
Install virtualenv (all time hotness)
pip install virtualenv
Create Virtual Environment
virtualenv –no-site-packages 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 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
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 Upstart job
sudo vim /etc/init/your_service_name.conf
add the following
description “Your Service Name”
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
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
start your service
sudo service your_service_name start
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 /favicon.ico { # favicon.ico
alias /home/user/my_storage/my_application_static/media/favicon.ico;
expires 30d;
}
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
- 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.