https://www.youtube.com/watch?v=RQ0eKv6HrpM 

https://wagtail.org/blog/wagtail-heroku-2017/

 

먼저 위에 사이트에서 운영체제에 맞는 Heroku를 다운 받도록 하자.

 

 

1. Heroku 설치

https://devcenter.heroku.com/articles/heroku-cli

 

The Heroku CLI | Heroku Dev Center

Last updated February 01, 2022 The Heroku Command Line Interface (CLI) lets you create and manage Heroku apps directly from the terminal. It’s an essential part of using Heroku. Install the Heroku CLI Pre-requisites The Heroku CLI requires Git, the popul

devcenter.heroku.com

먼저 위에 사이트에서 운영체제에 맞는 Heroku를 다운 받도록 하자.

 

 

2. Django-toolbelt 설치

배포를 위한 관련 라이브러리를 모아놓은 라이브러리이다. 

 

  • django
  • psycopg2
  • gunicorn
  • dj-database-url
  • dj-static

위에 라이브러리들이 설치된다.

 

pip freeze > requirements.txt

requirements.txt파일을 업데이트 해준다.

 

 

3. 데이터 베이스 설정 (PostreSQL)

https://vicapor.tistory.com/142

https://vicapor.tistory.com/122

 

참고

 

 

4. Procfile 생성

 

Procfile

web: gunicorn 프로젝트이름.wsgi --log-file -

 

 

5. runtime.txt 파일 생성

https://devcenter.heroku.com/articles/python-support

 

위의 사이트에서 지원되는 파이썬 버전을 확인해 볼 수 있다.

 

python-3.8.12

나는 프로젝트에서 사용한 3.8.12버전으로 진행해보겠다.

 

 

6. settings/production.py 수정

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config()
	
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

아직 Heroku url을 모르기 때문에 ALLOWED_HOSTS는 모두 허용을 해놓는다.

 

 

7. .gitIgnore 파일 생성

*.pyc
.DS_Store
*.swp
/venv/
/static/
/media/
.env

이미 git에 연결했으면 .gitignore파일이 존재할 것이다. 위의 내용을 추가해주자.

 

 

8. .env 파일생성

 

.env

DJANGO_SETTINGS_MODULE=project_title.settings.production
SECRET_KEY='####'

 

 

9. Production.py 파일 수정

from __future__ import absolute_import, unicode_literals
from .base import *
import dj_database_url
import os

env = os.environ.copy()
SECRET_KEY = env['SECRET_KEY']

중요한 것은 __future__은 제일 먼저 import 되어야 하므로 맨 위에 적는다.

 

 

10. Heroku 배포

 

배포전에 git에 push를 해놓자!!

 

git init
git add .
git commit -m "first commit to heroku"
heroku create 
# Creates a new Heroku app and connects it to your initialised git repo
git push heroku master 
# Pushes your commited code up to your new ap
heroku plugins:install heroku-config
# Install plugin that allows us to push settings to heroku
heroku config:push
# Pushes settings to heroku, if you get an error check your spaces.
heroku run python manage.py migrate 
# Heroku allows you to run shell commands remotely with the 'heroku run' command.
heroku run python manage.py createsuperuser
# Creates a new superuser on Heroku
heroku ps:scale web=1 
# Ensures that a new Dyno is running for your project

 

issue) 

Dependency on app with no migrations: user 오류가 발생했다. 

user app의 migrations폴더에 0001_initial.py을 .gitignore에 포함시키고 git push를 해주면 된다. 

user app에 user model이 정의 되어 있는데 diary app에서 user를 사용하기 때문에 dependency오류가 발생하는 것 같다.

 

 

11. whitenoise제거

 

pip install whitenoise

 

MIDDLEWARE = [

	...
    'whitenoise.middleware.WhiteNoiseMiddleware',

]

 

 

settings/production.py

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

COMPRESS_OFFLINE = True
COMPRESS_CSS_FILTERS = [
    'compressor.filters.css_default.CssAbsoluteFilter',
    'compressor.filters.cssmin.CSSMinFilter',
]
COMPRESS_CSS_HASHING_METHOD = 'content'

 

git push heroku master

 

 

heroku에 대한 자세한 설명이 보고싶다면

heroku help

 

추가) 

Heroku에서는 Local에서 makemigrations를 진행하고 migrations 파일까지 전부 git에 업로드 해야한다. 그리고 heroku에서 migrate를 해줘야한다. 

'Back-End > Wagtail, Django 배포' 카테고리의 다른 글

DumpData  (0) 2022.01.08
Wagtail RichText API  (0) 2022.01.07
2. Gunicorn, Nginx 적용  (0) 2021.12.23
1. PostgreSQL 적용  (0) 2021.12.22
3. Docker(Django, Wagtail)  (0) 2021.12.22

+ Recent posts