Django: Gunicorn

GreenBeanΒ·2022λ…„ 1μ›” 14일
0
post-thumbnail

Gunicorn

점프 투 μž₯κ³ : 4-09 Gunicorn

  • 이전 μž₯μ—μ„œ μ›Ή μ„œλ²„μ—μ„œ 파이썬 μž₯κ³  μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ κ΅¬ν˜„λ˜μ–΄ μžˆλŠ” wsgi.py νŒŒμΌμ„ ν˜ΈμΆœν•˜λ €λ©΄ WSGI μ„œλ²„κ°€ ν•„μš”ν•˜λ‹€κ³  ν–ˆμŒ
  • μ΄λ²ˆμ—λŠ” WSGI μ„œλ²„μΈ Gunicorn을 μ„€μΉ˜ν•˜κ³  μ‚¬μš©ν•΄ λ³Ό 것

Tip! Gunicorn은 'κ΅¬λ‹ˆμ½˜'이라고 읽음

  • WSGI μ„œλ²„μ˜ μ–‘λŒ€μ‚°λ§₯으둜 Gunicornκ³Ό uwsgiκ°€ 있음
    • κ³Όκ±°μ—λŠ” "μ„±λŠ₯은 uwsgiκ°€ μ•žμ„œκ³  νŽΈμ˜μ„±μ€ Gunicorn이 μ’‹λ‹€" λΌλŠ” μ˜κ²¬λ“€μ΄ λ§Žμ•˜λŠ”λ° μš”μƒˆλŠ” Gunicorn의 μ„±λŠ₯이 맀우 μ’‹μ•„μ‘ŒκΈ° λ•Œλ¬Έμ— Gunicorn을 μ‚¬μš©ν•˜λŠ” μ‚¬λžŒλ“€μ΄ 점점 더 λ§Žμ•„μ§€κ³  있음

Gunicorn μ„€μΉ˜

  • Gunicorn은 개발이 μ•„λ‹ˆλΌ μš΄μ˜μ„ μœ„ν•œ λ„κ΅¬μ΄λ―€λ‘œ 둜컬 ν™˜κ²½μ— μ„€μΉ˜ν•  ν•„μš”κ°€ μ—†μŒ
    • μ„œλ²„ ν™˜κ²½μ— Gunicorn을 μ„€μΉ˜
    • AWS μ„œλ²„μ— μ ‘μ†ν•œ λ’€ 가상 ν™˜κ²½μ—μ„œ pip 을 μ΄μš©ν•˜μ—¬ Gunicorn을 μ„€μΉ˜
(mysite) ubuntu@ip-172-26-12-247:~/projects/mysite$ pip install gunicorn
Collecting gunicorn
  Downloading https://files.pythonhosted.org/packages/69/ca/926f7cd3a2014b16870086b2d0fdc84a9e49473c68a8dff8b57f7c156f43/gunicorn-20.0.4-py2.py3-none-any.whl (77kB)
    (... μƒλž΅ ...)
Requirement already satisfied: setuptools>=3.0 in /home/ubuntu/venvs/mysite/lib/python3.6/site-packages (from gunicorn)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.0.4

Gunicorn ν…ŒμŠ€νŠΈ

  • Gunicorn이 μ •μƒμ μœΌλ‘œ μ‹€ν–‰λ˜λŠ”μ§€ κ°„λ‹¨ν•˜κ²Œ μ‹€ν–‰
(mysite) ubuntu@ip-172-26-12-247:~$ cd ~/projects/mysite/
(mysite) ubuntu@ip-172-26-12-247:~/projects/mysite$ gunicorn --bind 0:8000 config.wsgi:application
[2020-04-17 00:59:12 +0000] [32356] [INFO] Starting gunicorn 20.0.4
[2020-04-17 00:59:12 +0000] [32356] [INFO] Listening at: http://0.0.0.0:8000 (32356)
[2020-04-17 00:59:12 +0000] [32356] [INFO] Using worker: sync
[2020-04-17 00:59:12 +0000] [32359] [INFO] Booting worker with pid: 32359
  • /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이 정적 νŒŒμΌλ“€μ„ ν•΄μ„ν•˜μ§€ λͺ»ν–ˆκΈ° λ•Œλ¬Έ
    • ν•΄λ‹Ή νŽ˜μ΄μ§€λŠ” bootstrap.min.css, bootstrap.min.js, style.css λ“±μ˜ 정적 νŒŒμΌμ„ ν•„μš”λ‘œ ν•˜μ§€λ§Œ Gunicorn은 동적 νŽ˜μ΄μ§€ μš”μ²­λ§Œ μ²˜λ¦¬ν•  수 있기 λ•Œλ¬Έμ— μœ„μ™€ 같이 ν‘œμ‹œλ¨

Tip! Gunicorn κ΄€λ ¨ λͺ…λ Ήμ–΄

  • μ‹€ν–‰
    • gunicorn --bind 0.0.0.0:8000 [Django ν”„λ‘œμ νŠΈλͺ…].wsgi &
  • 터미널을 μ’…λ£Œ 후에도 μ‹€ν–‰
    • nohup gunicorn --bind 0.0.0.0:8000 [Django ν”„λ‘œμ νŠΈλͺ…].wsgi &
    • μ΄λ ‡κ²Œ μ„œλ²„λ₯Ό μ‹€ν–‰ν•˜λ©΄ SSH μ„Έμ…˜μ΄ λŠμ–΄μ Έλ„ μ„œλ²„κ°€ 계속 μΌœμ ΈμžˆλŠ” μƒνƒœκ°€ 됨
  • ν˜„μž¬ μ‹€ν–‰ 되고 μžˆλŠ” ν”„λ‘œμ„ΈμŠ€ 확인
    • ps -ef | grep python
  • μ„œλ²„ μ’…λ£Œ
    • kill [pid 번호]

Tip! nohup μ΄λž€?

  • nohup 은 no hang up의 μ•½μžλ‘œ λ¦¬λˆ…μŠ€μ™€ μœ λ‹‰μŠ€μ—μ„œ μ‰˜μŠ€ν¬λ¦½νŠΈνŒŒμΌ(*.sh)을 데λͺ¬ ν˜•νƒœλ‘œ μ‹€ν–‰μ‹œν‚€λŠ” ν”„λ‘œκ·Έλž¨
  • nohup μ‚¬μš© μ‹œ 디렉토리에 nohup.outμ΄λΌλŠ” 파일이 μƒμ„±λ˜λ©° λ‘œκ·Έκ°€ λ‚¨μŒ

Gunicorn μ†ŒμΌ“

  • Gunicorn은 μ•žμ—μ„œ λ³Έ κ²ƒμ²˜λŸΌ 포트(8000)λ₯Ό μ΄μš©ν•˜μ—¬ μ„œλ²„λ₯Ό λ„μšΈμˆ˜ 있음

    • ν•˜μ§€λ§Œ Unix 계열 μ‹œμŠ€ν…œμ—μ„œλŠ” 포트둜 μ„œλΉ„μŠ€ν•˜κΈ°λ³΄λ‹€λŠ” μœ λ‹‰μŠ€ μ†ŒμΌ“(Unix socket)을 μ‚¬μš©ν•˜λŠ” 것이 λΉ λ₯΄κ³  효율적
  • μ•„λž˜μ™€ 같이 Gunicorn μ‹€ν–‰

(mysite) ubuntu@ip-172-26-12-247:~/projects/mysite$ gunicorn --bind unix:/tmp/gunicorn.sock config.wsgi:application
[2020-04-17 01:14:51 +0000] [32392] [INFO] Starting gunicorn 20.0.4
[2020-04-17 01:14:51 +0000] [32392] [INFO] Listening at: unix:/tmp/gunicorn.sock (32392)
[2020-04-17 01:14:51 +0000] [32392] [INFO] Using worker: sync
[2020-04-17 01:14:51 +0000] [32395] [INFO] Booting worker with pid: 32395
  • λͺ…령을 μœ μ‹¬νžˆ μ‚΄νŽ΄λ³΄λ©΄ 포트 λ°©μ‹μœΌλ‘œ Gunicorn을 μ‹€ν–‰ν–ˆμ„ λ•Œμ™€ 닀름
    • 즉, --bind unix:/tmp/gunicorn.sock 뢀뢄이 닀름
    • κΈ°μ‘΄μ—λŠ” --bind 0:8000와 같이 μž…λ ₯ν–ˆμ§€λ§Œ μœ λ‹‰μŠ€ μ†ŒμΌ“ 방식은 --bind unix:/tmp/gunicorn.sock와 같이 μž…λ ₯

Tip!

  • μœ λ‹‰μŠ€ μ†ŒμΌ“ λ°©μ‹μœΌλ‘œ Gunicorn μ„œλ²„λ₯Ό μ‹€ν–‰ν•˜λ©΄ λ‹¨λ…μœΌλ‘œ Gunicorn μ„œλ²„μ— μ ‘μ†ν•˜μ—¬ μ‹€ν–‰ν•  수 μ—†μŒ
  • μœ λ‹‰μŠ€ μ†ŒμΌ“ λ°©μ‹μœΌλ‘œ μ‹€ν–‰ν•œ Gunicorn μ„œλ²„λŠ” Nginx와 같은 μ›Ή μ„œλ²„μ—μ„œ μœ λ‹‰μŠ€ μ†ŒμΌ“μœΌλ‘œ WSGI μ„œλ²„μ— μ ‘μ†ν•˜λ„λ‘ μ„€μ •ν•΄μ•Ό 함

Gunicorn μ„œλΉ„μŠ€

  • μ΄λ²ˆμ—λŠ” AWS μ„œλ²„μ— Gunicorn을 μ„œλΉ„μŠ€λ‘œ 등둝해 λ³Ό 것
    • Gunicorn의 μ‹œμž‘, 쀑지λ₯Ό μ‰½κ²Œ ν•˜κ³ , 또 AWS μ„œλ²„λ₯Ό λ‹€μ‹œ μ‹œμž‘ν•  λ•Œ Gunicorn을 μžλ™μœΌλ‘œ μ‹€ν–‰ν•  수 있음
    • Gunicorn을 μ„œλΉ„μŠ€λ‘œ λ“±λ‘ν•˜λ €λ©΄ ν™˜κ²½ λ³€μˆ˜ 파일과 μ„œλΉ„μŠ€ νŒŒμΌμ„ μž‘μ„±ν•΄μ•Ό 함

ν™˜κ²½ λ³€μˆ˜ 파일

  • Gunicorn이 μ„œλΉ„μŠ€λ‘œ 싀행될 κ²½μš°μ—λŠ” aliasλ‚˜ mysite.sh에 μ˜ν•΄ μƒμ„±λ˜λŠ” DJANGO_SETTINGS_MODULE ν™˜κ²½λ³€μˆ˜κ°€ μƒμ„±λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— ν™˜κ²½λ³€μˆ˜ νŒŒμΌμ„ 톡해 생성해 μ£Όμ–΄μ•Ό 함
  • μ•„λž˜κ³Ό 같이 Gunicorn의 ν™˜κ²½ λ³€μˆ˜ νŒŒμΌμ„ μž‘μ„±
    • 파일의 λ‚΄μš©μ€ ν•œμ€„λ§Œ μž‘μ„±ν•˜λ©΄ 됨
# [파일λͺ…: /home/ubuntu/venvs/mysite.env]

DJANGO_SETTINGS_MODULE=config.settings.prod

μ„œλΉ„μŠ€ 파일

  • /etc/systemd/system/ 디렉터리에 μ•„λž˜μ™€ 같은 λ‚΄μš©μ˜ mysite.serviceλΌλŠ” μ΄λ¦„μ˜ μ„œλΉ„μŠ€ νŒŒμΌμ„ 생성
    • λ‹€λ§Œ μ„œλΉ„μŠ€ νŒŒμΌμ€ μ‹œμŠ€ν…œ 디렉터리에 μ €μž₯ν•΄μ•Ό ν•˜λ―€λ‘œ sudo nano mysite.service와 같이 κ΄€λ¦¬μž κΆŒν•œμœΌλ‘œ νŒŒμΌμ„ 생성해야 함
# [파일λͺ…: /etc/systemd/system/mysite.service]

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

[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/projects/mysite
EnvironmentFile=/home/ubuntu/venvs/mysite.env
ExecStart=/home/ubuntu/venvs/mysite/bin/gunicorn \
        --workers 2 \
        --bind unix:/tmp/gunicorn.sock \
        config.wsgi:application

[Install]
WantedBy=multi-user.target
  • μ„œλΉ„μŠ€ 파일의 EnvironmentFile ν•­λͺ©μ΄ μš°λ¦¬κ°€ μž‘μ„±ν•œ ν™˜κ²½ λ³€μˆ˜ νŒŒμΌμ„ λΆˆλŸ¬μ˜€λŠ” μ„€μ •
  • --worker 2λŠ” Gunicorn ν”„λ‘œμ„ΈμŠ€λ₯Ό 2개 μ‚¬μš©ν•˜λΌλŠ” 의미

μ„œλΉ„μŠ€ μ‹€ν–‰κ³Ό 등둝

  • μ„œλΉ„μŠ€ νŒŒμΌμ„ μƒμ„±ν•œ ν›„ λ‹€μŒ λͺ…λ ΉμœΌλ‘œ μ„œλΉ„μŠ€λ₯Ό μ‹€ν–‰
    • μ„œλΉ„μŠ€ 파일이 κ΄€λ¦¬μž 디렉터리에 μžˆμœΌλ―€λ‘œ μ‹€ν–‰ μ—­μ‹œ κ΄€λ¦¬μž κΆŒν•œμœΌλ‘œ μ‹€ν–‰ν•΄μ•Ό 함
(mysite) ubuntu@ip-172-26-14-223:/etc/systemd/system$ sudo systemctl start mysite.service
  • μ„œλΉ„μŠ€κ°€ 잘 μ‹€ν–‰λλŠ”μ§€ ν™•μΈν•˜λ €λ©΄ sudo systemctl status mysite.service λͺ…령을 μ‹€ν–‰ν•˜λ©΄ 됨
    • λ§Œμ•½ μ•„λž˜μ™€ 같은 λ©”μ‹œμ§€κ°€ λ‚˜νƒ€λ‚˜μ§€ μ•ŠμœΌλ©΄ /var/log/syslog νŒŒμΌμ—μ„œ 였λ₯˜ 원인을 ν™•μΈν•˜κ³  μˆ˜μ •ν•΄μ•Ό 함
(mysite) ubuntu@ip-172-26-12-247:/etc/systemd/system$ sudo systemctl status mysite.service
● mysite.service - gunicorn daemon
   Loaded: loaded (/etc/systemd/system/mysite.service; disabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-04-23 12:12:27 UTC; 1s ago
 Main PID: 26513 (gunicorn)
    Tasks: 3 (limit: 547)
   CGroup: /system.slice/mysite.service
           β”œβ”€26513 /home/ubuntu/venvs/mysite/bin/python3 /home/ubuntu/venvs/mysite/bin/gunicorn --workers 2 --bind unix:/tmp/gunicorn.sock config.wsgi:application
           β”œβ”€26534 /home/ubuntu/venvs/mysite/bin/python3 /home/ubuntu/venvs/mysite/bin/gunicorn --workers 2 --bind unix:/tmp/gunicorn.sock config.wsgi:application
           └─26536 /home/ubuntu/venvs/mysite/bin/python3 /home/ubuntu/venvs/mysite/bin/gunicorn --workers 2 --bind unix:/tmp/gunicorn.sock config.wsgi:application

Apr 23 12:12:27 ip-172-26-12-247 systemd[1]: Started gunicorn daemon.
Apr 23 12:12:28 ip-172-26-12-247 gunicorn[26513]: [2020-04-23 12:12:28 +0000] [26513] [INFO] Starting gunicorn 20.0.4
Apr 23 12:12:28 ip-172-26-12-247 gunicorn[26513]: [2020-04-23 12:12:28 +0000] [26513] [INFO] Listening at: unix:/tmp/gunicorn.sock (26513)
Apr 23 12:12:28 ip-172-26-12-247 gunicorn[26513]: [2020-04-23 12:12:28 +0000] [26513] [INFO] Using worker: sync
Apr 23 12:12:28 ip-172-26-12-247 gunicorn[26513]: [2020-04-23 12:12:28 +0000] [26534] [INFO] Booting worker with pid: 26534
Apr 23 12:12:28 ip-172-26-12-247 gunicorn[26513]: [2020-04-23 12:12:28 +0000] [26536] [INFO] Booting worker with pid: 26536
  • λ§ˆμ§€λ§‰μœΌλ‘œ AWS μ„œλ²„κ°€ λ‹€μ‹œ μ‹œμž‘λ  λ•Œ Gunicorn을 μžλ™μœΌλ‘œ μ‹€ν–‰ν•˜λ„λ‘ enable μ˜΅μ…˜μ„ μ΄μš©ν•˜μ—¬ μ„œλΉ„μŠ€λ‘œ 등둝
(mysite) ubuntu@ip-172-26-12-247:/etc/systemd/system$ sudo systemctl enable mysite.service

Tip! μ„œλΉ„μŠ€ μ’…λ£Œμ™€ μž¬μ‹œμž‘

  • μ„œλΉ„μŠ€λ₯Ό μ’…λ£Œν•˜λ €λ©΄ λ‹€μŒ λͺ…령을 μˆ˜ν–‰
    • $ sudo systemctl stop mysite.service
  • μ„œλΉ„μŠ€λ₯Ό λ‹€μ‹œ μ‹œμž‘ν•˜λ €λ©΄ λ‹€μŒ λͺ…령을 μˆ˜ν–‰
    • $ sudo systemctl restart mysite.service
profile
🌱 Backend-Dev | hwaya2828@gmail.com

0개의 λŒ“κΈ€