LoginSignup
0
0

More than 5 years have passed since last update.

他のホスティングサービスにホストしている既存のRails4アプリをHerokuにdeploy

Last updated at Posted at 2016-10-04

とあるWEBサービスを運営しているが、使わせてもらっているホスティングサービス自体が終了するそうなので、Herokuに移行した。その手順をサラッとメモしておく。

git でブランチを切る

必須ではないが、それなりに大改造になるので、git で新しいブランチを切って作業する。

$ git checkout -b task-deploy-heroku

新しい group を定義

:production を使い回せない場合、新しい group を作る。
ここでは :production_heroku とする。
ちなみに :production-heroku(ハイフン) とした場合、Gemfileのパースでエラーが発生する。

また、GitHub上の野良gem(?)を使っている場合、記述方法によってはHerokuへのpush時にエラーになるので注意。

Gemfile
ruby '2.2.4'  # Herokuの場合、rubyバージョンを明示していないとWARNINGが出る。

# 野良gemは git: ではなく、github: で指定するように変更。
# gem 'mongoid-rspec', git: "git@github.com:chocoken517/mongoid-rspec.git"
gem 'mongoid-rspec', github: "chocoken517/mongoid-rspec"

group :production_heroku do
  # for Heroku
  gem 'pg'
  gem 'rails_12factor'
end
database.yml
production_heroku:
  <<: *default
  adapter: postgresql
  encoding: unicode
  pool: 5
secrets.yml
production_heroku:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
config/environment.rb
case Rails.env
  # when 'development' then
  # when 'staging' then
  when 'production' then
    xxx 
  when 'production_heroku' then
    # もし何か定義していたら追加
    xxx 
  else
end
config/environments/production_heroku.rb
# 中身は production.rb をコピーして、必要な部分だけを変更する。。

gitにコミットする。

Heroku で新しいAppを登録する

GUIで出来るので詳細は割愛。

デプロイ

git remote の追加

参考: Creating a Heroku remote

$ heroku git:remote -a アプリケーション名 [-r リモート名(デフォルトはheroku)]
$ git remote -v     ★以下が追加されていることを確認
heroku  https://git.heroku.com/アプリケーション名.git (fetch)
heroku  https://git.heroku.com/アプリケーション名.git (push)
環境変数の設定

GUIでも出来るが、コマンドラインで設定する方が簡単。※GUIで行うなら、Settings の Config Variables から。

production_herokuグループで動かすための設定。

$ heroku config:add RACK_ENV=production_heroku
$ heroku config:add RAILS_ENV=production_heroku

日本向けRailsアプリケーションなら以下を。
参考: 日本向けRailsアプリケーションをHerokuで走らせるためのまとめ

$ heroku config:add TZ=Asia/Tokyo
$ heroku config:set LANG=ja_JP.UTF-8

※正直これらの設定の効果が分からない…(2016/10/05現在)

git push の実行

最初の手順でブランチを切っているので、そのブランチを master として push する。参考: Advanced: Linking local branches to remote apps

$ git push heroku task-deploy-heroku:master

※ほとぼりが冷めて、masterにマージした暁には、git push heroku master でOK。
※そもそもmasterブランチのまま作業している場合は git push heroku master でOK。

DBのセットアップ
$ heroku run rake db:migrate
アプリをOPENしてみる

何も問題なければ正常に動作するはず。

その他

herokuコマンドについてこちらを参考に。Heroku コマンド・設定 メモメモ

ログ参照(tail)
$ heroku logs -t [-a <application name>]
アプリをメンテナンスモードにする

アプリページにアクセスすると、「メンテナンス中です」的なstaticページが表示される。参考: Maintenance Mode

$ heroku maintenance:on   # メンテナンスモードに
$ heroku maintenance:off  # 復旧
psqlでDBを参照・操作

※前提: git remote に heroku が定義されていること。そして、psqlにPATHが通っていること。

$ heroku pg:psql

Herokuに移行してみて

  • USリージョンはやはり体感速度(レイテンシー)が遅い
    そうでもなかった(~_~;) ただし、アクセスがない状態で一定時間経過するとアプリがスリープするので、そのあとの復帰には数秒かかってしまう。

  • 無料だとDB(PostgreSQL)に10000行しか格納できない。
    PostgreSQLをやめて、無料版でも500MBまで格納できるMongoDBに移行した。(移行手順はこちら。) ActiveRecordと完全に互換性があるわけではないので、ある程度のコード修正は必要だったが、ちゃんとMongoDBに移行できた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0