Gunicorn

박효상·2023년 1월 1일
0

BACKEND-TIL

목록 보기
21/21
post-thumbnail

Gunicorn이란?

  • WSGI(Web Server Gateway Interface) HTTP 서버 또는 미들웨어
  • *WSGI : CGI의 일종이며, 웹서버와 파이썬 WAS(Web Application Server) 간의 통신을 위해 만들어진 인터페이스
  • *CGI : Common Gateway Interface, 즉 공용 경로 인터페이스로 수많은 언어 또는 방식으로 구현되는 웹서버들과 앱 서버들 사이에서 데이터를 주고 받을 때 필요한 입출력 표준 방법 및 규약

사용 이유

  • Gunicorn은 멀티 쓰레드로 Client의 Requests가 많아져도 효율적으로 처리 가능
    • Django의 runserver 명령어는 단일 쓰레드 기반이며 서버 구동은 가능하지만 Production 단계에선 사용이 사실상 불가능한 반면 Gunicorn은 가능
    • Django에서 runserver 사용을 지양하는 배경
    DO NOT USE THIS SERVER IN A PRODUCTION SETTING. 
     It has not gone through security audits or performance tests. 
     (And that’s how it’s gonna stay. We’re in the business of making
     Web frameworks, not Web servers, so improving this server to be 
     able to handle a production environment is outside the scope of 
     Django)
  • Web Server와 WAS 사이에서 동시 통역사 역할
    • 여러 언어로 구현된 Web Application Server는 Gunicorn 같은 WSGI 없이 Nginx, Apache 같은 Web Server와 통신 불가

주의사항

  • Nginx는 웹서버이고, Gunicorn은 웹서버와 웹 애플리케이션을 연결해주는 WSGI 미들웨어이기 때문에 같이 사용되야 한다

EC2에서 Gunicorn 시스템 적용 방법

  1. Gunicorn 설치
pip3 install gunicorn
  1. Gunicorn 실행 파일들 담을 폴더 생성 및 접근/수정 권한 부여
mkdir run
chmod sudo chown ubuntu:www-data run
  1. EC2 인스턴스에 Gunicorn 등록하기 위한 파일 생성
sudo vi /etc/systemd/system/gunicorn.service
  1. Gunicorn 설정값 파일에 작성
    • workers는 앱서버를 위한 작업 프로세스 개수
Description=gunicorn daemon
After=network.target
User=<계정명> ubuntu
Group=<계정명> www-data
WorkingDirectory=<manage.py가 위치한 디렉토리 pwd>
ExecStart=<gunicorn이 설치된 가상환경 위치> \
        --workers 3 \
        --bind 0.0.0.0:8000 \
        <wsgi.py가 있는 디렉토리 이름>.wsgi:application
WantedBy=multi-user.target
  1. Gunicorn 실행
    • gunicorn.service 파일 실행
sudo systemctl start gunicorn
  1. Gunicorn 정상 작동 여부 체크
    • Active: active (running)! 출력 시 정상
sudo systemctl status gunicorn
profile
집념의 백엔드 개발자

0개의 댓글