6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby on Rails ~herokuにRailsアプリをデプロイ(アップロード)する方法(コードメモ)

6
Last updated at Posted at 2017-01-16

今回は、自作のRailsアプリをherokuにデプロイするまでの流れを紹介していきます。

【ポイント】
1.データベースはpostgresを指定する場合とMySQLを指定する2パターンを実装します。
2.deviseを導入している場合は、本番環境で使えるように設定しておきます。http://qiita.com/wtb114/items/176c19bd9caff0893d7c

1.Herokuにアカウント登録

下記URLからアカウントを登録します。
https://signup.heroku.com/identity

登録後、クレジットカードの登録を済ませます。無料で使う場合も必要です。
https://signup.heroku.com/identity

また、herokuのToolbeltをインストールして、herokuコマンドをターミナル上で入力できるようにしておきます。
https://devcenter.heroku.com/articles/getting-started-with-ruby#set-up

2.herokuにログイン

$ heroku login
# ユーザー名とパスワードを入力

3.Gemfileに以下を記述(データベースの設定)

・postgresを指定する場合

Gemfile


    ・・・

  gem 'mysql2', '0.3.18', group: :development #開発環境でMySQLを使用していた場合。

  ・・・

group :production do
  gem 'rails_12factor'
  gem 'pg'
end

・MySQLを指定する場合

Gemfile

    ・・・
    gem 'mysql2', '0.3.18'
    
    ・・・
   
group :production, :staging do
  gem 'rails_12factor'
end

Gemfile.lockを削除後

$ bundle install

4.作成したコードをバージョン管理システムに追加

$ git init
Reinitialized existing Git repository in /Users/ユーザー名/projects/アプリ名/.git/

$ rake assets:precompile

$ git add .

$ git commit -m "init commit"

5.アプリをデプロイ

$ heroku create
Creating app... done, ⬢ アプリ名
https://アプリ名.herokuapp.com/ | https://git.heroku.com/アプリ名.git

6.データベースを設定(MySQLを指定する場合のみ)

・postgresを指定する場合は必要ありません。

・MySQLを指定する場合

$ heroku addons:add cleardb
Creating cleardb on ⬢ アプリ名... free
Created cleardb-アプリ名 as CLEARDB_DATABASE_URL
Use heroku addons:docs cleardb to view documentation
$ heroku config | grep CLEARDB_DATABASE_URL
=> CLEARDB_DATABASE_URL: mysql://b1efe712be_______________________38710?reconnect=true 
# URL:後をコピーしておきます。

mysqlの部分をmysql2に変更

$ heroku config:set DATABASE_URL=mysql2://b1efe712be_______________________38710?reconnect=true 
# URL=後に先ほどコピーしたURLを貼り付け。「mysql」の箇所を「mysql2」に変更します。

7.アプリをプッシュ

$ git push heroku master

※別のブランチで作業している場合は

$ git push heroku ブランチ名:master

最後に heroku run rake db:migrate

$ heroku run rake db:migrate

$ heroku open 

自分のアプリが表示されていれば無事アップロードが完了です。

【補足】

データベースの情報を確認する場合は下記コマンドを入力します。

※postgresの場合

$ heroku config | grep DATABASE_URL

返ってきた情報の、postgres://〜が自分のデータベース情報です。

PG Commanderに接続してください。

PG Commanderのインストール
https://eggerapps.at/pgcommander/

返ってきた情報の、postgres://〜の部分を末尾まで選択してコピー後、「New Favorite」をクリックしてください。すると自動的に値がセットされます。

※MySQLの場合

$ heroku config --app アプリケーション名

返ってきた情報の、mysql2://〜が自分のデータベース情報です。

mysql2://ユーザー名:パスワード@ホスト名/データベース?reconnect=true

となっているので、この情報をSequelProに入力して、Herokuアプリのデータベースに接続してください。

6
8
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
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?