2011-12-20

Debian/Ubuntu + NginX + trunserver + Django 1.3.1 + Python 2.7.2

Django를 Deploy하기 위한 여러가지 방법들을 소개했었는데, 이번 방법이 내가 가장 선호하는 방식이다. Instance 별로 정상동작 유무를 확인하기가 쉽고, NginX 에서는 로드밸런싱을 하고, 하나의 Instance 에 문제가 발생하면 자동으로 제외되어 무장애/무중단 서비스를 제공할 수 있다.

여기에서 더 나아가 memcached 등을 이용하여 캐싱까지 처리하면 성능에서도 상당히 만족스러운 결과를 볼 수 있을 것이다. 또한, trunserver 조작에 대해서 개선할 부분이 많이 있다. 이런 부분들은 다음에 생각해보고, NginX와 trunserver 를 이용한 방법을 살펴보자.

Install Python

$ apt-get install zlibc zlib1g-dev libxml2 libxml2-dev libbz2-dev curl build-essential python
$ curl -kL http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install | bash
$ echo 'source ~/.pythonbrew/etc/bashrc' >> ~/.bashrc
$ source ~/.bashrc
$ pythonbrew install --force --no-test 2.7.2
$ pythonbrew switch 2.7.2

Install Django, trunserver

$ pip install django psycopg2 trunserver

Create Project

$ mkdir -p /opt/project
$ cd /opt/project
$ django-admin.py startproject sample_project
$ cd sample_project
$ mkdir media

/opt/project/sample_project 에 start.sh를 만든다.

nohup python manage.py trunserver --verbosity=0 --noreload 0.0.0.0:9000 > /dev/null 2>&1 &
nohup python manage.py trunserver --verbosity=0 --noreload 0.0.0.0:9001 > /dev/null 2>&1 &
$ chmod 755 start.sh

trunuwsgi 실행

$ ./start.sh

Install NginX

$ aptitude install nginx

Configure NginX

$ cd /etc/nginx/sites-available
$ vi django 
upstream sample_project {
    ip_hash;
    server 127.0.0.1:9000;
    server 127.0.0.1:9001;
}

server {
    listen 80;
    server_name mydomain.com;
    
    location /site_media  {
        root /opt/project/sample_project/media/;
    }

    location / {
        proxy_pass sample_project;
        proxy_set_header XRealIP $remote_addr;
    }
}
$ cd /etc/nginx/sites-enabled
$ ln -s /etc/nginx/sites-available/django django

Run NginX

$ /etc/init.d/nginx restart

댓글 없음:

댓글 쓰기