파이썬과 함께 Heroku 시작하기
Getting Started with Python on Heroku
Heroku 로 웹서버 프로그래밍을 해보려고 합니다.
(참고 문서는 heroku 의 Get Started with Python on Heroku)
기본적으로 Python 과 virtualenv, git 사용을 하실 줄 아는 것을 전제로 합니다.
그리고 pip 역시 기본적으로 설치 바랍니다.
그리고 다음 작음은 맥에서 하였습니다.
1. Install Heroku Toolbelt
https://toolbelt.heroku.com 에서 본인의 플랫폼에 맞는 Toolbelt 를 다운받아 설치합니다.
toolbelt를 통해서 터미날에서 직접 heroku 서비스에 접근 가능합니다.
아래 처럼 heroku 가 제대로 설치되었는지 확인하시고 login 시도를 해봅니다.
$ heroku login Enter your Heroku credentials. Email: kenneth@example.com Password: Could not find an existing public key. Would you like to generate one? [Yn] Generating new SSH public key. Uploading ssh public key /Users/kenneth/.ssh/id_rsa.pub
2. Start Flask App inside Virtualenv
helloflask 라는 디렉토리를 만들어서 개발하려는 App의 디렉토리를 만듭니다.
$ mkdir helloflask $ cd helloflask
$ virtualenv venv --distribute New python executable in venv/bin/python Installing distribute...............done. Installing pip...............done.
$ source venv/bin/activate
-> Flask 는 flask.pocoo.org 참고하시길.
3. Hello World
언제나 그렇듯 hello world를 브라우저에 출력하기 위한 첫 코드죠?
import os from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World!'
3. Procfile 작성하기
Procfile 을 작성하여서 Process type을 선언해야합니다. 이와 관련해서는 다음 포스팅에서 한번 적도록 하죠. 저도 heroku 처음 그냥 따라하는 것이라
Procfile 이라는 이름으로 작성하시면 됩니다.
web: gunicorn hello:app
$ foreman start 2013-04-03 16:11:22 [8469] [INFO] Starting gunicorn 0.14.6 2013-04-03 16:11:22 [8469] [INFO] Listening at: http://127.0.0.1:8000 (8469)
4. Specify dependencies with Pip
Heroku 는 requirements.txt 를 통해서 application의 dependency를 자동으로 알아차린다고 합니다.
따라서 다음과 같이 application의 Top directory에 다음과 같이 하여 requirements.txt를 작성합니다.
$ pip freeze > requirements.txt
5. Git repository 생성
- .gitignore 만들기
venv *.pyc
ff
$ git init $ git add . $ git commit -m "init"
6. Deploy your application to Heroku
heroku create 명령어를 통해서 application을 repository 를 push 하기 위한 저장소를 받아옵니다.
$ heroku create Creating stark-window-524... done, stack is cedar http://stark-window-524.herokuapp.com/ | git@heroku.com:stark-window-524.git Git remote heroku added이것은 자동으로 본인의 local repository가 저장될 heroku의 remote repository 가 생성된되는 거죠. 그리고 다음과 같이 하여 push를 할 수 있습니다.
$ git push heroku master Counting objects: 10, done. Delta compression using up to 8 threads. Compressing objects: 100% (8/8), done. Writing objects: 100% (10/10), 3.59 KiB, done. Total 10 (delta 0), reused 0 (delta 0) -----> Heroku receiving push -----> Python app detected -----> No runtime.txt provided; assuming python-2.7.6. -----> Preparing Python runtime (python-2.7.6) -----> Installing Distribute (0.6.36) -----> Installing Pip (1.3.1) -----> Installing dependencies using Pip (1.3.1) ... Successfully installed Flask Werkzeug Jinja2 gunicorn Cleaning up... -----> Discovering process types Procfile declares types -> web -----> Compiled slug size is 3.5MB -----> Launching... done, v2 http://stark-window-524.herokuapp.com deployed to Heroku To git@heroku.com:stark-window-524.git * [new branch] master -> master
7. Visit your application
$ heroku ps:scale web=1 Scaling web processes... done, now running 1
$ heroku ps === web: `gunicorn hello:app` web.1: up for 5s
댓글
댓글 쓰기