RailsアプリのホスティングにHerokuを利用する
Railsでアプリを作ると、実行環境の用意がけっこう大変です。
Centosもそうですが、まず、最新のRubyをインストールするのが一手間です。
なので、今回はHerokuを使ってみることにしました。
Cloud Application Platform | Heroku
Railsの実行環境を提供してくれるサービスです。
フリープランでは5MBまで無料でデータベースが使えます。
嬉しいことにDBMSはPostgreSQLです。
環境
- Rails 2.3.5
- Ruby 1.8.7
- msysgit 1.6.5.1
- heroku gem 1.8.2
- windows7
railsアプリケーションの作成
いつも通り作ればOKです。
gitの実行
msysgitでインストールされる、Git Bashを使います。
Git Bashでアプリケーションのディレクトリに移動しておきます。
Welcome to Git (version 1.6.5.1-preview20091022) Run 'git help git' to display the help index. Run 'git help <command>' to display help for specific commands. toshifusa@TOSHIFUSA-PC ~/Documents/NetBeansProjects/sample $
git initでリポジトリの初期化をします。
$ git init Initialized empty Git repository in C:/Users/toshifusa/Documents/NetBeansProject s/sample/.git/ toshifusa@TOSHIFUSA-PC ~/Documents/NetBeansProjects/sample (master) $
まずは全てのファイルを追加しますので、git addコマンドの引数 "."(ピリオド)を忘れないように。
$ git add . warning: LF will be replaced by CRLF in README ~中略~ warning: LF will be replaced by CRLF in test/unit/livedoorweather_test.rb toshifusa@TOSHIFUSA-PC ~/Documents/NetBeansProjects/sample (master) $
コミットします。
$ git commit -m "new app" [master (root-commit) 5598d5d] new app warning: LF will be replaced by CRLF in README ~中略~ warning: LF will be replaced by CRLF in test/unit/livedoorweather_test.rb 57 files changed, 10900 insertions(+), 0 deletions(-) create mode 100644 README ~中略~ create mode 100644 test/unit/livedoorweather_test.rb toshifusa@TOSHIFUSA-PC ~/Documents/NetBeansProjects/sample (master) $
Herokuにアプリを作成
heroku createコマンドを実行すると、適当な名前でアプリケーションがHeroku側に作成されます。
アプリケーション名は後ほど変更可能です。
初回のみメールアドレス、パスワードを聞かれます。
$ heroku create Creating empty-mist-34... done! Created http://empty-mist-34.heroku.com/ | git@heroku.com:empty-mist-34.git Git remote heroku added toshifusa@TOSHIFUSA-PC ~/Documents/NetBeansProjects/sample (master) $
Herokuにアプリをpush
pushの際には毎回、事前にGit GUIで作っておいた、ssh keyのパスフレーズを聞かれます。
$ git push heroku master Enter passphrase for key '/c/Users/toshifusa/.ssh/id_rsa': Counting objects: 87, done. Delta compression using up to 2 threads. Compressing objects: 100% (77/77), done. Writing objects: 100% (87/87), 87.08 KiB, done. Total 87 (delta 10), reused 0 (delta 0) -----> Heroku receiving push -----> Rails app detected Compiled slug size is 80K -----> Launching..... done http://empty-mist-34.heroku.com deployed to Heroku To git@heroku.com:empty-mist-34.git * [new branch] master -> master toshifusa@TOSHIFUSA-PC ~/Documents/NetBeansProjects/sample (master) $
まとめ
一度環境を作ってしまえば、デプロイに必要なコマンドはたったこれだけです。
rails myapp cd myapp git init git add . git commit -m "my new app" heroku create git push heroku master
その他の機能
Gemのインストール、Railsコンソール、Rakeの実行、データベースへのデータインポートの機能も備えています。
便利です。