heroku / Django

heroku に登録


登録すらしてなかった。登録から。
heroku のサイト へ行って、

  • メールアドレス入力
  • メールが届くのでそこにあるURL からログイン
  • password 入力
  • 登録

で登録完了。

toolbelt を install


登録後の dashboard か、ここ から Heroku toolbelt を入手。

Heroku login


% ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/user/.ssh/id_rsa): /Users/user/.ssh/heroku
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/user/.ssh/heroku.
Your public key has been saved in /Users/user/.ssh/heroku.pub.
The key fingerprint is:
(略)
The key's randomart image is:
+--[ RSA 2048]----+
(略)
+-----------------+
鍵を作る。

heroku login コマンドでログインする。
公開鍵も登録。

%heroku login
Enter your Heroku credentials.
Email: mailaddress
Password (typing will be hidden):
Found the following SSH public keys:
1) heroku.pub
Which would you like to use with your Heroku account? 1

秘密鍵を登録。Host は heroku.com じゃないといけない。
% vim ~/.ssh/config

Host heroku.com
Hostname heroku.com
IdentityFile ~/.ssh/heroku
User mail address

pip を入れる


私の OS X に入っていた pip がおかしかったので入れなおし。

ここ を参考に、セキュリティに注意して(?) get.py を取ってきて

% sudo python get-pip.py

で install する。

setuptools が install されていなければ、get-pip.py が setuptools を自動的に install してくれるらしい。

% sudo python install -U setuptools

で更新。

virtualenv を入れる


% sudo python install virtualenv
だけ。
おしまい。

virtualenv 環境を作る


% virtualenv venv --distribute
New python executable in venv/bin/python
Installing setuptools, pip...done.

これで heroku プロジェクトを作る予定のフォルダの下に venv ディレクトリが出来る。

% source venv/bin/active

で仮想環境をアクティブに。成功すれば、shell の横に(venv)が付く。


Django を使うための設定


とりあえず Django と heroku に必要なものを install する。
% ./venv/bin/pip install django-toolbelt

python から postgresql を install するための psycopg2 をインストールするときに、がっつりとエラーを吐かれる。

~/.pip/pip.log ファイルをみろ、とあるので見てみると、

Error: pg_config executable not found.

どうやら PostgreSQL が install されていないとインストール出来ないらしい。

なので PostgreSQL をインストール。
% brew install postgresql

これで install 可能になった。
というわけで再度、
% ./venv/bin/pip install psycopg2

Django を使う


紆余曲折を経てやっとたどり着きました。
アプリケーションの作成から。

% ./venv/bin/django-admin.py startproject hellodjango .

動かしてみる。

% python hellodjango/manage.py runserver
Validating models...

0 errors found
March 08, 2014 - 05:21:01
Django version 1.6.2, using settings 'hellodjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

URL localhost:8000 をブラウザで開くと……!



わーい。local で実行できたー。

Procfile の設定が必要らしい。

%vim Procfile
web: gunicorn hellodjango.wsgi

の一行を保存。

次は

% foreman start
15:42:02 web.1 | started with pid 9159
15:42:04 web.1 | 2014-03-08 15:42:04 [9159] [INFO] Starting gunicorn 18.0
15:42:04 web.1 | 2014-03-08 15:42:04 [9159] [INFO] Listening at: http://0.0.0.0:5000 (9159)
15:42:04 web.1 | 2014-03-08 15:42:04 [9159] [INFO] Using worker: sync
15:42:04 web.1 | 2014-03-08 15:42:04 [9162] [INFO] Booting worker with pid: 9162

で実行できるか確認する。
localhost:5000 にアクセスしてできてたらおっけー。


heroku にデプロイ


終わりじゃありません。ゴールは heroku で実行できるようになってから。

% ./venv/bin/pip freeze > requirements.txt
% cat requirements.txt
Django==1.6.2
dj-database-url==0.2.2
dj-static==0.0.5
django-toolbelt==0.0.1
gunicorn==18.0
psycopg2==2.5.2
pystache==0.5.3
static==1.0.2
wsgiref==0.1.2

で txt ファイルを作成。

% vim .gitignore

絶賛 vim 派です。.gitignore ファイルを作る。
そして git 初期化。

% git init

git add から commit まで。

% git add requirements.txt hellodjango/
% git status [/Users/taira/projects/django/hellodjango]
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: hellodjango/__init__.py
# new file: hellodjango/settings.py
# new file: hellodjango/urls.py
# new file: hellodjango/wsgi.py
# new file: manage.py
# new file: requirements.txt
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# .gitignore

% git commit -m "startapp"
[master (root-commit) 5105166] startapp
6 files changed, 121 insertions(+)
create mode 100644 hellodjango/__init__.py
create mode 100644 hellodjango/settings.py
create mode 100644 hellodjango/urls.py
create mode 100644 hellodjango/wsgi.py
create mode 100755 manage.py
create mode 100644 requirements.txt

そして heroku に登録。appname は任意。他の人とかぶっている名前は使えないようなので、任意にオリジナリティあふれる名前をどうぞ。
% heroku apps:create appname
Creating levelmanager... done, stack is cedar
http://appname.herokuapp.com/ | git@heroku.com:appname.git
Git remote heroku added

% git push heroku master

で heroku へアプリが push される。

動いてないなら試しに
% heroku ps:scale web=1

とかやるといいかも。

% heroku ps:scale web=0
とかやると死ぬ。

ログの確認。
% heroku logs

プロセスの確認。
% heroku ps

終わりに


結構いろいろと試行錯誤した部分もあった。英語の公式サイトが一番信頼できる。Procfile とかが必要なのも公式サイトで知ったし。
git push heroku master した後、どうやら Procfile に書いてあることを実行するみたい。
gunicorn とか必要だったりしたのを途中で気がつく。gunicorn は django を動かすのに必要らしい。
django-toolbelt を pip でインストールすれば、それらは一発で入る。それを知らずに django と psycopg2 だけをインストールして試してて失敗してしまった。

後からいろいろ試したのを追記したり変更したりして書き上げた記事なので、そのまま上からやっていくとたまにおかしなことになるかも。ご注意。


参考

https://devcenter.heroku.com/articles/getting-started-with-django#specify-dependencies-with-pip
http://spiri-tua-lism.com/?p=617
http://qiita.com/youcune/items/5b783f7fde45d0fd4b35

コメント

このブログの人気の投稿

相互インクルード対策

make error "Circular"

gdb操作