その他
    ホーム 技術発信 DoRuby railsでの環境使い分け production.rb development.rb

    railsでの環境使い分け production.rb development.rb

    この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。

    こんにちはtacchiです。

    今回はrails開発でよくやる環境の使い分けを書きます。

    短い記事ですが、お付き合い下さい。

    KBMJで開発を行う場合、production、developmentは次のようなシーンで 利用されます。

    本番環境:production

    個人の開発環境:development

    では、テストサーバとかどうすんの?

    とかの疑問が浮かぶと思うんですが、このようにしてます。

    本番環境:production

    個人の開発環境:development

    テストサーバ:stagingでは、staging環境の構築手順を記載します。

    まずはプロジェクト生成

    rails hogehoge

    create

    create app/controllers

    create app/helpers

    create app/models

    create app/views/layouts

    create config/environments

    create config/initializers

    create config/locales …

    staging作成

    以下のようなhogehogeプロジェクトが作成されます

    -rw-r–r– 1 tacchi staff 10011 8 4 11:10 README

    -rw-r–r– 1 tacchi staff 307 8 4 11:10 Rakefile

    drwxr-xr-x 6 tacchi staff 204 8 4 11:10 app

    drwxr-xr-x 9 tacchi staff 306 8 4 11:10 config

    drwxr-xr-x 2 tacchi staff 68 8 4 11:10 db

    drwxr-xr-x 3 tacchi staff 102 8 4 11:10 doc

    drwxr-xr-x 3 tacchi staff 102 8 4 11:10 lib

    drwxr-xr-x 6 tacchi staff 204 8 4 11:10 log

    drwxr-xr-x 11 tacchi staff 374 8 4 11:10 public

    drwxr-xr-x 11 tacchi staff 374 8 4 11:10 script

    drwxr-xr-x 8 tacchi staff 272 8 4 11:10 test

    drwxr-xr-x 6 tacchi staff 204 8 4 11:10 tmp

    drwxr-xr-x 3 tacchi staff 102 8 4 11:10 vendor

    staging.rbの作成

    cp config/environments/production.rb config/environments/staging.rbdatabase.ymlの編集 デフォルトで以下のようなconfig/database.ymlが作成されますので編集します。

    # SQLite version 3.x

    # gem install sqlite3-ruby (not necessary on OS X Leopard)

    development:

      adapter: sqlite3

      database: db/development.sqlite3

      pool: 5

      timeout: 5000

    # Warning: The database defined as “test” will be erased and

    # re-generated from your development database when you run “rake”.

    # Do not set this db to the same as development or production.

    test:

      adapter: sqlite3

      database: db/test.sqlite3

      pool: 5

      timeout: 5000

    production:

      adapter: sqlite3

      database:

      db/production.sqlite3

      pool: 5

      timeout: 5000

    staging追加

    staging:

    adapter: sqlite3 #この辺りは、postgresql、mysqlなどお好きなように

    database: db/production.sqlite3 #ここも同上

    pool: 5

    timeout: 5000

    最後の仕上げ

    これで、migrate流せばよいだけですねrake db:migrate RAILS_ENV=staging

    補足(script/consoleを実行する場合)

    ruby script/console -e staging

    補足(passengerを使う場合)

    apacheのconfの以下を変更(すでにpassengerの記述がある前提)RailsEnv production   ⇒ RailsEnv staging