LoginSignup
2
1

More than 3 years have passed since last update.

herokuにruby on railsアプリをmysqlを使用してデプロイする。deploy

Posted at

前提条件

nodeとyarnは必ず最新にしておいてください。
それは書きません。

プロジェクトの作成

noracorn-railsというプロジェクトで作成します

ghq get https://github.com/noracorn/noracorn-rails.git
rails new noracorn-rails
cd noracorn-rails

scaffoldで、適当にサイトを作成

rails generate scaffold person name:string age:integer
rake db:migrate

トップページをいったんscaffoldで作成したものにする

noracorn-rails/config/routes.rb
Rails.application.routes.draw do
  resources :people
  root 'people#index'
end

立ち上がることを確認しましょう

以下のコマンドを打ってから、http://localhost:3000/にアクセス
Personのscaffoldにアクセスできることを確認する

bundle install
rails server

herokuでアプリを作成する

heroku login
heroku create noracornrails

herokuにmysqlを追加

(herokuにクレジットカードを登録してないと追加できません。)

heroku addons:add cleardb:ignite -a noracornrails

database設定

noracorn-rails/database.yml
production:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>

以下のコマンドを打つと、データベース接続情報が表示されます。

heroku config

こんな感じで表示されます

mysql://user_xxx:password_xxx@host_xxx/dename_xxx?reconnect=true

表示された接続情報をherokuの環境変数に入れていく

heroku config:set DATABASE_URL="mysql2://user_xxx:password_xxx@host_xxx/dename_xxx?reconnect=true"
heroku config:set DATABASE_USERNAME="user_xxx"
heroku config:set DATABASE_PASSWORD="password_xxx"

config.assets.initialize_on_precompile = falseを、application.rbに入れる

noracorn-rails/config/application.rb
module NoracornRails
  class Application < Rails::Application
    config.load_defaults 6.0
    config.assets.initialize_on_precompile = false
  end
end

production.rbのconfig.assets.compileをtrueにする

noracorn-rails/config/environments/production.rb
config.assets.compile = true

Gem Fileを編集する。

ローカル環境とテストでは、sqlite3を使用してherokuではmysqlを使用するようにした。
上のほうで定義されているsqlite3は、コメントアウトしておいてください。

#gem 'sqlite3', '~> 1.4'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'sqlite3', '~> 1.4'
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'sqlite3', '~> 1.4'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :production do
  gem 'mysql2'
end

bundle installする

bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

bundle install

すべてコミットして、herokuにpush

git add .
git commit
git push origin master
heroku git:remote --app noracornrails
git push heroku master

herokuのmysqlをマイグレーション

heroku run rails db:migrate

アプリが動いているか確認する

heroku open
2
1
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
2
1