Heroku ๐Ÿš€์— Django ํ”„๋กœ์ ํŠธ ๋ฐฐํฌํ•˜๊ธฐ

4122 ๋‹จ์–ด djangotutorialherokupython
์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค ๐ŸŽ‰ Django ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  Heroku ๐Ÿš€์— ๋ฐฐํฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” Heroku์— django ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฐฐํฌํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ๋‹จ๊ณ„๋ฅผ ์ง„ํ–‰ํ•  ๊ฒƒ์ด๋ฏ€๋กœ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜



๋จผ์ € ๋ช‡ ๊ฐ€์ง€ ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

1. django_heroku ์„ค์น˜
์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌdjango_heroku๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค. ์›ํ™œํ•œ ๋ฐฐํฌ ๋ฐ ๊ฐœ๋ฐœ ๊ฒฝํ—˜์„ ๋ณด์žฅํ•˜๋Š” Heroku ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์šฉ Django ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

pip3 install django-heroku


django_heroku๋Š” Heroku์˜ ๊ธฐ๋ณธ PostgresDB๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ Postgres๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ settings.py DATABASES ๋ณ€์ˆ˜์—์„œ ์•„๋ฌด ๊ฒƒ๋„ ๋ณ€๊ฒฝํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ž์ฒด DB๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  DATABASES ๋ณ€์ˆ˜์— ์ž๊ฒฉ ์ฆ๋ช…์„ ์ œ๊ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

2. gunicorn ์„ค์น˜

UNIX์šฉ Python WSGI HTTP ์„œ๋ฒ„๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹คGunicorn.

pip3 install gunicorn


ํ•„์ˆ˜ ํŒŒ์ผ



๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ฐฐํฌํ•˜๊ธฐ ์ „์— ํ•„์š”ํ•œ ํŒŒ์ผ์„ ์ค€๋น„ํ•ฉ์‹œ๋‹ค.

1. ์š”๊ตฌ ์‚ฌํ•ญ.txt

๋‹ค์Œ ๋ช…๋ น์„ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ์ ํŠธ์—์„œ ์„ค์น˜ํ•˜๊ณ  ์‚ฌ์šฉํ•œ ๋ชจ๋“  ํŒจํ‚ค์ง€๋ฅผ requirements.txt ํŒŒ์ผ์— ์ €์žฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

pip3 freeze > requirements.txt


2. ํ”„๋กœํ•„

ํ™•์žฅ์ž๊ฐ€ ์—†๋Š” Procfile๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ํŒŒ์ผ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜๊ณ  ์—ฌ๊ธฐ์— ๋ช‡ ์ค„์„ ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค.

release: export DEVELOPMENT=True
release: python manage.py makemigrations --no-input
release: python manage.py migrate --no-input

web: gunicorn [YOUR_PROJECT_NAME].wsgi


์ด ํŒŒ์ผ์€ ์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ์‹คํ–‰ํ•˜๋ ค๋Š” ๋ชจ๋“  ํ•„์ˆ˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์ž์œ ๋กญ๊ฒŒ ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์„œ๋ฒ„ ์‹œ์ž‘์„ ๋‹ด๋‹นํ•˜๋Š” ๋งˆ์ง€๋ง‰ ์ค„์€ ์ œ๊ฑฐํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค.

3. ๋Ÿฐํƒ€์ž„.txt
์ด ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜๊ณ  ํ”„๋กœ์ ํŠธ์—์„œ ํ˜„์žฌ ์‚ฌ์šฉ ์ค‘์ธ Python ๋ฒ„์ „์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

python-3.8.0


4. settings.py ์ˆ˜์ •
๋งˆ์ง€๋ง‰์œผ๋กœ settings.py ํŒŒ์ผ์— ๋ช‡ ์ค„์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋จผ์ € django_heroku ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

import django_heroku


๊ทธ๋Ÿฐ ๋‹ค์Œ ์ด ์ค„์„ settings.py ํŒŒ์ผ ๋์— ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค.

django_heroku.settings(locals())


GitHub์— ๊ฒŒ์‹œ



GitHub์— ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ  ํ”„๋กœ์ ํŠธ๋ฅผ ํ‘ธ์‹œํ•ฉ๋‹ˆ๋‹ค.

Like you haven't done that already ๐Ÿ‘€๐Ÿ˜…



Heroku ์•ฑ ๋งŒ๋“ค๊ธฐ



๊ณ„์ •์ด ์—†๋‹ค๋ฉด Heroku์—์„œ ๊ณ„์ •์„ ๋งŒ๋“ค๊ณ  ์ƒˆ ์•ฑ์„ ๋งŒ๋“œ์‹ญ์‹œ์˜ค. ์•ฑ ์ด๋ฆ„๊ณผ ์›ํ•˜๋Š” ์ง€์—ญ์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.



Heroku ํ”„๋กœ์ ํŠธ๋ฅผ Github Repo์— ์—ฐ๊ฒฐ



์•ฑ ๋Œ€์‹œ๋ณด๋“œ ๋‚ด ๋ฐฐํฌ ํƒญ์œผ๋กœ ์ด๋™ํ•˜์—ฌ ๋ฐฐํฌ ๋ฐฉ๋ฒ•์œผ๋กœ GitHub๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.



๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ด๋ฆ„์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์—ฐ๊ฒฐ์„ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.



์ž๋™ ๋ฐฐํฌ ํ™œ์„ฑํ™”



์ž๋™ ๋ฐฐํฌ ์•„๋ž˜์—์„œ ๋ฐฐํฌํ•˜๋ ค๋Š” ๋ถ„๊ธฐ๋ฅผ ์„ ํƒํ•˜์‹ญ์‹œ์˜ค. ๐Ÿš€ ์ƒˆ ์ปค๋ฐ‹์ด ํ•ด๋‹น ๋ถ„๊ธฐ์— ํ‘ธ์‹œ๋  ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ๋ฐฐํฌ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด ์ˆ˜๋™ ๋ฐฐํฌ์—์„œ ๋ฐฐํฌํ•˜๋ ค๋Š” ๋ถ„๊ธฐ๋ฅผ ์„ ํƒํ•œ ๋‹ค์Œ ๋ฐฐํฌ๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‚˜๋ญ‡๊ฐ€์ง€.



์•ฑ ํ™•์ธ



๋ฐฐํฌ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜๋ฉด ์•ฑ์„ ์—ด๊ณ  ํ™•์ธํ•˜์‹ญ์‹œ์˜ค. ์ด์ œ ๊ฐˆ ์ค€๋น„๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ๐Ÿš€๐Ÿ”ฅ.



๊ฒฐ๋ก 



๊ทธ๊ฒƒ์ด Heroku์— Django ํ”„๋กœ์ ํŠธ๋ฅผ ๋ฐฐํฌํ•˜๋Š” ๊ฒƒ์— ๊ด€ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ™•์ธํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ํ”„๋กœ์ ํŠธsource code๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์งˆ๋ฌธ๋„ ๋ถ€๋‹ด์—†์ด ํ•ด์ฃผ์„ธ์š” ๐Ÿ˜‰.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ