結城浩のはてなブログ

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

Windows 7からHerokuを使ってHello, worldを表示するWebアプリを作る

前提

  • Windows 7
  • gitインストール済み
  • sshインストール済み
  • www.heroku.comのアカウント登録済み
  • heroku-toolbelt.exeのインストール済み
  • gem install bundler 済み

問題

  • ブラウザでアクセスするとHello, worldを表示するWebアプリを作りたい。

解決

  • 1. Webアプリをローカルに用意
  • 2. WebアプリをHerokuに配置

詳細

1. Webアプリをローカルに用意

  • Gemfile
# Gemfile
source :rubygems
gem 'sinatra', '1.1.0'
gem 'rack'
  • Procfile
# Procfile
web: bundle exec ruby web.rb -p $PORT
  • web.rb
# web.rb
require 'sinatra'

get '/' do
  "Hello, world!"
end
  • 以下を実行
C:\work> bundle install (これで Gemfile.lock ができる)
C:\work> foreman start (http://localhost:5000 でテストする)

2. WebアプリをHerokuに配置

C:\work> heroku login
C:\work> git init
C:\work> git add .
C:\work> git commit -m "init"
C:\work> heroku keys:add
C:\work> heroku create (ここでherokuapp.com上のWebアプリ名が表示される)
C:\work> git push heroku master

これで、herokuapp.com上のWebアプリにブラウザでアクセスできる。