Django uWSGI 연결하기

Youngkwon Kim·2022년 7월 28일

Django

목록 보기
1/3
post-thumbnail

서론

지금까지 장고 서버를 개발한 후 원격서버에서 runserver를 통해 서버를 구동 시켰는데, 이것이 옳지 않은 방법임을 우연히 알게 되었다.
runserver는 개발자를 위한 서버 구동이라면, 사용자를 위한 서버를 구동하기 위해서는 uWSGI와 nginx 등의 웹서버를 이용하여야 한다.

장고는 우리가 잘 알듯 "웹 프레임워크"이지 웹서버가 아니기 때문이다.


1. 우선 가상환경을 켜고 uwsgi 패키지를 설치한다.

$ source ~/myvenv/bin/activate
$ pip3 install uwsgi

2. uwsgi 서버를 이용해 Django 프로젝트를 연결

uwsgi --http :[포트번호] --home [가상환경 경로] --chdir [장고프로젝트폴더 경로] -w [wsgi 모듈이 있는 폴더].wsgi

명령어가 조금 복잡한데, 필자의 경우는 다음과 같다

$ uwsgi --http :8080 --home /home/ubuntu/myvenv/ --chdir /srv/youngkwonFE/ -w youngkwonFE.wsgi

다음과 같은 콘솔 출력이 나오면 성공

*** Starting uWSGI 2.0.18 (64bit) on [Sun Mar 15 08:27:52 2020] ***
compiled with version: 7.5.0 on 15 March 2020 08:27:18
os: Linux-4.15.0-1057-aws #59-Ubuntu SMP Wed Dec 4 10:02:00 UTC 2019
nodename: ip-172-31-26-18
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/ubuntu
detected binary path: /home/ubuntu/myvenv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /srv/django-deploy-test/
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3840
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8080 fd 4
spawned uWSGI http 1 (pid: 1940)
uwsgi socket 0 bound to TCP address 127.0.0.1:44211 (port auto-assigned) fd 3
Python version: 3.6.9 (default, Nov  7 2019, 10:44:02)  [GCC 8.3.0]
PEP 405 virtualenv detected: /home/ubuntu/myvenv/
Set PythonHome to /home/ubuntu/myvenv/
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x557cd6278b80
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72904 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x557cd6278b80 pid: 1939 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 1939, cores: 1)

성공적으로 접속이 되는 것을 확인 할 수 있지만, 보통은 .ini 파일을 작성해서 해당 과정을 간소화 한다.

3. *.ini 파일 생성하기

먼저 manage.py가 있는 곳에 .config 폴더를 생성한다.

$ mkdir .config

다음으로 .config 폴더 내부에 uwsgi 폴더를 생성한다.

$ cd .config
$ mkdir uwsgi

이제 uwsgi 폴더 내부에 mysite.ini 파일을 생성한다.

$ cd uwsgi
$ vim mysite.ini

mysite.ini 파일 내용은 다음과 같다.

[uwsgi]
chdir = /srv/youngkwonFE/
module = youngkwonFE.wsgi:application
home = /home/ubuntu/myvenv/

uid = ubuntu
gid = ubuntu

http = :8080

enable-threads = true
master = true
vacuum = true
pidfile = /tmp/mysite.pid
logto = /var/log/uwsgi/youngkwonFE/@(exec://date +%%Y-%%m-%%d).log
log-reopen = true

4. 서버 로그 기록을 위한 파일 생성하기

먼저 로그파일을 보관하기 위한 폴더를 생성한다.

$ sudo mkdir -p /var/log/uwsgi/youngkwonFE

ubuntu 사용자에 로그 폴더에 대한 권한을 부여한다.

$ sudo chown -R ubuntu:ubuntu /var/log/uwsgi/youngkwonFE/

5. 서버 실행하기

다음 명령어로 서버를 실행한다.

$ sudo /home/ubuntu/myvenv/bin/uwsgi -i /srv/youngkwonFE/.config/uwsgi/mysite.ini

❗️참고

위의 과정을 완료해서 서버가 구동되는 것은 확인했으나 프로젝트의 static 파일들을 불러오지 못하는 현상이 발생했다.
해당 경우 ini 파일에 다음 한 줄을 추가한다.

static-map = /static={Static 파일의 경로}

0개의 댓글