Python 으로 가장 많이 이용되는 웹프레임워크는 Django 이다. 최근 가벼움과 성능으로 인정을 받고 있는 NginX 와 FastCGI 로 연동하여 설치하는 방법을 간단하게 정리해보았다. CentOS 6.0 이 나오긴 했지만, 아직 5.x 환경도 많이 이용되고 있으리라 생각하여 CentOS 5.6 에서 테스트하였으며, 아마 6.0 에서도 무리없이 설치될 것으로 믿는다.

  1. Install Python
    # echo 'export PYTHONBREW_ROOT=/opt/pythonbrew' >> /etc/profile; source /etc/profile
    # curl -kLO http://xrl.us/pythonbrewinstall; chmod +x pythonbrewinstall; ./pythonbrewinstall
    # echo 'source /opt/pythonbrew/etc/bashrc' >> /etc/profile; source /etc/profile
    # pythonbrew install --force --no-test 2.7.2
    # pythonbrew switch 2.7.2
    
  2. Install Django
    # pip install django flup
    
  3. Create Project
    # mkdir -p /opt/project
    # cd /opt/project
    # django-admin.py startproject sample_project
    # cd sample_project
    # mkdir media
    
  4. Run Project
    # cd /opt/project/sample_project
    # python manage.py runfcgi method=prefork pidfile=/tmp/django_sample.pid host=127.0.0.1 port=8000
    
  5. Install NginX
    # yum install nginx
    
  6. Configure NginX
    # cd /etc/nginx
    # vi nginx.conf
    ...
        server {
            server_name 도메인주소;
            location /site_media  {
                root /opt/project/sample_project/media/;
            }
            location / {
                # host and port to fastcgi server
                fastcgi_pass 127.0.0.1:8000;
                fastcgi_param PATH_INFO $fastcgi_script_name;
                fastcgi_param REQUEST_METHOD $request_method;
                fastcgi_param QUERY_STRING $query_string;
                fastcgi_param CONTENT_TYPE $content_type;
                fastcgi_param CONTENT_LENGTH $content_length;
                fastcgi_pass_header Authorization;
                fastcgi_intercept_errors off;
            }
        }
    ...
    
  7. Run NginX
    # /etc/init.d/nginx restart
    

오랜만에 Django 를 살펴보고 있는데, 예전에는 설치할때, mod_python 이 권장이었는데, 지금은 mod_wsgi 로 설치하는 것을 권장하고 있다. 이에 WSGI 연동 설치하는 방법을 다시 한번 정리하였다. 급하게 정리한 것이라서 뭔가 부족한 것이 있을 수도 있지만, 일단 페이지가 열리는 것을 확인했으니, 테스트/개발 시에는 참고하면 괜찮을 것이다. ^^ 참고로, Debian/Ubuntu Linux 를 기반으로 작성하였으니 주의 바란다.

  1. Django 설치, Apache + mod_wsgi 설치
    # apt-get install python-django libapache2-mod-wsgi
    
  2. 프로젝트 생성
    $ mkdir -p /opt/project/web
    $ cd /opt/project/web
    $ django-admin startproject 프로젝트이름
    
  3. 템플릿 디렉토리 설정
    $ cd 프로젝트이름
    $ mkdir templates
    
    settings.py :
    import os
    
    ...
    
    TEMPLATE_DIRS = (
        os.path.join(os.path.dirname(__file__), 'templates'),
    )
    
  4. 어플리케이션 생성
    $ cd /opt/project/web/프로젝트이름
    $ python manage.py startapp 어플리케이션이름
    
  5. URL 설정 (urls.py)
    from 프로젝트이름.어플리케이션이름.views import *
    urlpatterns = patterns('',
        (r'^$', index),
    )
    
  6. View 작성
    $ cd /opt/project/web/프로젝트이름/어플리케이션이름
    $ vi views.py
    from django.shortcuts import render_to_response
    from django.template import RequestContext
    
    def index(request):
        return render_to_response(
            'index.html',
        )
    
  7. 템플릿 작성
    $ cd /opt/project/web/프로젝트이름/templates
    $ vi index.html
    <html>
    <head><title>The First Page</title></head>
    <body><h1>The First Page : Success ^^</h1></body>
    </html>
    
  8. django.wsgi 설정
    # cd /opt/project/web/프로젝트이름
    # vi django.wsgi
    import os
    import sys
    sys.path.append('/opt/project/web')
    os.environ['DJANGO_SETTINGS_MODULE'] = '프로젝트이름.settings'
    
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    
  9. Apache 설정
    # cd /etc/apache2/sites-available
    # vi 프로젝트이름
    NameVirtualHost 아이피:80
    
    <Virtualhost 아이피:80>
        ServerName 도메인주소
        DocumentRoot /opt/project/web
    
        WSGIScriptAlias / /opt/project/web/프로젝트이름/django.wsgi
    
        <Location "/css">
            SetHandler None
        </Location>
    </Virtualhost>
    
    # a2ensite 프로젝트이름
    
  10. 이제, 아파치를 재시작하고 웹브라우즈에서 http://도메인주소/를 입력해보자.

+ Recent posts