Photo by Oğuzhan Akdoğan on Unsplash |
개요
CentOS 6.5에서 Django 기반 애플리케이션을 uWSGI와 nginx를 이용하여 서비스 할 수 있도록 설정하는 방법이다. 아래 순서대로 진행하였으며, uWSGI의 가이드를 따라했다. uWSGI 실행은 사용자 계정으로 하였으며, nginx는 root 계정으로 실행하였다.
- Django 애플리케이션 준비
- uWSGI(http)를 이용하여 서비스
- uWSGI(유닉스 소켓)와 nginx를 이용하여 서비스
- 소켓 기반이 부하가 더 적음
참고:
작업 환경
- CentOS 6.5
- nginx 1.10.1
- Python 2.7.5
(myvirtualenv) [USER@BLAH bin]$ pip listDjango (1.9.7)django-el-pagination (2.1.2)mysqlclient (1.3.7)pip (8.1.2)setuptools (23.1.0)unicodecsv (0.14.1)uWSGI (2.0.13.1)wheel (0.29.0)(myvirtualenv) [USER@BLAH bin]$cs
1. Django 애플리케이션 준비
django-admin startproject mypjt python manage.py startapp myapp python manage.py migrate python manage.py createsuperuser python manage.py runserver 0.0.0.0:8000 | cs |
2. uWSGI(http)를 이용하여 서비스
1에서 생성한 Django 프로젝트 디렉토리로 이동하여 uwsgi.ini 파일을 생성한다.
(myvirtualenv) [USER@BLAH mypjt]$ pwd /BLAH/mypjt (myvirtualenv) [USER@BLAH mypjt]$ cat uwsgi.ini [uwsgi] http = 0.0.0.0:8000 chdir = /BLAH/mypjt/ wsgi-file = mypjt/wsgi.py processes = 4 threads = 2 stats = 0.0.0.0:8001 virtualenv = /BLAH/myvirtualenv (myvirtualenv) [USER@BLAH mypjt]$ | cs |
그리고 아래 명령어를 이용하여 uWSGI를 실행하고, 브라우저에서 정상적으로 호출되는 지 확인한다.
(myvirtualenv) [USER@BLAHBLAH mypjt]$ uwsgi uwsgi.ini | cs |
3. uWSGI와 nginx를 이용하여 서비스
2번 과정이 정상적으로 완료되었으면, nginx와의 연동을 진행한다. nginx와 연동 시 유닉스 소켓을 이용하도록 uwsgi.ini 파일을 수정한다.
(myvirtualenv) [USER@BLAH mypjt]$ pwd /BLAH/mypjt (myvirtualenv) [USER@BLAH mypjt]$ cat uwsgi.ini [uwsgi] # http = 0.0.0.0:8000 socket = mypjt.sock chmod-socket = 666 chdir = /BLAH/mypjt/ wsgi-file = mypjt/wsgi.py processes = 4 threads = 2 stats = 0.0.0.0:8001 virtualenv = /BLAH/myvirtualenv master-fifo = /BLAH/mypjt/uwsgififo (myvirtualenv) [USER@BLAH mypjt]$ | cs |
다음으로 nginx 설정파일을 생성한다.
(myvirtualenv) [USER@BLAH mypjt]$ pwd /BLAH/mypjt (myvirtualenv) [USER@BLAH mypjt]$ cat mypjt_nginx.conf # mypjt_nginx.conf # configuration of the server upstream django { server unix:///BLAH/mypjt/mypjt.sock; # for a file socket #server 0.0.0.0:8000; # for a web port socket (we'll use this first) } server { listen 80; server_name localhost; #charset utf-8; client_max_body_size 10M; # Django media location /media { alias /BLAH/mypjt/media; # your Django project's media files - amend as required } location /static { alias /BLAH/mypjt/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { include /BLAH/mypjt/uwsgi_params; uwsgi_pass django; } } (myvirtualenv) [USER@BLAH mypjt]$ sudo ln -s /BLAH/mypjt/mypjt_nginx.conf /etc/nginx/conf.d/ (myvirtualenv) [USER@BLAH mypjt]$ sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak | cs |
설정을 완료한 후, uWSGI와 nginx를 재시작 후, 브라우저에서 정상적으로 호출되는 지 확인한다.
(myvirtualenv) [USER@BLAH mypjt]$ uwsgi uwsgi.ini & (myvirtualenv) [USER@BLAH mypjt]$ sudo service nginx restart | cs |
uWSGI의 종료와 재시작 명령어는 아래와 같다.
- 종료: (myvirtualenv) [USER@BLAH mypjt]$ echo q > /BLAH/mypjt/uwsgififo
- 재시작: (myvirtualenv) [USER@BLAH mypjt]$ echo r > /BLAH/mypjt/uwsgififo
참고로 uwsgi.ini에서 socket 파일의 접근 권한을 설정하지 않으면, nginx에서 연결을 하지 못해 서비스가 안될 수 있다.
2016/07/10 13:10:34 [crit] 12552#12552: *1 connect() to unix:///BLAH/mypjt/mypjt.sock failed (13: Permission denied) while connecting to upstream, client: 114.200.240.6, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:///BLAH/mypjt/mypjt.sock:", host: "BLAH:80", referrer: "http://BLAH:80/static/xx/xx.js" | cs |
nginx 에러 로그는 '/var/log/nginx/error.log'에서 확인할 수 있다.
0 댓글