๐Ÿ—“ Django + Nginx + Gunicorn

may_soouuยท2020๋…„ 10์›” 27์ผ
10

๊ธฐ์—…ํ˜‘์—…

๋ชฉ๋ก ๋ณด๊ธฐ
1/2
post-thumbnail

๊ธฐ์—…ํ˜‘์—… ์ง„ํ–‰ํ•˜๋ฉด์„œ, ๊ฑฐ์˜ ์ดํ‹€์— ๊ฑธ์ณ์„œ ์ง„ํ–‰ํ–ˆ๋˜ ์ž‘์—…์ด์—ˆ๋‹ค.
์œ„์ฝ”๋“œ์—์„œ ํ”„๋กœ์ ํŠธ ํ•  ๋•Œ๋Š”, ํ„ฐ๋ฏธ๋„์—์„œ runserver ํ˜น์€ gunicorn์œผ๋กœ ์„œ๋ฒ„ ์—ฌ๋Š” ๊ฒƒ ๊นŒ์ง€๊ฐ€ ์ „๋ถ€์˜€๋Š”๋ฐ ์ฒ˜์Œ ์ ‘ํ•˜๋Š” nginx๋ฅผ ํ•˜๊ฒŒ ๋˜๋‹ˆ ๋งŽ์€ ์‹œํ–‰์ฐฉ์˜ค๊ฐ€ ์žˆ์—ˆ๋‹ค.

๐Ÿ“Œ ๋‚˜์˜ ํ™˜๊ฒฝ

  • mac os
  • miniconda ์‚ฌ์šฉ
  • Django 3.1.1
  • python 3.8

๐Ÿ“Œ ํ˜„์žฌ ์ƒํ™ฉ
1. ๋กœ์ปฌ์— Django ์ดˆ๊ธฐ ์„ธํŒ… ๋ฐ views ์ž‘์„ฑ
2. AWS EC2 ์ƒ์„ฑ
3. ํ„ฐ๋ฏธ๋„์—์„œ AWS ์ ‘์†ํ•ด์„œ gunicorn ๋ฐ nginx ์„ค์ •ํ•ด์„œ ์„œ๋ฒ„ ์—ด๊ธฐ
4. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ํ•„์š”๊ฐ€ ์—†๋Š” ์ž‘์—…์ด๋ผ ๋”ฐ๋กœ ์„ค์ •ํ•˜์ง€ ์•Š์•˜๋‹ค

๋ฏธ๋‹ˆ์ฝ˜๋‹ค & ์žฅ๊ณ  ๊ธฐ์ดˆ์…‹ํŒ… & ํŒŒ์ด์ฌ์ด ์ค€๋น„๋˜์–ด ์žˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

1. AWS EC2 ์ƒ์„ฑ

1-1. AWS ์‚ฌ์ดํŠธ ์ ‘์†

  1. https://aws.amazon.com/ko/ ์ ‘์† + ํšŒ์›๊ฐ€์ž… + ๋กœ๊ทธ์ธ

  2. ์„œ๋น„์Šค > EC2 > ์ธ์Šคํ„ด์Šค ์‹œ์ž‘

  3. Ubuntu Server 18.04 LTS (HVM), SSD Volume Type ์„ ํƒ

  4. t2 / t2 micro(ํ”„๋ฆฌํ‹ฐ์–ด ์‚ฌ์šฉ๊ฐ€๋Šฅ) ์„ ํƒ > ๋‹ค์Œ:์ธ์Šคํ„ด์Šค ์„ธ๋ถ€ ์ •๋ณด ๊ตฌ์„ฑ

  5. ์•„๋ž˜ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด ๊ตฌ์„ฑ

  • ์„œ๋ธŒ๋„ท์˜ ๊ฒฝ์šฐ 2b๋กœ ํ•ด๋„ ๋ฌด๊ด€ํ•จ
  1. ์Šคํ† ๋ฆฌ์ง€ ์ถ”๊ฐ€์—์„œ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ˆ˜์ •์—†์ด ๋‹ค์Œ:ํƒœ๊ทธ ์ถ”๊ฐ€ ๋ฒ„ํŠผ ํด๋ฆญ
  2. ํƒœ๊ทธ ์ถ”๊ฐ€ ๋ฒ„ํŠผ
    • ๋‹ค์–‘ํ•œ ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ˆ˜์ •๋„ ๊ฐ€๋Šฅํ•˜๋‹ค
    • ๋‚˜์˜ ๊ฒฝ์šฐ : ํ˜‘์—…์ค‘์ธ ํšŒ์‚ฌ์˜ ํ”„๋กœ์ ํŠธ๋ผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์„ฑํ–ˆ๋‹ค.
      - ํ‚ค : ํšŒ์‚ฌ๋ช…_project
      - ๊ฐ’ : first_project
  1. ํ•˜๋‹จ ๊ทœ์น™ ์ถ”๊ฐ€ ๋ฒ„ํŠผ
    - ์œ ํ˜• : ์‚ฌ์šฉ์ž์ง€์ • TCP๊ทœ์น™
    - ํฌํŠธ๋ฒ”์œ„ : 8000
    - ์†Œ์Šค : ์œ„์น˜ ๋ฌด๊ด€

    ๊ฒ€ํ†  ๋ฐ ์‹œ์ž‘๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.

1-2. ํ„ฐ๋ฏธ๋„์—์„œ AWS ์„ค์ •ํ•˜๊ธฐ

์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ, ํ‚คํŽ˜์–ด ํŒŒ์ผ์„ ๋‹ค์šด ๋ฐ›์•„ ๋†“์€ ๊ฒŒ ์žˆ์„ ๊ฒƒ์ด๋‹ค.
ssh๋กœ ์ ‘์†์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ๋” pemํŒŒ์ผ์˜ ๊ถŒํ•œ์„ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค.

# ํ„ฐ๋ฏธ๋„์„ ํ‚ค๊ณ , pem ํŒŒ์ผ์„ ๋ฐ›์•„ ๋†“์€ ์œ„์น˜๋กœ ๊ฐ„๋‹ค
# ex. aws ๋ผ๋Š” ํŒŒ์ผ์— ์žˆ๋‹ค๋ฉด, cd aws
# ํ•ด๋‹น ์œ„์น˜์—์„œ ์•„๋ž˜์˜ ๋ช…๋ น์–ด ์ž…๋ ฅ

$ chmod 400 ํ‚คํŽ˜์–ดํŒŒ์ผ์ด๋ฆ„.pem

๊ถŒํ•œ์„ ๋ฐ”๊ฟ”์คฌ์œผ๋ฉด, ssh ์ ‘์†์„ ํ•ด๋ณด์ž

$ ssh -i ํ‚คํŽ˜์–ดํŒŒ์ผ์ด๋ฆ„.pem ubuntu@๊ณต์ธIP
ex. ssh -i proejct.pem ubuntu@33.121.14.11

33.121.14.11 ์€ ์˜ˆ๋กœ ๋“  ๊ฒƒ์ด๋ฉฐ, ๊ณต์ธ IP๋Š” AWS์— EC2 ๋กœ ๊ฐ€๋ฉด ์œ„์—์„œ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ธ์Šคํ„ด์Šค๋ฅผ ํด๋ฆญํ•˜๋ฉด ํผ๋ธ”๋ฆญ IPv4 ์ฃผ์†Œ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด ์ฃผ์†Œ๋ฅผ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

2. ๋ฏธ๋‹ˆ์ฝ˜๋‹ค ์„ค์ •ํ•˜๊ธฐ

2-1. ๋ฏธ๋‹ˆ์ฝ˜๋‹ค๋กœ ๊ฐ€์ƒํ™˜๊ฒฝ ์„ค์ •ํ•˜๊ธฐ

์šฐ๋ถ„ํˆฌ๋ผ๋Š” ์ƒˆ๋กœ์šด ํ™˜๊ฒฝ์ด ํ„ฐ๋ฏธ๋„์—์„œ ์—ด๋ฆฐ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋‚ด๊ฐ€ ๋กœ์ปฌ์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ๋“ค์€ ์šฐ๋ถ„ํˆฌ ํ™˜๊ฒฝ์— ์—†๋Š” ์ƒํƒœ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฏธ๋‹ˆ์ฝ˜๋‹ค ๋ฐ ํ•„์š”ํ•œ ํˆด๋“ค์„ ์„ค์น˜ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

  • ๋ฏธ๋‹ˆ์ฝ˜๋‹ค ํ™ˆํŽ˜์ด์ง€ ์ ‘์† https://docs.conda.io/en/latest/miniconda.html
  • linux installers ์—์„œ ๋งํฌ ์œ„์— ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ > ๋งํฌ ์ฃผ์†Œ ๋ณต์‚ฌ

# ํ„ฐ๋ฏธ๋„๋กœ ๋Œ์•„์™€์„œ ์•„๋ž˜ ๋‚ด์šฉ ์ž…๋ ฅ(๋งํฌ๋Š” ์œ„์˜ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ ๋งํฌ ๋ณต์‚ฌ ํ•œ ๊ฒƒ)
$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

#์‹คํ–‰๊ถŒํ•œ ์ฃผ๊ธฐ
$ chmod +x Miniconda3-latest-Linux-x86_64.sh

#๋ฏธ๋‹ˆ์ฝ˜๋‹ค ์‹คํ–‰์‹œ์ผœ์„œ ์„ค์น˜ํ•˜๊ธฐ
$ ./Miniconda3-latest-Linux-x86_64.sh
# ์—”ํ„ฐ์—”ํ„ฐ์—”ํ„ฐ ๊ณ„์† ๋ˆ„๋ฅด๊ณ  ๋‹ค yesํ•ด์„œ ์„ค์น˜ํ•˜๊ธฐ

# bash shell ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ด์„œ ๋กœ์ปฌ ํ™˜๊ฒฝ๊ณผ ๊ฐ™๊ฒŒ ์„ค์ •ํ•˜๊ธฐ
$ source .bashrc
# ์ด๋ ‡๊ฒŒ ์ž…๋ ฅํ•˜๋ฉด ๋ช…๋ น๋ผ์ธ ์ œ์ผ ์•ž์— (base) ์ƒ์„ฑ

# ํŒจํ‚ค์ง€ ์—…๋ฐ์ดํŠธ ๋ฐ ์—…๊ทธ๋ ˆ์ด๋“œ
$ sudo apt-get update
$ sudo apt-get upgrade

# mysql ํด๋ผ์ด์–ธํŠธ ์„ค์น˜์‹œ ์—๋Ÿฌ ์•ˆ๋‚˜๊ฒŒ ํ•˜๋ ค๋ฉด gcc ์„ค์น˜ํ•ด์•ผํ•จ
$ sudo apt-get install gcc
$ sudo apt-get install libmysqlclient dev
# ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ ์œ„์˜ ๋‘๊ฐœ๋Š” ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค

# ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ
$ conda create -n project python=3.8
# project๋Š” ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์€ ๊ฐ€์ƒํ™˜๊ฒฝ ์ด๋ฆ„์ด๋ผ, ์›ํ•˜๋Š” ๊ฑธ๋กœ ํ•˜๋ฉด ๋œ๋‹ค.

# ๊ฐ€์ƒํ™˜๊ฒฝ ํ™œ์„ฑํ™”
$ conda activate project

3. Django ์„ธํŒ…ํ•˜๊ธฐ

3-1. git clone ์œผ๋กœ django ์„ค์ •ํ•˜๊ธฐ

# git clone๋ฐ›๊ธฐ
# ๋‚˜์˜ ๊ฒฝ์šฐ, git์— ์žฅ๊ณ  ์ดˆ๊ธฐ์„ธํŒ… ์ž‘์—…ํ•œ ํŒŒ์ผ์„ ์˜ฌ๋ ค๋†จ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ํด๋ก  ๋ฐ›์•˜๋‹ค.
$ git clone ๊นƒ ์ฃผ์†Œ

# ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™
$ cd ์žฅ๊ณ  ๋””๋ ‰ํ† ๋ฆฌ ์ด๋ฆ„

# requirements ์„ค์ •
# ๋กœ์ปฌ์—์„œ ์žฅ๊ณ  ์ดˆ๊ธฐ์„ธํŒ… ํ•  ๋•Œ, ํ”„๋กœ์ ํŠธ์— ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ๋ชฉ๋ก์„ requirements ํŒŒ์ผ๋กœ ์ƒ์„ฑํ•ด๋’€๋‹ค.
# ๋ฐฐํฌ๋ฅผ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด ํŒŒ์ผ์— ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์šฐ๋ถ„ํˆฌ์—์„œ๋„ ๋˜‘๊ฐ™์ด ์„ค์น˜ํ•ด์ค˜์•ผํ•œ๋‹ค.
# ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด requirementsํŒŒ์ผ์— ์žˆ๋Š” ํŒจํ‚ค์ง€๊ฐ€ ํ•œ๋ฒˆ์— ์„ค์น˜๋œ๋‹ค.
$ pip install -r requirements.txt
$ pip freeze (์ œ๋Œ€๋กœ ์„ค์น˜๋๋‚˜ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด)

# ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ธฐ
$ python manage.py runserver 0.0.0.0:8000

4. gunicorn ์„ค์น˜ํ•˜๊ธฐ

gunicorn์€?

  • ์›น ์„œ๋ฒ„์—์„œ ์žฅ๊ณ  ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋Œ๋ ค์ฃผ๋Š” ๋ฐฉ์‹
$ pip install gunicorn
$ pip freeze > requirements.txt

# requirements ํŒŒ์ผ์— ์—†๋Š” ํŒจํ‚ค์ง€๋ฅผ ์šฐ๋ถ„ํˆฌ์—์„œ๋Š” ์„ค์น˜ํ•œ๊ฑฐ๋‹ˆ๊นŒ requirements์—๋„ ๋˜‘๊ฐ™์ด ์˜ฎ๊ธฐ๊ธฐ

# nohup ์ด๋ผ๋Š” ํˆด๋กœ gunicorn ๋™์ž‘์‹œํ‚ค๊ธฐ
$ nohup gunicorn --bind=0.0.0.0:8000 ํ”„๋กœ์ ํŠธ๋ช….wsgi &
# ํ”„๋กœ์ ํŠธ ๋ช…์€ django ์„ค์ •ํ•  ๋•Œ, ์ƒ์„ฑํ–ˆ๋˜ ํ”„๋กœ์ ํŠธ ๋ช…
# settings.py ๊ฐ€ ์žˆ๋Š” ํด๋”์˜ ๋ช…

# ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜๋‚˜ log ํ™•์ธํ•˜๊ธฐ
$ ps -ef | grep python

5. nginx ์„ค์น˜ํ•˜๊ธฐ

# ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ sites-enabled / sites-enabled ์œ„์น˜๋กœ ์ด๋™
# ์•„๋ž˜ ๋‘๊ฐœ์˜ ๊ฒฝ๋กœ์— ์•„๋ž˜ ๋‚ด์šฉ ๋˜‘๊ฐ™์ด ์ž…๋ ฅํ•˜๊ธฐ

$ cd /etc/nginx/sites-enabled
$ cd /etc/nginx/sites-available

# ์œ„์˜ ๊ฒฝ๋กœ์— ํ”„๋กœ์ ํŠธ ๋ช…(settings.py๊ฐ€ ์žˆ๋Š” ํด๋”๋ช…)์œผ๋กœ ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ

$ sudo -vi ํ”„๋กœ์ ํŠธ๋ช…

# ์•„๋ž˜๋‚ด์šฉ ์ž…๋ ฅ
server {
        listen 80;
        server_name [AWS EC2 public ip์ฃผ์†Œ];
		charset utf-8;
       
		location / {
                include proxy_params;
                proxy_pass http://127.0.0.1:8000
        }
        
        location /static/ {
                alias /home/ubuntu/ํ”„๋กœ์ ํŠธ ํด๋”;
        }
}

# ์—ฐ๊ฒฐํ•  ๋„๋ฉ”์ธ์ด ์žˆ๋‹ค๋ฉด ?! (ex. ๋„๋ฉ”์ธ์ด http://soohyun.com ์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด)
server {
        listen 80;
        server_name soohyun.com www.soohyun.com;
        #www ๋ถ™์ธ ๊ฒƒ ํ•˜๋‚˜, ์•ˆ ๋ถ™์ธ ๊ฒƒ ํ•˜๋‚˜ ํ•ด์„œ ๋‘๋ฒˆ ์จ์ฃผ๊ธฐ
		charset utf-8;
       
		location / {
                include proxy_params;
                proxy_pass http://127.0.0.1:8000
        }
        
        location /static/ {
                alias /home/ubuntu/ํ”„๋กœ์ ํŠธ ํด๋”;
        }
}

์ฐพ์•„๋ณด๋‹ˆ๊นŒ ๋ธ”๋กœ๊ทธ๋งˆ๋‹ค static๊ณผ media ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ณณ๋„ ์žˆ๊ณ , ์„ค์ •ํ•˜์ง€ ์•Š๋Š” ๊ณณ๋„ ์žˆ๋Š”๋ฐ ๊ทธ ์ฐจ์ด๋ฅผ ์•„์ง ๋ชจ๋ฅด๊ฒ ๋‹ค ใ… ใ…  > ์ •๋ฆฌํ•„์š”!!

# nginx ๋ฌธ๋ฒ• ์˜ค๋ฅ˜ ํŒ๋‹จ
$ sudo nginx -t

# ์ž˜ ์„ค์ •๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด?
$ systemctl status nginx

# ์—๋Ÿฌ๊ฐ€ ์—†๋‹ค๋ฉด nginx ๊ตฌ๋™
$ sudo systemctl restart nginx

๐Ÿ“Œ ์ค‘์š”!!! ์—ฌ๊ธฐ๊นŒ์ง€ํ•˜๋ฉด ํฌ์ŠคํŠธ๋งจ์œผ๋กœ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ์„œ๋ฒ„๊ฐ€ ๋Œ์•„๊ฐ€์ง€ ์•Š๋Š”๋‹ค. 80๋ฒˆ ํฌํŠธ๋ฅผ aws ์—์„œ ๋”ฐ๋กœ ํ—ˆ์šฉ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

  1. aws ec2 ์ธ์Šคํ„ด์Šค ํด๋ฆญ
  2. ํ•˜๋‹จ ๋ณด์•ˆ ํƒญ ํด๋ฆญ
  3. ๋ณด์•ˆ ๊ทธ๋ฃน ๋งํฌ ํด๋ฆญ
  4. ์ธ๋ฐ”์šด๋“œ ๊ทœ์น™ ํŽธ์ง‘ ํด๋ฆญ
  5. ์•„๋ž˜์™€ ๊ฐ™์ด 80๋ฒˆ ํฌํŠธ 2๊ฐœ๋ฅผ ์ถ”๊ฐ€๋กœ ์„ค์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

nginx ์ฐธ๊ณ 1
nginx ์ฐธ๊ณ 2
nginx ์ฐธ๊ณ 3

profile
back-end ๊ฐœ๋ฐœ์ž

9๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2020๋…„ 10์›” 30์ผ

์ธ๋„ค์ผ ์ถ”๊ฐ€...

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2020๋…„ 10์›” 30์ผ

์ธ๋„ค์ผ ์ถ”๊ฐ€...

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ
comment-user-thumbnail
2020๋…„ 11์›” 1์ผ

์ตœ๊ณ ์‹œ๋‹ค๐Ÿ‘

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2020๋…„ 11์›” 11์ผ

์ฐธ๊ณ  ๋งํฌ์— ์˜คํƒ€๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋„ค์—ฌ
uginx -> nginx

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2021๋…„ 4์›” 14์ผ

๋งˆ์ง€๋ง‰ AWS ๋ณด์•ˆ ๊ทœ์น™ ์ถ”๊ฐ€๊ฐ€ ์ •๋ง ํฐ ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!

1๊ฐœ์˜ ๋‹ต๊ธ€