Python Flask -> Heroku 배포하기
Python3 Flask 로 만든 간단한 웹서버를 Heroku 에 배포해보기
0. Heroku 가입 / Heroku-cli 설치
- Heroku 가입 : https://www.heroku.com/
- Heroku CLI 다운 : https://devcenter.heroku.com/articles/heroku-cli#download-and-install
1. gunicorn 설치 (톰캣같은것)
pip install gunicorn
2. Procfile 생성 (heroku가 실행할 명령어 - 파이썬 파일명이 app.py 일때)
web: gunicorn app:app
3. requirements.txt 생성 (관련 라이브러리 목록)
pip freeze > requirements.txt
4. .gitignore 파일 생성 (git 제외목록)
https://www.gitignore.io/ 에서 Flask 일경우의 .gitignore 생성
5. runtime.txt 파일 생성 (실행할 파이썬 버전)
heroku는 기본적으로 python2로 시작하기 때문에 python3로 실행함을 명시
python-3.7.6
6. git 초기화 및 설정
git init
git config --global user.name 사용자이름
git config --global user.email 사용자이메일
7. heroku 로그인
heroku login
8. heroku 앱 생성
앱 생성 -> 이름자동생성
heroku create
자동생성된 이름 변경하고 싶을때
heroku rename 변경하고싶은이름
9. commit 생성
git add .
git commit -m "커밋문구"
10. push 하기
git push heroku master
11. 확인 !
12. Redis 연결해보기
- heroku 에서 addon 으로 redis 설치 : 카드 번호 입력하라고 나옴. 무료니깐 입력해줌.
- python flask redis 연결부분 수정
# r = redis.StrictRedis(host='localhost', port=6379, db=0) # redis 로컬 r = redis.from_url(os.environ['REDISCLOUD_URL']) # redis heroku addon 연결
- heroku-cli 에서 다음 명렁어 실행(아마도 이것만 하면 되는게 아니었을까? )
heroku addons:create rediscloud
- 이제 디비까지 연결되었네 대박! : https://jn22l.herokuapp.com/getKeys