WSGI (Web Server Gateway Interface)

웹 서버와 웹 어플리케이션 인터페이스를 위한 파이썬 모듈이다.

web server (nginx, apache) <=> web application (django)

How to deploy with WSGI

uWSGI

Setting up Django and your web server with uWSGI and nginx 정리한 것이다.

Concept

웹 서버는 외부 세계를 마주한다. 파일 시스템에서 직접 파일 (HTML, 이미지, CSS 등)을 제공 할 수 있다. 그러나 장고 응용 프로그램과 직접 대화 할 수는 없다. 응용 프로그램을 실행하고 웹 클라이언트(예: 브라우저)의 요청에 응답을 보내고 응답을 반환 할 수있는 무언가가 필요하다.

웹 서버 게이트웨이 인터페이스(WSGI)가 이 작업을 수행한다. WSGI는 Python 표준이다.

uWSGI는 WSGI 구현이다. 이 튜토리얼에서는 uWSGI를 설정하여 유닉스 소켓을 만들고 WSGI 프로토콜을 통해 웹 서버에 대한 응답을 제공합니다. 구성 요소 스택은 다음과 같다.

the web client <=> the web server <=> the socket <=> uwsgi <=> Django

Requirement

  • pyenv
  • django : django-admin startproject mysite
  • About the domain and port

uWSGI 설치와 구성

uWSGI virtualenv에 설치

$ pip install uwsgi

Basic test

test.py 파일 생성 :

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3

# Take into account that Python 3 requires 'bytes()'

uWSGI 실행 :

$ uwsgi --http :8000 --wsgi-file test.py
  • http :8000 : use protocol http, port 8000
  • wsgi-file test.py : load the specified file, test.py

구성 요소 스택은 다음과 같다.

the web client <-> WSGI <-> Python

Test your Django project

uWSGI가 test.py 모듈 대신 장고 사이트를 실행해보자.

mysite 프로젝트가 실제로 작동하는지 확인한다. (서버에서 테스트하기 위해 ALLOWED_HOSTS에 서버 도메인을 입력한다.)

$ ./manage.py runserver 0.0.0.0:8000
$ vi mysite/settings.py
# settings.py
ALLOWED_HOSTS = ["ec2-00-000-000-000.ap-northeast-2.compute.amazonaws.com"]

uWSGI를 사용하여 실행 :

$ uwsgi --http :8000 --module mysite.wsgi
  • module mysite.wsgi: load the specified wsgi module

구성 요소 스택은 다음과 같다.

the web client <-> uWSGI <-> Django

Basic nginx

nginx 설치

$ sudo apt-get install nginx
$ sudo /etc/init.d/nginx start    # start nginx
[ ok ] Starting nginx (via systemctl): nginx.service.

ec2-00-000-000-000.ap-northeast-2.compute.amazonaws.com에 80번 포트로 접속해 “Welcome to nginx!” 메시지 확인

구성 요소 스택은 다음과 같다.

the web client <-> the web server