Ossian Story
article thumbnail


[ Gunicorn 이란? ]

  • Python WSGI(Web Server Gateway Interface)로 WEB Server(Nginx)로부터 서버사이드 요청을 받으면 WSGI(Gunicorn)를 통해 서버 어플리케이션(Django)으로 전달하는 역할을 함
  • Django의 [ runserver ] 명령어는 단일 쓰레드로 작동하여 테스트용도로 적당하나, Request 요청이 많아질 경우 현저히 처리 능력이 떨어지므로 Production 환경에는 사용할 수 없음
  • WSGI는 멀티 쓰레드를 만들 수 있도록하여 Request 요청이 많아지더라도 효율적으로 처리하므로 Production 환경에 사용

 

 


[ Gunicorn 설치 ]

  • Python 가상환경에서 Gunicorn 설치
$ pip install gunicorn

 


[ Gunicorn 설정 ]

  • Gunicorn Demon Service 생성 및 설정
    + User = Gunicorn 서비스를 실행할 사용자 계정
    + Group = Gunicorn 서비스를 실행할 사용자 그룹
    + WorkingDirectory = Django Project의 manage.py가 위치한 경로
    + ExecStart = gunicorn 서비스가 설치된 경로(가상환경에 설치하였으므로 해당 경로를 지정)
    + ExecStart의 --workers 옵션 = 쓰레드 갯수 입력(통상 해당 서버 Core * 2를 지정)
    + ExecStart의 --bind 옵션 = unix:[ sock 파일 생성 위치 ] [ Django Project의 wsgi 파일위치 ]
$ vi /etc/systemd/system/gunicorn.service


[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/opt/todo/
ExecStart=/root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --bind unix:/opt/todo/socket.sock todo.wsgi:application

[Install]
WantedBy=multi-user.target

 


[ Gunicorn Service 실행 및 확인 ]

  • Gunicorn Service 적용 및 시작
$ systemctl daemon-reload

$ systemctl start gunicorn

 

  • Gunicorn Service 확인
    + Worker에서 설정한 숫자만큼 쓰레드가 만들어짐
    + Gunicorn에서 설정한 Bind의 scoket.sock가 만들어짐
$ systemctl status gunicorn

● gunicorn.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: disabled)
   Active: active (running) since 토 2020-02-08 22:20:29 KST; 1h 43min ago
 Main PID: 1655 (gunicorn)
   CGroup: /system.slice/gunicorn.service
           ├─1655 /root/.virtualenvs/env_django/bin/python3 /root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --...
           ├─1658 /root/.virtualenvs/env_django/bin/python3 /root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --...
           ├─1660 /root/.virtualenvs/env_django/bin/python3 /root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --...
           ├─1661 /root/.virtualenvs/env_django/bin/python3 /root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --...
           ├─1664 /root/.virtualenvs/env_django/bin/python3 /root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --...
           ├─1666 /root/.virtualenvs/env_django/bin/python3 /root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --...
           ├─1668 /root/.virtualenvs/env_django/bin/python3 /root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --...
           ├─1670 /root/.virtualenvs/env_django/bin/python3 /root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --...
           └─1672 /root/.virtualenvs/env_django/bin/python3 /root/.virtualenvs/env_django/bin/gunicorn --access-logfile - --workers 8 --...

 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1655] [INFO] Listening at: unix:/opt/todo/socket.sock (1655)
 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1655] [INFO] Using worker: sync
 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1658] [INFO] Booting worker with pid: 1658
 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1660] [INFO] Booting worker with pid: 1660
 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1661] [INFO] Booting worker with pid: 1661
 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1664] [INFO] Booting worker with pid: 1664
 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1666] [INFO] Booting worker with pid: 1666
 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1668] [INFO] Booting worker with pid: 1668
 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1670] [INFO] Booting worker with pid: 1670
 2월 08 22:20:29 django-01 gunicorn[1655]: [2020-02-08 22:20:29 +0900] [1672] [INFO] Booting worker with pid: 1672
 
 
 ----
 
 
 $ cd /opt/todo
 
 .
├── db.sqlite3
├── manage.py
├── socket.sock
└── todo
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-36.pyc
    │   ├── settings.cpython-36.pyc
    │   ├── urls.cpython-36.pyc
    │   └── wsgi.cpython-36.pyc
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

 

 

profile

Ossian Story

@ossians