LoginSignup
158
171

More than 5 years have passed since last update.

[Rails]production環境で動かす

Last updated at Posted at 2014-12-02

備忘録的な

アセットのプリコンパイル

アセットファイル:css, jsファイルを圧縮したものが出来上がる

$bundle exec rake assets:precompile RAILS_ENV=production
$ls -al  public/assets/
合計 168
drwxrwxr-x 2 rails rails   4096 12月  2 16:15 2014 .
drwxrwxr-x 3 rails rails   4096 12月  2 15:51 2014 ..
-rw-rw-r-- 1 rails rails 112986 12月  2 15:49 2014 application-40a3084d920836679c47402189d51b8c.js
-rw-rw-r-- 1 rails rails  38458 12月  2 15:49 2014 application-40a3084d920836679c47402189d51b8c.js.gz
-rw-rw-r-- 1 rails rails      0 12月  2 15:49 2014 application-9cc0575249625b8d8648563841072913.css
-rw-rw-r-- 1 rails rails     20 12月  2 15:49 2014 application-9cc0575249625b8d8648563841072913.css.gz
-rw-rw-r-- 1 rails rails    511 12月  2 15:51 2014 manifest-0e23dfa8f406c5b2a001045f345b4b25.json

manifest-0e23dfa8f406c5b2a001045f345b4b25.jsonには、コンパイル対象のファイルパス、作成時間、サイズ、などの情報が含まれる

manifest-0e23dfa8f406c5b2a001045f345b4b25.json
{
    "assets": {
        "application.css": "application-9cc0575249625b8d8648563841072913.css", 
        "application.js": "application-40a3084d920836679c47402189d51b8c.js"
    }, 
    "files": {
        "application-40a3084d920836679c47402189d51b8c.js": {
            "digest": "40a3084d920836679c47402189d51b8c", 
            "logical_path": "application.js", 
            "mtime": "2014-12-02T15:49:08+09:00", 
            "size": 112986
        }, 
        "application-9cc0575249625b8d8648563841072913.css": {
            "digest": "9cc0575249625b8d8648563841072913", 
            "logical_path": "application.css", 
            "mtime": "2014-12-02T15:49:08+09:00", 
            "size": 0
        }
    }
}


production DBの作成

パスワードは環境変数で参照する。

database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: 
  password: 
  socket: /var/lib/mysql/mysql.sock
~
~
production:
  <<: *default
  database: json_server_production
  username: json_server
  password: <%= ENV['JSON_SERVER_DATABASE_PASSWORD'] %>
$JSON_SERVER_DATABASE_PASSWORD=password bundle exec rake db:create RAILS_ENV=production
$JSON_SERVER_DATABASE_PASSWORD=password bundle exec rake db:migrate RAILS_ENV=production

SECRET_KEY_BASEの設定

config/secrets.ymlに設定が書かれている。セッションでやり取りされるクッキーの暗号化に使われる。

config/secrets.yml

development:
  secret_key_base: 80421ba10b4abb9effa4ef827ae91c7630f494763e20ad1dc708c88d1f81926fec250181a29d4317149b373d3f66c4cc985d55466f4d31915f79fed042fbad97

test:
  secret_key_base: 7a68883502f6a2278259b72f22b01ddfb1f81bc13da6e47c01a520d0423a42691d838b6fd176bdbc5ef5602e8c65d4c6e2dfb0897fb486a6fa408f70827b921e

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

production用の環境変数SECRET_KEY_BASE設定

$bundle exec rake secret
ランダム文字列

$SECRET_KEY_BASE=ランダム文字列
$export SECRET_KEY_BASE
$env | grep SECRET_KEY_BASE
SECRET_KEY_BASE=ランダム文字列

アセットの提供設定(WEBrickでの起動のみ)

nginxまたはapacheをwebサーバとして使っている場合は必要ない。

config/enviroments/production.rb
# Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = true  

4.2では

config/environments/production.rb

 # Apache or NGINX already handles this.
  config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

となっていたため、RAILS_SERVE_STATIC_FILES環境変数に何かしら値を設定してあげる

$ RAILS_SERVE_STATIC_FILES=1
$ export RAILS_SERVE_STATIC_FILES
$ env | grep RAILS
RAILS_SERVE_STATIC_FILES=1

サーバ起動

パスワードを指定して起動

$JSON_SERVER_DATABASE_PASSWORD=password bundle exec rails s -e production

4.2の場合、-b (バインディングオプション)でbindするIPアドレスを指定
0.0.0.0の場合、すべてのインタフェースでリッスンをする *:3000のイメージ。リッスンしたいIPがあればそれを指定。

$JSON_SERVER_DATABASE_PASSWORD=password bundle exec rails s -e production -b 0.0.0.0 

158
171
2

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
158
171