Heroku を使ってみる
最近人気な Heroku 。
Python に対応したそうなのでいじってみました。
以下、Flask を使った場合の手順。
参考サイト:
Heroku で Flask + SQLAlchemy を使う - Twisted Mind
http://d.hatena.ne.jp/Voluntas/20110920/1316529816
Heroku | Dev Center | Getting Started with Python on Heroku/Cedar
http://devcenter.heroku.com/articles/python
環境
- Mac OS X ( 10.6.8 )
- Git はインストール済みなので割愛。
- Heroku アカウントは取得済み。
gem update する
$ sudo gem update --system
Heroku インストール
$ sudo gem install heroku
フォルダ作成
$ mkdir sample_heroku $ cd sample_heroku
virtualenv 作成
$ virtualenv --no-site-packages . $ source bin/activate
これ以下 virtualenv「sample_heroku」 内で行います。
Flask インストール
$ bin/pip install flask
git 初期化
$ git init
application 作成
フォルダ application を作る。
application 内に起動用 __init__.py 作る。
# -*- coding: utf-8 -*- import os from flask import Flask, Response app = Flask(__name__) @app.route("/") def root(): return Response("Hello, Heroku.") if __name__ == "__main__": port = int(os.environ.get("PORT", 5000)) app.run(host='0.0.0.0', port=port)
環境設定ファイル作成
インストールしたものリストファイルを作る。
これを見て Heroku がサーバ上に同一環境を作成するっぽい。
$ bin/pip freeze > requirements.txt
中身はこんな感じ。(編集不要)
Flask==0.7.2 Jinja2==2.6 Werkzeug==0.8.1 gunicorn==0.12.2 wsgiref==0.1.2
Procfile ファイル作成
Heroku の設定ファイル。中身は以下の1行
web: bin/gunicorn -b 0.0.0.0:$PORT application:app
「$PORT」は Heroku がよしなにしてくれる模様。
ちなみに「web: 」以下を「$PORT」除いてコマンドラインで実行すると、
ローカルホストが立ち上がる。
$ gunicorn -b 0.0.0.0 application:app 2011-10-05 16:03:00 [46319] [INFO] Starting gunicorn 0.12.2 2011-10-05 16:03:00 [46319] [INFO] Listening at: http://0.0.0.0:8000 (46319) 2011-10-05 16:03:00 [46319] [INFO] Using worker: sync 2011-10-05 16:03:00 [46320] [INFO] Booting worker with pid: 46320
ファイル構成
で、ここまででファイル構成はこうなってるハズ
sample_heroku/application/__init__.py sample_heroku/bin/ sample_heroku/include/ sample_heroku/lib/ sample_heroku/Procfile sample_heroku/requirements.txt
.gitignore 設定
フォルダ「sample_heroku」直下に「.gitignore」ファイル作成。
中身は以下。
bin/ include/ lib/ *.pyc .Python
Git に commit
$ git add application/__init__.py $ git add Profile $ git add requirements.txt $ git commit -m 'init'
git add 3回でなくて「$ git add . 」でも可能だけど、
その辺は宗教上の理由なのでお好みで。
Heroku にアプリを作る
アカウントの Email アドレスとパスワードを聞かれます。
公開鍵は適当に探してくれるっぽい。
$ heroku create --stack cedar Enter your Heroku credentials. Email: [youremailaddress@example.com] Password: [your password] Found existing public key: /Users/*****/.ssh/id_rsa.pub Uploading ssh public key /Users/*****/.ssh/id_rsa.pub Creating [your application name]... done, stack is cedar http://[your application name].herokuapp.com/ | git@heroku.com:[your application name].git Git remote heroku added
[your application name] は Heroku が適当に発行してくれます。
作成後は「$ heroku info 」等で確認可能。
確認
以上で Remote ブランチができるので確認してみる。
初回は「接続するけど大丈夫?(超意訳)」と確認入るので「yes」と答えるべし。
$ git remote show heroku The authenticity of host 'heroku.com (50.19.85.154)' can't be established. RSA key fingerprint is [your fingerprint]. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'heroku.com,50.19.85.154' (RSA) to the list of known hosts. * remote heroku Fetch URL: git@heroku.com:[your application name].git Push URL: git@heroku.com:[your application name].git HEAD branch: (unknown)
アップロード
さっき確認した Remote ブランチに push するだけのお手軽アップロード。
$ git push heroku master
最終確認
アプリの URL(http://[your application name].herokuapp.com/)をブラウザで見ると
「Hello, Heroku.」と表示されるハズ。
詰まったトコ
- 「Profile」かと思ったら「Procfile」だった。「c」重要。
- 「$ heroku scale web=1」を忘れていた。
他覚えとくと便利なコマンド
ヘルプ
$ heroku help
ログ参照
$ heroku logs
再起動
$ heroku restart
以上、割と簡単に諸々出来る環境が作れるので
趣味でやる範囲なら楽しそうな感じですね。
さて何を作ろうかな。
-
- -
修正:
- $ mkvirtualenv --no-site-packages sample_heroku + $ virtualenv --no-site-packages .
上だと source 〜 が意味ないです。ごめんなさい。