본문 바로가기
Python and Django

Apache와 Django 연결

by leo21c 2012. 11. 29.
SMALL

1. Apache를 다운 받아 설치 한다.
http://www.apache.org

2. Python, Django를 다운 받아 설치 한다.
http://www.python.org/download/

 python27폴더를 속성(내컴퓨터) -> 고급 -> 환경변수 -> path에 추가

https://www.djangoproject.com/download/

 command line에서 django 압축해제한 디렉토리로 이동
 c:\>cd c:\Django-1.4
 c:\Django-1.4>python setup.py install

 Django-1.4\django\bin 디렉토리에 있는 django-admin.py를 c:\windows나 파이썬이 설치된 디렉토리에 복사한다.

3. Django 프로젝트를 생성한다.

 command line에서 프로젝트 생성 (원하는 디렉토리 이동 후 아래 명령어 입력. 하위에 mysite폴더가 생성됨.)

 C:\django-admin.py startproject mysite

아래와 같은 구조로 폴더 및 파일이 생성된다.

 C:\mysite\manage.py
 C:\mysite\mysite\__init__.py
 C:\mysite\mysite\settings.py
 C:\mysite\mysite\urls.py
 C:\mysite\mysite\wsgi.py 

 mysite /
            manage.py /
                   mysite /
                               __init__.py
                               settings.py
                               urls.py
                               wsgi.py 

 위와 같이 생성 되는데 manage.py와 settings.py가 같은 폴더에 있어야 한다.
 따라서 misite 아래에 있는 4개의 파일을 manage.py와 같은 폴더로 옮기고 sub 폴더인
 mysite는 지운다.

4. manager.py 파일 편집
 mysite 폴더에 있는 파일을 옮겼기 때문에 아래와 같이 수정을 한다.
 mysite.settings -> settings

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

5. setting.py 파일 편집
 manage.py와 같이 mysite 표시를 제거한다.

ROOT_URLCONF = 'urls'

WSGI_APPLICATION = 'wsgi.application'

6. wsgi.py 파일 편집 및 테스트 소스 추가
 mysite 표시를 제거하고 Apache가 정상적으로 연결되었는지 확인을 해 보기 위한 소스를 아래와 같이 추가한다.

"""
WSGI config for mysite project.

This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.

Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.

"""
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)
'''
def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]
'''

아래 라인을 주석 처리하고 
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

'''로 주석 처리되어 있는 def applicaton 함수의 주석을 푼다.

#from django.core.wsgi import get_wsgi_application
#application = get_wsgi_application()

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

7. Apache "httpd.conf" 파일 편집
 8080 포트로 접근 할 수 있도록 VirtualHost 세팅을 한다. 80 포트로 한다면 VirtualHost만 배고 세팅을 하면 된다.

Listen localhost:8080

LoadModule wsgi_module modules/mod_wsgi.so

NameVirtualHost *:8080

<VirtualHost *:8080>
    ServerName localhost
    DocumentRoot "C:/mysite"

    <Directory "C:/mysite">
       Order allow,deny
       Allow from all
    </Directory>

    WSGIScriptAlias / "C:/mysite/wsgi.py"
</VirtualHost>

8. Apache를 실행하고 웹브라우저에서 http://localhost:8080 입력을 하면 "Hello World!"를 볼 수 있다. 만약 정상적으로 실행이 되지 않는다면 path 편집 하는 부분을 다시 한번 확인해 보기 바란다.

LIST