Engineer Llfe Dogear

エンジニアリング活動におけるメモやTipsを書くブログ

Postgresqlの設定からRailsアプリケーションをherokuで動かすまで⑥

Railsの新規アプリケーションをherokuにデプロイする手順

今回は自前で作成したRailsアプリケーションをHerokuへデプロイする手順について説明していきます。

1. Railsアプリケーションの生成

まず、myAppという名前でRailsアプリケーションを作成します。 databaseのオプションとして -d postgresqlを指定します。 postgresql

$ rails new myApp -d postgresql // myAppというRails アプリケーションが生成
$ cd myApp

2. ベースとなるモデル、ビュー、コントローラの作成

Railsのscaffoldで、サンプルのモデル、ビュー、コントローラのひな形を作成します。 今回はtitle、authorをもつbookモデルを生成します。

$ rails generate scaffold book title:string author:string

3. Gemfileの修正とgemのインストール

以下のことを実現するためにGemfileの修正を行います。 * WebサーバをPumaで動かすためpumaの追加 * Rails4をHerokuで動かすためにrails_12factorを追加 * RailsJavaScriptのエンジンであるV8を動かすためtherubyracerを追加

source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby  // コメントアウトを削除
gem 'rails_12factor'    // 追加
gem 'puma'  // 追加
# Use jquery as the JavaScript library

更新したgemファイルをinstallします。

$ bundle install

4. WebサーバのPumaへの変更

Procfileを作成し、以下の記述を追加します。

web: bundle exec puma -C config/puma.rb

config/puma.rbを作成し、以下の通り設定(設定は、herokuサンプルのものを参考)

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/deploying-rails-applications-    with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end

5. DBの設定変更

PostgresqlでDBが生成できるよう、ユーザ名などの設定を変更します。 config/database.ymlの設定test、develop項目に関して以下の通り設定します。

// PostgresqlのDB作成権限をもつユーザ名とパスワードを指定します。
// ここでは、それぞれuser、fooを指定しています。
development:
  <<: *default
  database: myapps_development
  username: user
  password: foo

test:
  <<: *default
  database: myapps_test
  username: user
  password: foo

6. ローカルでの動作確認

ローカル環境で、Rails Applicationが動作することを確認します。(※ こちらは前回の記事と同じです) まず、DBの作成

// 事前にpostgresqを起動していることを確認
$ bundel exec rake db:create db:migrate

次に、アプリの起動

foreman start web

ブラウザでhttp://localhost:5000/booksにアクセスし、作成したRailsアプリが起動していることを確認します。

7. railsアプリケーションのgitでの管理設定

まず、作成しているrailsアプリケーションのrootディレクトリでgitの管理を設定します。

git init

次に、.gitignoreを追加し、以下の通り設定します。(herokuのサンプル or herou createで作成される.gitignoreを参考) 必要に応じて、ignoreするファイルを追加します。

# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
#   git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore all logfiles and tempfiles.
/log/*
!/log/.keep
/tmp

gitで管理するファイルの追加とコミットし

$ git add . //今回は、rootディレクトリ以下のものを管理するようにする
$ git commit -m "initial commit"

8. herokuでの動作確認

herokuへのログイン

$ heroku login

作成したrailsのrootディレクトリで以下のコマンドを実行します。(gitのremoteにheroku用の設定が追加される)

$ heroku create

index.htmlをpublic以下に追加 ※ 追加しないと、herokuにrails アプリケーションをpushしたときにerrorとなる

herokuのremoteへアプリをpushします。

$ git push heroku master

デプロイしたアプリを開きます。

$ heroku open

web ブラウザーで、デプロしたアプリが開かれます。

このとき、以下のようなエラーが出た場合、heroku側のDBを再設定します。

2015-06-21T06:40:16.916385+00:00 app[web.1]: Started GET "/app_infos" for 36.12.227.221 at 2015-06-21 06:40:16 +0000
2015-06-21T06:40:16.929108+00:00 app[web.1]:   AppInfo Load (8.2ms)  SELECT "app_infos".* FROM "app_infos"
2015-06-21T06:40:16.929120+00:00 app[web.1]: PG::UndefinedTable: ERROR:  relation "app_infos" does not exist
2015-06-21T06:40:16.929122+00:00 app[web.1]: LINE 1: SELECT "app_infos".* FROM "app_infos"
2015-06-21T06:40:16.929123+00:00 app[web.1]:                                   ^
2015-06-21T06:40:16.929124+00:00 app[web.1]: : SELECT "app_infos".* FROM "app_infos"
2015-06-21T06:40:16.929932+00:00 app[web.1]:   Rendered app_infos/index.html.erb within layouts/application (9.8ms)
2015-06-21T06:40:16.931850+00:00 app[web.1]:
2015-06-21T06:40:16.919048+00:00 app[web.1]: Processing by AppInfosController#index as HTML
2015-06-21T06:40:16.930101+00:00 app[web.1]: Completed 500 Internal Server Error in 11ms (ActiveRecord: 8.2ms)
2015-06-21T06:40:16.931853+00:00 app[web.1]: ActionView::Template::Error (PG::UndefinedTable: ERROR:  relation "app_infos" does not exist
2015-06-21T06:40:16.931855+00:00 app[web.1]: LINE 1: SELECT "app_infos".* FROM "app_infos"
2015-06-21T06:40:16.931856+00:00 app[web.1]:                                   ^

再設定は以下の通り

$ heroku rake db:reset
$ heroku rake db:migrate

以上が、自前で作成したRailsアプリケーションをherokuにデプロイする手順に関してです。

参考文献