Monday, February 28, 2011

Installing django, nginx and uwsgi on Ubuntu

Installing nginx and uwsgi

sudo su -
nginx=stable # use nginx=development for latest development version
apt-get install python-software-properties
add-apt-repository ppa:nginx/$nginx
add-apt-repository ppa:uwsgi/release
apt-get update
apt-get install nginx uwsgi python-virtualenv

Make a virtual environment. Ex.
virtualenv /home/jason/virtualenv

Activate the virtualenv ex.
source /home/jason/virtualenv/bin/activate

Install django
pip install django

Create django project ex.
django-admin.py startproject /home/jason/virtualenv/djangoprojecthome

Deploy nginx
Edit /etc/nginx/sites-available/default
ex.
upstream django {
# server unix:/var/run/uwsgi/uwsgi-python2.6/pinax/socket;
server 127.0.0.1:4000;
}

server {
listen 80;
server_name django.jasonwang.us;

location / {
uwsgi_pass django;
include uwsgi_params;
uwsgi_param UWSGI_PYHOME /home/jason/virtualenv;
uwsgi_param UWSGI_SCRIPT deploy; #the name of deploy.py
# uwsgi_param SCRIPT_NAME django;
uwsgi_param UWSGI_CHDIR /home/jason/virtualenv/djangoprojecthome;
}
location ^~ /media/ {
root /home/jason/virtualenv/djangoprojecthome/static;

}
}

ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

Edit /etc/uwsgi/uwsgi-python2.6/default.ini
I use vhost in my xml for virtual hosting to run multiple sites.

[uwsgi]
socket = 127.0.0.1:4000
master = true
processes = 4
vhost = true
no-site = true


Create a django wsgi file in your django project file. ex. /home/jason/virtualenv/djangoprojecthome/deploy.py

# pinax.wsgi is configured to live in projects/pine/deploy.
# from pinax
import os
import sys

from os.path import abspath, dirname, join
from site import addsitedir
addsitedir('/home/jason/virtualenv/lib/python2.6/site-packages')

sys.path.insert(0, abspath(join(dirname(__file__), "../")))

from django.conf import settings
os.environ["DJANGO_SETTINGS_MODULE"] = "djangoprojecthome.settings"

# sys.path.insert(0, join(settings.PROJECT_ROOT, "apps"))

from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

Restart nginx and uwsgi

sudo /etc/init.d/uwsgi-python2.6 restart
sudo /etc/init.d/nginx restart




No comments:

Post a Comment