wsgi.py
νμΌμ νΈμΆνλ €λ©΄ WSGI
μλ²κ° νμνλ€κ³ νμWSGI
μλ²μΈ Gunicorn
μ μ€μΉνκ³ μ¬μ©ν΄ λ³Ό κ²Tip!
Gunicorn
μ 'ꡬλμ½'μ΄λΌκ³ μ½μ
WSGI
μλ²μ μλμ°λ§₯μΌλ‘ Gunicorn
κ³Ό uwsgi
κ° μμuwsgi
κ° μμκ³ νΈμμ±μ Gunicorn
μ΄ μ’λ€" λΌλ μ견λ€μ΄ λ§μλλ° μμλ Gunicorn
μ μ±λ₯μ΄ λ§€μ° μ’μμ‘κΈ° λλ¬Έμ Gunicorn
μ μ¬μ©νλ μ¬λλ€μ΄ μ μ λ λ§μμ§κ³ μμGunicorn
μ κ°λ°μ΄ μλλΌ μ΄μμ μν λꡬμ΄λ―λ‘ λ‘컬 νκ²½μ μ€μΉν νμκ° μμGunicorn
μ μ€μΉ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
μ΄ μ μμ μΌλ‘ μ€νλλμ§ κ°λ¨νκ² μ€ν(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
μ μμμ λ³Έ κ²μ²λΌ ν¬νΈ(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
μ μλΉμ€λ‘ λ±λ‘ν΄ λ³Ό κ²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
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