結城浩のはてなブログ

ふと思いついたことをパタパタと書いてます。

Windows 7でHerokuを始める

背景

  • さくらVPSRuby on Railsをいじっていたら「Herokuがいいですよ」とお勧めされた。
  • 以前からHerokuのことは聞いていたので試してみようと思った。

前提

インストール

  • heroku.com でサインアップ。
  • Getting Startedを読む。
  • Windows用にheroku-toolbelt.exeをダウンロード。インストール。
  • Windowsを再起動(PATHが更新される)。
  • heroku.batを動かす。
C:\work> heroku login
Enter your Heroku credentials.
Email: (自分のメールアドレス)
Password (typing will be hidden):
Found the following SSH public keys: (SSHで公開鍵を作っていたのでどれを使うかと聞かれた)
1) XXXX.pub
2) YYYY.pub
3) ZZZZ.pub
Which would you like to use with your Heroku account? 3
Uploading SSH public key ZZZZ.pub... done
Authentication successful.

アプリ作り

試行錯誤開始

  • 自分が使っていたrubyが1.8だったのでforemanが動かない。
    • heroku-toolbelt.exeがインストールした1.9.2の方を先に動かすため、PATH書き換えて再起動。
  • bundle installが動かない。
    • gem install bundler
  • bundle installするとコンパイルエラーになる。
    • mkmf.logを見ると、コマンドラインに以下のようなオプションが渡されていてスペースが悪さしている模様。
-IC:/Program Files (x86)/ruby-1.9.2/include/ruby-1.9.1/i386-mingw32
...
gcc: error: (x86)/ruby-1.9.2/include/ruby-1.9.1/i386-mingw32: No such file or directory
    • ruby-1.9.2を C:\ に移動。heroku.batの内容もそれに合わせて書き換え(強引)
  • gem install thin -v '1.5.0' でコンパイルエラー。調べてみると、以下のようになっているので、ポインタサイズが期待と違うらしい。プラットホームが64bitなことに起因してるのかなと予想したが、深追いするのはやめた。
C:/ruby-1.9.2/include/ruby-1.9.1/ruby/ruby.h:112:14: error: size of array 'ruby_check_sizeof_voidp' is negative

助け船現る

  • @ayumin さんが登場して回避策をくださった。thinへdependするのをやめる。

結局

  • 以下のGemfile, Procfile, web.rbを用意する。
# Gemfile
source :rubygems
gem 'sinatra', '1.1.0'
gem 'rack'
# Procfile
web: bundle exec ruby web.rb -p $PORT
# web: bundle exec thin -p $PORT -e $RACK_ENV start
# web: bundle exec rackup -p $PORT -E $RACK_ENV
# web.rb
require 'sinatra'

get '/' do
  "Hello, world"
end
  • bundle installを実行。
C:\work> bundle install
  • foremanを実行。ローカルなWebサーバ。ポートは5000。
C:\work> foreman start
09:18:18 web.1  | started with pid 9100
09:18:22 web.1  | [2012-11-13 09:18:22] INFO  WEBrick 1.3.1
09:18:22 web.1  | [2012-11-13 09:18:22] INFO  ruby 1.9.2 (2011-07-09) [i386-mingw32]
09:18:22 web.1  | [2012-11-13 09:18:22] INFO  WEBrick::HTTPServer#start: pid=8072 port=5000
Hello, world
  • foremanはCTRL+Cで止める。
SIGINT received
09:19:45 system | sending SIGKILL to all processes
09:19:45 system | sending SIGKILL to web.1 at pid 9100
09:19:46 web.1  | exited with code 0

Gitに保存してHerokuに配置(deploy)

C:\work> git init
C:\work> git add .
C:\work> git commit -m "init"
  • Herokuに配置しようとしてエラー。
C:\work> heroku create
(ここで murmuring-badlands-5886.herokuapp.com のようにアプリ名が表示される)
(あとでリネーム可能)

C:\work> git push heroku master
Enter passphrase for key 'ZZZZ.pub':
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
  • 調べると公開鍵をaddしてなかった(あれ?)
C:\work> heroku keys:add
C:\work> git push heroku master
  • ここで murmuring-badlands-5886.herokuapp.com にアクセスすると以下が表示される。
Hello, world

はまったところまとめ

  • Windowsでnativeなコンパイルするところ。パス名の空白と(たぶん)プラットホームのビット数。
    • 回避策を授けてくださった @ayumin さんに感謝!
  • 公開鍵をadd(FAQらしい)するところ。