AWS: gunicorn ๋ฐฐํฌ ๐Ÿผ

On a regular basisยท2021๋…„ 7์›” 14์ผ
3

AWS

๋ชฉ๋ก ๋ณด๊ธฐ
4/4

  1. EC2 ์„œ๋ฒ„ ์ ‘์†!
  2. ๋ฏธ๋‹ˆ์ฝ˜๋‹ค ์„ค์น˜ -> ํŒจํ‚ค์ง€ ์„ค์น˜
๐Ÿ‘ wget https://repo.anaconda.com/miniconda/Miniconda3-py39_4.9.2-Linux-x86_64.sh ๐Ÿ‘ ์„ค์น˜์Šคํฌ๋ฆฝํŠธ๋ฐ›๊ธฐ

๐Ÿ‘ chmod +x Miniconda3-py39_4.9.2-Linux-x86_64.sh ๐Ÿ‘ ์‹คํ–‰๊ถŒํ•œ์„ ์ฃผ๊ธฐ
๐Ÿ‘ ./Miniconda3-latest-Linux-x86_64.sh ๐Ÿ‘ ๋ฏธ๋‹ˆ์ฝ˜๋‹ค์„ค์น˜ ๋ฐ ์‹คํ–‰ 

  • ์š”๋ ‡๊ฒŒ ๋œจ๋ฉด ์„ฑ๊ณต~~!
source .bashrc ๐Ÿ‘ source๋ฅผ ํ†ตํ•ด ๋กœ์ปฌ ํ™˜๊ฒฝ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„ค์ •ํ•ด์ฃผ๊ธฐ!
sudo apt-get update
sudo apt-get upgrade
  • ์ด์ œ GCC๋ฅผ ์„ค์น˜ํ•ด์ค„๊ฑด๋ฐ ๊ทธ๋ž˜์•ผ ์ดํ›„์— mysqlclient ์„ค์น˜ํ•  ๋•Œ ์—๋Ÿฌ์•ˆ๋‚จ!
sudo apt-get install gcc
sudo apt-get install libmysqlclient-dev
  • ์ด์ œ ์ฝ˜๋‹ค๋กœ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด์ค„ ์ฐจ๋ก€!
conda create -n project python=3.9 ๐Ÿ‘ project๋Š” ๊ฐ€์ƒํ™˜๊ฒฝ ์ด๋ฆ„!
conda activate project
  • cloneํ•  ์‚ฌ์ดํŠธ ๊ฐ€์ ธ์™€์ฅฌ๊ธฐ
git clone https://github.com/eunhye43/hoxylush.git ๐Ÿ‘ hoxylush์‚ฌ์ดํŠธ๋ฅผ ๊นƒํด๋ก ~~~ , hoxylush๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™ !
  • requierements ๋ฅผ ํ™•์ธํ•ด๋ณด์ž! ํ”„๋กœ์ ํŠธ์— ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ๋ชฉ๋ก์„ ๋ณด์—ฌ์ฃผ๋‹ˆ๊นŒ~!
  • ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด์„œ๋Š” ์—ฌ๊ธฐ ๋‚˜์™€์žˆ๋Š” ๊ฒƒ๋“ค์„ pip install ํ•ด์ค˜์•ผ ํ”„๋กœ์ ํŠธ ๋™์ž‘ ๊ฐ€๋Šฅ !
  • ๊ทผ๋ฐ ๋„ˆ๋ฌด ๋งŽ์œผ๋‹ˆ๊นŒ ํ•œ๋ฒˆ์— ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด๋ฅผ ๐Ÿ˜—
cat requirements.txt
pip install -r requirements.txt 
๐Ÿ‘ ์ฃผ์˜ํ•  ๋ถ€๋ถ„ -> python์€ ์‚ญ์ œ ๊ฐ€๋Šฅ, < ํŒจํ‚ค์ง€ == ๋ฒ„์ „ > ์ด๋Ÿฐํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ์ €์žฅ!
pip freeze ๐Ÿ‘ ์„ค์น˜ ์ž˜๋๋‚˜ ํ™•์ธ
  • ์ด์ œ ๋‚ด ํ”„๋กœ์ ํŠธ์— my_settings.py๋ฅผ ๋งŒ๋“ค๊ณ , settings์— ec2 ์„œ๋ฒ„์— public IP๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค! -> ๊ทธ๋ฆฌ๊ณ  ์„œ๋ฒ„ ๋Œ๋ ค์ฃผ๊ธฐ~
  • public IP ์–ด๋””์„œํ™•์ธ?
  • settings.py ๋“ค์–ด๊ฐ€์„œ!
    ์š”๋ ‡๊ฒŒ ํผ๋ธ”๋ฆญ ip๋กœ ์ˆ˜์ •!
  • ์ด์ œ my_settings.py๋ฅผ manage.py์žˆ๋Š” ๊ณณ์— ๋งŒ๋“ค์–ด์ฃผ๊ณ ,
DATABASES = {
    'default' : {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'hoxylush', ๐Ÿ‘ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ด๋ฆ„
        'USER': 'root',
        'PASSWORD': 'high0426',
        'HOST': '127.0.0.1', 
        'PORT': '3306',
    }
}

SECRET = 'django-insecure-=8db%og*e9s2=7f%5j+$^v9(qqsl^#oufj=k-zu5%2e^&myng%'
![](https://velog.velcdn.com/images%2Fmajaeh43%2Fpost%2F442b4505-c7d1-4faf-b176-1e3ba879530a%2Fimage.png)
  • ์ด๊ฑธ ํ•ด์ฃผ๋ฉด EC2์—์„œ RDS์„œ๋ฒ„๋กœ ์ ‘์†์„ํ•ด์„œ ๋กœ์ปฌ์—์„œ ์„œ๋ฒ„๋ฅผ ๋Œ๋ฆฌ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์„œ๋ฒ„๋ฅผ ๋Œ๋ฆด ์ˆ˜ ์žˆ์Œ!
    ๐Ÿ‘ RDS HOST ํ™•์ธ ๋ฐฉ๋ฒ•์€?
  • ์ด์ œ ๋Ÿฐ์„œ๋ฒ„ ๋™์ž‘์‹œ์ผœ์ค„๊ฑฐ์•ผ!
python manage.py runserver 0:8000

๐Ÿ‘ ์ชผ๊ธฐ์— ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ my_settings.py์— ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ• ๐Ÿ‘(https://media.vlpt.us/images/majaeh43/post/b7cafade-f809-48fb-aaf1-35716bfdf70d/image.png) ์š”๋ ‡๊ฒŒ ์ถ”๊ฐ€!
๐Ÿ‘ ๋‚ด๊ฐ€ ์–ด๋–ค EC2์˜ public IP์ฃผ์†Œ๋ฅผ ๊ฐ€์กŒ๋Š”์ง€,
๐Ÿ‘ ๋‚ด RDS ์—”๋“œํฌ์ธํŠธ๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ •ํ™•ํ•˜๊ฒŒ ์ž…๋ ฅํ•ด์•ผํ•จ. ์•ˆ๊ทธ๋Ÿผ ์‚ฐ์œผ๋กœ๊ฐ„๋‹ค.

  • ์ž ์ด์ œ๋Š”.. ์—”๋“œํฌ์ธํŠธ ํ˜ธ์ถœ์ด ๋˜๋Š”์ง€ ํฌ์ŠคํŠธ๋งจ์œผ๋กœ ํ™•์ธํ•ด๋ณด์Ÿˆ!
  • ํฌ์ŠคํŠธ๋งจ ์„ฑ๊ณต ! ๋‚ดํ”„๋กœ์ ํŠธ์—์„œ ๋ฐฐํฌ๋ฅผ ํ•œ๊ฒŒ ๋จ ^^

๐Ÿ‘ ์‚ฌ์‹ค ์ด๋ ‡๊ฒŒ๋งŒํ•ด๋„ ์—”๋“œํฌ์ธํŠธ ํ˜ธ์ถœ์„ ๋˜์ง€๋งŒ ์žฅ๊ณ ์—์„œ ์“ฐ๋Š” manage.py runserver ๋ช…๋ น์–ด๋Š” ๋‹จ์ผ์“ฐ๋ ˆ๋“œ๋กœ ๋™์ž‘ํ•˜์—ฌ ๊ฐœ๋ฐœ ๋ฐ ํ…Œ์ŠคํŠธ๋กœ๋Š” ์ ๋‹นํ•˜์ง€๋งŒ request๊ฐ€ ๋งŽ์„ ์ˆ˜ ์žˆ๋Š” ์šด์˜ํ™˜๊ฒฝ์—์„œ๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์–ด์„œ ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ๋ฅผ ์ง€์›ํ•˜๋Š” gunicorn์„ ์„ค์น˜ํ•˜์—ฌ ๋ฐฐํฌํ•ด๋ณด๋„๋ก ํ•˜์žก!

๐Ÿ‘ gunicorn์ด๋ž€?
python ์–ธ์–ด ์ƒ์—์„œ ์ž‘๋™ํ•˜๋Š” HTTP์„œ๋ฒ„์ด๋‹ค. gunicorn์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒ์—์„œ ์„œ๋ฒ„๋ฅผ ๋ฐ๋ชฌ์œผ๋กœ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค!

  • gunicorn์€ ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„๊ฐ€ ์žˆ๊ณ  ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  ์žฅ๊ณ  ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์žˆ๋‹ค.
    gunicorn์„ ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•ด์„œ๋Š” pip install๋กœ ์„ค์น˜ํ•˜๋ฉด ๋จ!
    ssh session์ด ๋Š์–ด์ ธ๋„ server๋ฅผ ๋™์ž‘์‹œ์ผœ์ฃผ๊ธฐ ์œ„ํ•ด์„œ! ๐Ÿ‘nohup๐Ÿ‘์ด๋ผ๋Š” ํˆด์„ ์จ์„œ gunicorn์„ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ๋™์ž‘์‹œ์ผœ์ค„๊ฑฐ์•ผ.
gunicorn --bind=0.0.0.0:8000 welonmusk.wsgi
๐Ÿ‘ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์•ˆ๋Œ๋ฆฌ๊ณ  ๊ทธ๋ƒฅ ๋Œ๋ฆฌ๋Š” ๊ฒƒ
nohup gunicorn --bind=0.0.0.0:8000 hoxylush.wsgi &
๐Ÿ‘ ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ๋Œ๋ฆฌ๋Š” ๊ฒƒ / wsgi๋Š” ์žฅ๊ณ ์— ์žˆ๋Š” ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ง€์ •ํ•ด์ฃผ๋Š” ์—ญํ• 

๐Ÿซ ๋„ˆ๋ฌด ๋ฌด์„œ์šด๊ฒŒ gunicorn์„ ์œ„์—์„œ๋Š” Pip๋กœ ๊น”์•„์คฌ๋Š”๋ฐ ์•ˆ๋๋‹ค.

๐Ÿซ ๊ทผ๋ฐ ๋‹ค์‹œ sudo apt-get install gunicorn์œผ๋กœ ๊น”์•„์ฃผ๋‹ˆ๊นŒ ๋œ๋‹ค... ์ด๊ฒŒ ๋จธ์„ ์ผ์ด๊ณ ?....?

sudo apt-get install gunicorn
  • wsgi.py ํŒŒ์ผ์ด ์žˆ๋Š” ๊ณณ์—์„œ ์ง„ํ–‰ํ•ด์ฃผ๊ธฐ!

๐Ÿ’ ํ˜„์žฌ ์‹คํ–‰ ๋˜๊ณ  ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค ํ™•์ธํ•˜๊ธฐ

ps -ef | grep python

๐Ÿ’ ์„œ๋ฒ„ ์ข…๋ฃŒ

kill [pid๋ฒˆํ˜ธ]
profile
๊ฐœ๋ฐœ ๊ธฐ๋ก

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