WSGI 서버 Gunicorn을 설치하고 사용해 보자.
Gunicorn 설치 및 사용방법
(1단계) Gunicorn 가상 환경에 설치하기
Gunicorn 은 개발이 아니라 운영을 위한 도구이므로 로컬 환경에 설치할 필요가 없다. 서버 환경에 Gunicorn을 설치하자. MobaXterm으로 AWS 서버에 접속한 뒤 가상 환경에서 pip를 이용하여 Gunicorn을 설치한다.
ubuntu@ip-172-26-8-122:~$ mysite (mysite) ubuntu@ip-172-26-8-122:~/projects/mysite$ pip install gunicorn Collecting gunicorn Downloading gunicorn-20.1.0-py3-none-any.whl (79 kB) |████████████████████████████████| 79 kB 6.7 MB/s Requirement already satisfied: setuptools>=3.0 in /home/ubuntu/venvs/mysite/lib/python3.8/site-packages (from gunicorn) (44.0.0) Installing collected packages: gunicorn Successfully installed gunicorn-20.1.0 (mysite) ubuntu@ip-172-26-8-122:~/projects/mysite$ |
(2단계) Gunicorn 실행 방법
Gunicorn이 정상으로 실행되는지 간단하게 실행해 보자. 여러 명령을 입력해야 하므로 주의한다.
명령어> gunicorn --bind 0:8000 config.wsgi:application
(mysite) ubuntu@ip-172-26-8-122:~/projects/mysite$ gunicorn --bind 0:8000 config.wsgi:application [2022-10-21 09:16:40 +0900] [120314] [INFO] Starting gunicorn 20.1.0 [2022-10-21 09:16:40 +0900] [120314] [INFO] Listening at: http://0.0.0.0:8000 (120314) [2022-10-21 09:16:40 +0900] [120314] [INFO] Using worker: sync [2022-10-21 09:16:40 +0900] [120316] [INFO] Booting worker with pid: 120316 Not Found: /static/bootstrap.min.css Not Found: /static/style.css Not Found: /static/jquery-3.6.1.min.js Not Found: /static/bootstrap.min.js Not Found: /favicon.ico |
/home/ubuntu/projects/mysite 디렉터리에서 gunicorn --bind 0:8000 config.wsgi:application 명령을 수행했다.
--bind 0:8000 ▶ 8000번 포트로 WSGI 서버를 수행한다는 의미
config.wsgi:application ▶ WSGI 서버가 호출하는 WSGI 애플리케이션이 config/wsgi.py 파일의 application이라는 의미
서버가 잘 시작되는 것을 확인할 수 있을 것이다.
이렇게 보이는 이유는 Gunicorn이 정적 파일을 제대로 읽지 못했기 때문이다. 다시 말하지만 Gunicorn은 파이썬 프로그램을 실행하는 WSGI 서버이다. 그래서 정적 파일을 처리하는 데는 적합하지 않다. 정적 파일을 효과적으로 처리하는 웹 서버에 관련되서는 다음 기회에 알아보도록 하고, 우선 Gunicorn이 정상적으로 작동하는 것까지만 확인하자.
Gunicorn 소켓 사용
Gunicorn은 앞에서 본 것처럼 포트(8000)를 이용하여 서버를 띄운다. 하지만 Unix 계열 시스템에서는 포트로 서비스하기보다는 유닉스 소켓을 사용하는 것이 빠르고 효율적이다. Gunicorn을 유닉스 소켓으로 서비스하는 방법을 알아보자.
(1단계) Gunicorn 실행방법
다음과 같이 Gunicorn을 실행한다.
명령어> gunicorn --bind unix:/tmp/gunicorn.sock config.wsgi:application
(mysite) ubuntu@ip-172-26-8-122:~/projects/mysite$ gunicorn --bind unix:/tmp/gunicorn.sock config.wsgi:application [2022-10-21 09:27:36 +0900] [120335] [INFO] Starting gunicorn 20.1.0 [2022-10-21 09:27:36 +0900] [120335] [INFO] Listening at: unix:/tmp/gunicorn.sock (120335) [2022-10-21 09:27:36 +0900] [120335] [INFO] Using worker: sync [2022-10-21 09:27:36 +0900] [120337] [INFO] Booting worker with pid: 120337 |
명령어가 포트 방식으로 Gunicorn을 실행했을 때와 다르다. 기존에는 --bind 0:8000 와 같이 입력했지만 유닉스 소켓 방식은 --bind unix:/tmp/gunicorn.sock 와 같이 입력했다.
※ 유닉스 소켓 방식으로 Gunicorn 서버를 실행하면 단독으로 Gunicorn 서버에 접속하여 실행할 수 없다. 유닉스 소켓 방식으로 실행한 Gunicorn 서버는 Nginx와 같은 웹 서버에서 유닉스 소켓으로 WSGI 서버에 접속하도록 설정해야 한다.
Gunicorn 서비스로 등록하기
이번에는 AWS 서버에 Gunicorn을 서비스로 등록해 보자. 그 이유는 Gunicorn의 시작, 중지를 쉽게 하고, 또 AWS 서버를 다시 시작할 때 Gunicorn을 자동으로 실행하기 위해서이다. Gunicorn을 서비스로 등록하려면 환경변수 파일과 서비스 파일을 작성해야 한다.
(1단계) 환경변수 파일 생성
다음과 같이 Gunicorn이 사용하는 환경변수 파일을 생성하자. (nano 편집기 사용하여 작성)
(mysite) ubuntu@ip-172-26-8-122:~/projects/mysite$ cd /home/ubuntu/venvs/ (mysite) ubuntu@ip-172-26-8-122:~/venvs$ nano mysite.env |
# /home/ubuntu/venvs/mysite.env DJANGO_SETTINGS_MODULE=config.settings.prod |
내용을 입력하고 [Ctrl+O]를 눌러서 mysite.env 파일을 저장한 뒤 [Ctrl+X]를 눌러서 편집기를 종료한다.
(2단계) 서비스 파일 생성
그리고 /etc/systemd/system/ 디렉터리에서 다음과 같은 내용의 'mysite.service'라는 이름의 서비스 파일을 생성한다. 서비스 파일은 시스템 디렉터리에 저장해야 하므로 sudo nano mysite.service와 같이 관리자 권한으로 파일을 생성해야 한다.
▶ 관리자 권한으로 파일 열기: sudo nano mysite.service
(mysite) ubuntu@ip-172-26-8-122:~/venvs$ cd /etc/systemd/system/ (mysite) ubuntu@ip-172-26-8-122:/etc/systemd/system$ sudo nano mysite.service |
# /etc/systemd/system/mysite.service [Unit] Description=gnicorn deamon After=network.target [Service] User=ubuntu Grop=ubuntu WorkingDirectory=/home/ubuntu/projects/mysite EnvironmentFile=/home/ubuntu/venvs/mysite.env ExecStart=/home/ubuntu/venvs/mysite/bin/gunicorn \ --workers 2 \ --bind unix:/tmp/gnicorn.sock \ config.wsgi:application [Install] WantedBy=multi-user.target |
서비스 파일에 입력한 EnvironmentFile이 우리가 작성한 환경변수 파일을 불러오는 설정이다. --workers 2는 Gunicorn 프로세스를 2개 사용하라는 의미이다. (월 사용료 3.5달러인 사양의 서버는 프로세스 개수가 이 정도가 적당)
(3단계) 서비스 실행하고 등록
서비스 파일이 생성되면 다음 명령으로 서비스를 실행해 보자. 서비스 파일이 관리자 디렉터리에 있으므로 실행 역시 관리자 권한으로 실행해야 한다.
명령어> sudo systemctl start mysite.service
서비스가 잘 실행되었는지 확인하려면 sudo systemctl status mysite.service 명령을 실행하면 된다. 만약 다음과 같은 메시지가 나타나지 않는다면 /var/log/syslog/ 파일에서 오류 원인을 확인하고 수정해야 한다.
(mysite) ubuntu@ip-172-26-8-122:/etc/systemd/system$ sudo systemctl start mysite.service (mysite) ubuntu@ip-172-26-8-122:/etc/systemd/system$ sudo systemctl status mysite.service ● mysite.service - gnicorn deamon Loaded: loaded (/etc/systemd/system/mysite.service; disabled; vendor prese> Active: active (running) since Fri 2022-10-21 09:59:07 KST; 1min 12s ago Main PID: 120396 (gunicorn) Tasks: 3 (limit: 560) Memory: 63.1M CGroup: /system.slice/mysite.service ├─120396 /home/ubuntu/venvs/mysite/bin/python3 /home/ubuntu/venvs/> ├─120408 /home/ubuntu/venvs/mysite/bin/python3 /home/ubuntu/venvs/> └─120409 /home/ubuntu/venvs/mysite/bin/python3 /home/ubuntu/venvs/> Oct 21 09:59:07 ip-172-26-8-122 systemd[1]: Started gnicorn deamon. Oct 21 09:59:08 ip-172-26-8-122 gunicorn[120396]: [2022-10-21 09:59:08 +0900] [> Oct 21 09:59:08 ip-172-26-8-122 gunicorn[120396]: [2022-10-21 09:59:08 +0900] [> Oct 21 09:59:08 ip-172-26-8-122 gunicorn[120396]: [2022-10-21 09:59:08 +0900] [> Oct 21 09:59:08 ip-172-26-8-122 gunicorn[120408]: [2022-10-21 09:59:08 +0900] [> Oct 21 09:59:08 ip-172-26-8-122 gunicorn[120409]: [2022-10-21 09:59:08 +0900] [> lines 1-17/17 (END) |
(4단계) AWS 서버가 재시작될 때 자동으로 Gunicorn 실행
마지막으로 AWS 서버가 재시작될 때 Gunicorn을 자동으로 실행하도록 'enable' 옵션을 이용하여 서비스로 등록한다.
▶ 서비스 등록
명령어> sudo systemctl enable mysite.service
(mysite) ubuntu@ip-172-26-8-122:/etc/systemd/system$ sudo systemctl enable mysite.service Created symlink /etc/systemd/system/multi-user.target.wants/mysite.service → /etc/systemd/system/mysite.service. (mysite) ubuntu@ip-172-26-8-122:/etc/systemd/system$ |
▶ 서비스 종료
명령어> sudo systemctl stop mysite.service
(mysite) ubuntu@ip-172-26-8-122:/etc/systemd/system$ sudo systemctl stop mysite.service (mysite) ubuntu@ip-172-26-8-122:/etc/systemd/system$ |
▶ 서비스 재시작
명령어> sudo systemctl restart mysite.service
(mysite) ubuntu@ip-172-26-8-122:/etc/systemd/system$ sudo systemctl restart mysite.service (mysite) ubuntu@ip-172-26-8-122:/etc/systemd/system$ |
여기까지.
'개발자모드 > 혼자공부하는파이썬' 카테고리의 다른 글
[파이썬/장고#37] AWS 서버 환경에서 장고 Admin 사용하기 (ft. 슈퍼유저 생성, STATIC_ROOT 설정, 정적파일 복사) (0) | 2022.10.26 |
---|---|
[파이썬/장고#36] 웹 서버 Nginx 설치 및 설정 방법 (0) | 2022.10.24 |
[파이썬/장고#34] 웹 브라우저와 서버, 서비스 작동 방식 이해 (ft. 정적/동적 페이지 요청, WSGI 서버 작동 원리) (0) | 2022.10.20 |
[파이썬/장고#33] MobaXterm 사용방법 - AWS라이트세일 비밀키생성, MobaXterm 사용하여 서버 접속하기 (0) | 2022.10.19 |
[파이썬/장고#32] 개발/운영 환경 분리 방법(--settings 옵션 추가, alias 적용) (0) | 2022.10.18 |
댓글