ํ์ฐฝ ์ฅ๊ณ ๋ก ๊ฐ๋ฐ์ ํ๊ณ ๋๋ฉด runserver๋ก ํ ์คํธ๋ฅผ ํ์ง๋ง, ์ง์ ๋ฐฐํฌ๋ฅผ runserver๋ก ํ๊ฒ๋๋ฉด ๋ณด์, ์์ ์ฑ, ํจ์จ ๋ฑ ์ฌ๋ฌ ๋ฉด์์ ์ข์ง์๋ค.
- django.core.servers.basehttp
""" HTTP server that implements the Python WSGI protocol (PEP 333, rev 1.21). Based on wsgiref.simple_server which is part of the standard library since 2.5. This is a simple server for use in testing or debugging Django apps. It hasn't been reviewed for security issues. DON'T USE IT FOR PRODUCTION USE! """
์ฅ๊ณ ์์๋ runserver๋ฅผ ํ ์คํธ๋ ๋๋ฒ๊น ์ฉ์ผ๋ก ์ฐ๋ผ๊ณ ๊ถ์ฅํ๋ค.
๋ฐ๋ผ์, WS์ธ NGINX์ uWSGI๋ฅผ ํตํด์ ์ฐ๋์์ผ ๋ณด๋ ค๊ณ ํ๋ค.
NGINX์ ๊ฐ์ WS๋ค์ python ํ๋ก๊ทธ๋จ์ ํด์ํ ๋ฅ๋ ฅ์ด ์๋ค.
python๊ณผ๊ฐ์ ์ธ์ด๋ฅผ ํด์ํ ์ ์๋ ์ธํฐํ์ด์ค ์ญํ ์ ํ๋ CGI๊ฐ ํ์ํ๋ค.
์ด ๋ WSGI๋ ์น ์์ฒญ์ ์ฒ๋ฆฌํ๋ ํ์ค ์ธํฐํ์ด์ค์ด๋ค.
๋ณดํต์ WS + CGI + Django
์์ WSGI๋ก gunicorn์ด๋ uWSGI๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ ๊ฒ ๊ฐ์๋ฐ,
gunicorn๋ณด๋ค uWSGI๊ฐ ํจ์จ์ ์ธ ๋ฉด์์ ์ข์ ๊ฒ ๊ฐ์์ uWSGI๋ฅผ ์ด์ฉํด๋ณด๋ ค๊ณ ํ๋ค.
1. uWSGI ์ค์
uWSGI ์ค์น
$ yum install -y gcc
$ pip install uwsgi
yum ์ ์ด์ฉํด์ gcc๋ฅผ ๋จผ์ ์ค์นํ๊ณ , pip์ ์ด์ฉํด์ uWSGI๋ฅผ ์ค์นํ ์ ์๋ค.
uWSGI ์ค์
์ค์น๋ฅผ ์๋ฃํ๋ค๋ฉด, ์ค์ ํ์ผ์ ๋ง๋ค์ด์ค๋ค.
$ mkdir -p /etc/uwsgi/sites
$ vi /etc/uwsgi/sites/django-project.ini
์ ํ์ผ๋ช ์ 'django-project'์๋ ๋ด๊ฐ ์์ํ ์ฅ๊ณ ํ๋ก์ ํธ์ ์ด๋ฆ์ ๋ฃ์ด๋๋๋ก ํ์.
[uwsgi]
project = django-project #ํ๋ก์ ํธ ๋ช
username = ec2-user #์ฌ์ฉ์
base = / #base ๋๋ ํ ๋ฆฌ
### Django Settings
# base directory
chdir = /home/ec2-user/django-project #์ฅ๊ณ ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ
# python path
home = /usr #ํ์ด์ฌ ํ ๋๋ ํ ๋ฆฌ
# virtualenv path
# virtualenv = /home/env/test #๊ฐ์ํ๊ฒฝ. ์๋ค๋ฉด ์ค์
# wsgi.py path
module = config.wsgi:application #์ฅ๊ณ ํ๋ก์ ํธ ๋ด wsgi
master = true
processes = 5 #ํ๋ก์ธ์ค ์
uid = ec2-user
socket = /run/uwsgi/django-project.sock #์์ผ ์ ๋ฌ ์์น
chown-socket = root:nginx
chmod-socket = 660
vacuum = true
logto = /var/log/uwsgi/django-project.log
์๋ฒ ํ๊ฒฝ์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ์ง๋ง, ๋๋ ์ด๋ ๊ฒ ์์ฑํด์ผ ์๋ํ๋ค.
ํ์ด์ฌ ํ ๋๋ ํ ๋ฆฌ, ์ฅ๊ณ module ์ด๋ฆ ๋ชจ๋ ์ผ์นํด์ผ ์ ์ ์๋ํ๋ค.
โผ๏ธ home์ ๊ฒฝ์ฐ python ํ ๋๋ ํ ๋ฆฌ๋ก ์๋์ผ๋ก ์ค์ ๋๋ค.
๋ง์ฝ ํ์ด์ฌ์ด /usr/bin/python ๊ฒฝ๋ก์ ์๋ค๋ฉด, /usr ๋ง ์ค์ ํด์ฃผ๋ฉด uwsgi๊ฐ ๋์๊ฐ๋ฉด์ ์๋์ผ๋ก ๋ค์ /bin/python์ ๋ถ์ธ๋ค.
โผ๏ธ module์ ๊ฒฝ์ฐ ์ฅ๊ณ ํ๋ก์ ํธ์ wsgi.py๋ฅผ ๋งคํํด์ฃผ๋ฉด ๋๋ค.
๋ด ํ๋ก์ ํธ๋ config ์ฑ ๋ด์ wsgi.py๊ฐ ์์๊ธฐ ๋๋ฌธ์ config.wsgi:application๋ก ์ค์ ํด์คฌ๋ค.
uWSGI๋ฅผ ๊ตฌ๋ํ๋ ์ฌ์ด ๋์ค๋ ๋ก๊ทธ๋ logto์ ๋๋ ํ ๋ฆฌ๋ก ๊ฐ๊ฒ ๋๋ค. ํด๋น ๋๋ ํ ๋ฆฌ๋ฅผ ๋ง๋ค์ด ์ฃผ์ด์ผ ํ๋ค.
mkdir -p /var/log/uwsgi
uWSGI ์๋น์ค ์์ฑ
uWSGI๊ฐ ์๋น์ค๋ก ์๋ํ ์ ์๊ฒ ์ค์ ํด์ค์ผ ํ๋ค.
emperor๋ชจ๋์์ ์คํํด์ค์ผ ํ๋ค.
$ vi /etc/systemd/system/uwsgi.service
[Unit]
Description=uWSGI service
[Service]
ExecStartPre=/bin/mkdir -p /run/uwsgi
ExecStartPre=/bin/chown root:nginx /run/uwsgi
ExecStart=/home/ec2-user/.local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
$ systemctl daemon-reload
systemctl start uwsgi
์๋น์ค๋ฅผ ๋ฆฌ๋ก๋ํ๊ณ ์์ํ๋ฉด ๋๋ค.
2. NGINX ์ค์
์ด์ ์ค์นํด๋ NGINX๋ฅผ ์ค์ ํด์ฃผ์.
vi /etc/nginx/nginx.conf
server{
listen 8000;
server_name {์๋ฒ ๋๋ฉ์ธ ๋๋ IP};
location / {
include uwsgi_params;
uwsgi_pass unix:/run/uwsgi/django-proejct.sock;
}
}
์๋ฒ์ ๋ง๊ฒ ์ค์ ํด์ฃผ๋ฉด ๋๋ค.
uwsgi_params๋ ํ๋กํ ์ฝ ๋ณ์์ด๋ค.
ํด๋น ๋ณ์๋ฅผ include ํด์ค์ผ๋ก์จ ์น ์๋ฒ๊ฐ ์ ๋ฌํ๋ ๋ณ์๋ค์ uWSGI์๋ฒ๊ฐ ๋์ ์ผ๋ก ์คํ๋ ์ ์๋ค.
uwsig_pass๋ uwsgiํ๋กํ ์ฝ์ ์ด์ฉํด์ uWSGI์ ์๋ฒ ํธ๋ํฝ์ ์ ๋ฌํ๊ณ , iniํ์ผ์์ ์์ฑํ๋ unix socket์ ์ฌ์ฉํ๋ค.
3. Selinux
vi /etc/sysconfig/selinux
SELINUX=disabled
reboot
well-knownํฌํธ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ํด์ ํด์ค๋ค.
์๋๋ Selinux๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ถ๋ถ๊ถํ์ ์ฃผ๋๊ฒ ์ข๋ค.
4. ์คํ
systemctl start uwsgi
systemctl enable uwsgi
uwsgi ์๋ฒ๋ฅผ ์คํํ๊ณ , 8000ํฌํธ๋ก ์ ์ํ๋ฉด ์ฅ๊ณ ์ฑ ํ๋ก์ ํธ๊ฐ ์คํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
'BackEnd > Django' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Django] debug = False & uwsgi Protocol ์ฌ์ฉ ์ static file ๋ก๋ํ๊ธฐ (0) | 2023.09.02 |
---|