How to install Django (python) on Nginx

apt-get update
apt-get upgrade
apt-get install build-essential psmisc python-dev libxml2 libxml2-dev python-setuptools MYSQL-python
apt-get install python-pip
apt-get install virtualenv supervisor

Download and compile uWSGI:

cd /opt/
wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
tar -zxvf uwsgi-latest.tar.gz
mv uwsgi-1.2.3/ uwsgi/
cd uwsgi/
python setup.py install
make -f MakeFile

Copy the uwsgi daemon from /opt/uwsgi/ to /usr/bin/ and /usr/sbin/
Create user and group to run uwsgi:

adduser --system --no-create-home --disabled-login --disabled-password --group uwsgi

Create log file and apply permissions:

chown -R uwsgi:uwsgi /opt/uwsgi
touch /var/log/uwsgi.log
chown uwsgi /var/log/uwsgi.log

Create init script to start and stop uwsgi:

cd /opt/
wget -O init-deb.sh http://library.linode.com/assets/683-uwsgi-init-deb.sh
mv /opt/init-deb.sh /etc/init.d/uwsgi
chmod +x /etc/init.d/uwsgi

Content of init script in case the above url doesn’t work:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          uwsgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the uwsgi app server
# Description:       starts uwsgi app server using start-stop-daemon
### END INIT INFO

PATH=/opt/uwsgi:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/uwsgi

OWNER=uwsgi

NAME=uwsgi
DESC=uwsgi

test -x $DAEMON || exit 0

# Include uwsgi defaults if available
if [ -f /etc/default/uwsgi ] ; then
        . /etc/default/uwsgi
fi

set -e

DAEMON_OPTS="-s 127.0.0.1:9001 -M 4 -t 30 -A 4 -p 4 -d /var/log/uwsgi.log --pythonpath $PYTHONPATH --module $MODULE"

case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --chuid $OWNER:$OWNER --user $OWNER 
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --signal 3 --user $OWNER --quiet --retry 2 --stop 
                --exec $DAEMON
        echo "$NAME."
        ;;
  reload)
        killall -1 $DAEMON
        ;;
  force-reload)
        killall -15 $DAEMON
       ;;
  restart)
        echo -n "Restarting $DESC: "
        start-stop-daemon --signal 3 --user $OWNER --quiet --retry 2 --stop 
                --exec $DAEMON
        sleep 1
        start-stop-daemon --user $OWNER --start --quiet --chuid $OWNER:$OWNER 
               --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  status)  
        killall -10 $DAEMON
        ;;
      *)  
            N=/etc/init.d/$NAME
            echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
            exit 1
            ;;
    esac
    exit 0

Make this script executable using:

/usr/sbin/update-rc.d -f uwsgi defaults
/etc/init.d/uwsgi start

Nginx configuration:

location / {
                include uwsgi_params;
                uwsgi_pass localhost:8989; 
                uwsgi_param UWSGI_PYHOME /home/navi/sites/website.com;
                uwsgi_param UWSGI_SCRIPT askbot_wsgi; 
                uwsgi_param UWSGI_CHDIR  /home/navi/sites/website.com/public;
        }

location /static/ {
                alias /home/navi/sites/website.com/public/static/;
expires max;
   }

location /upfiles/ {
                alias /home/navi/sites/website.com/public/upfiles/;
        }

        location /admin/media {
                alias /home/navi/sites/website.com/public/static/admin;
        }
location /media/ {
      alias /home/navi/sites/website.com/public/media/;
    }

References:
http://library.linode.com/web-servers/nginx/python-uwsgi/debian-6-squeeze
http://askbot.org/en/question/7492/how-to-deploy-on-nginx-webserver-using-uwsgi
http://brandonkonkle.com/blog/2010/sep/14/django-uwsgi-and-nginx/
http://senya.pl/2011/03/sexy-nginx-uwsgi-stack-for-django-with-virtualenv/

virtualenv --version

apt-get install python-virtualenv
pip install virtualenv

To exit your virtualenv just type "deactivate".

To update, type "pip install -U askbot"

To install specific django version, type "pip install Django==1.4.5"

/etc/init.d/uwsgi

STEPS:
go to /home/user/sites
type "virtualenv --no-site-packages --distribute ask.website.com"
type "source bin/activate" while in directory /ask.website.com/
check with "which python" "which django-admin.py" "which pip"
type "pip install askbot"
type "pip install mysql-python"
type "askbot-setup" and enter "public" as directory and database details
rename django.wsgi to askbot_website.py
type "pip install Django==1.4.5"
python manage.py syncdb
python manage.py migrate
python manage.py collectstatic
chown -R uwsgi:uwsgi public/
service uwsgi restart
Now play with settings.py in /public with Time zone, Cache Settings and Debug Mode

Installing uwsgi:
https://library.linode.com/web-servers/nginx/python-uwsgi/debian-6-squeeze
Supervisor can be used instead of init.d script.
uwsgi can be installed in virtualenv using pip

Leave a comment

Your email address will not be published. Required fields are marked *