Ruby on Railsの初期設定

Bundlerを使って、 Rails 4.1, Unicorn HTTP server, PostgreSQL の組み合わせで初期設定します。

BundlerでRailsをinstall

最初からbundlerを使ってRailsをinstallします。 Gemを使ってinstallする場合は、${HOME}/.gem 以下にRailsがinstallされますが、 以下の方法ではすべてrails_test01/vendor/bundle以下にinstallされます。 複数のversionを試したりする場合にはこの方法をお勧めします。

$ mkdir rails_test01
$ cd rails_test01
$ vi Gemfile
$ bundle install --path vendor/bundle

Gemfileには以下の内容を書きます。

source 'https://rubygems.org'

gem 'rails', '~> 4.1.0'
gem 'pg'
gem 'sprockets', '~> 2.10.0'
gem 'therubyracer', platforms: :ruby
gem 'unicorn'

Railsの初期化

rails new で Railsの初期化を行います。

$ bundle exec rails new .

途中で"Overwrite Gemfile?" と聞かれるので、"Y"と答えて上書きします。 rails newの後、Gemfileが書き換えられてしまうので、 Gemfileに再度pg, therubyracer, unicornを追記します。

Unicornの仮設定

config/unicorn.rbに以下を書きます。

worker_processes 4
listen 3000

Unicornの動作確認

Unicorn HTTP serverを走らせて動作確認してみます。 上記のconfig/unicorn.rbに書いた TCP 3000 で起動しますので、 http://localhost:3000/ にaccessしてUnicornが起動していることを確認します。 この時点ではPostgreSQLではなくSQLiteで動いています。 Ctrl+Cで停止できます。

$ bundle exec unicorn_rails -c config/unicorn.rb -E development

PostgreSQLの設定

まったく新しくPostgreSQLをinstallした場合、 以下のようにしてdatabaseを初期化します。

# su -l postgres -c 'initdb --auth-host=md5 --locale=C --encoding=utf-8 --pgdata=/var/lib/pgsql/data'

続いて、/var/lib/pgsql/data/pg_hba.conf に localhost からの TCP/IP接続を受け付ける設定があるか確認し、なければ追記します。 (上記の initdb を行った場合はあるはずです)

local   all             all                                     trust
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5

pg_hba.confの確認を行なったら、PostgreSQLを起動します。 systemctl(Fedora>=15,RHEL>=7) もしくは service を使って以下のように起動します。

# systemctl start postgresql.service
or
# service postgresql start

PostgreSQLにRails用のdatabaseとuserを作成

Rails用のuserとdatabaseをPostgreSQL上に準備します。 railsuser, railsdb, password を適宜置き換えてください。

# su - postgres -c 'psql'
psql (9.2.7)
Type "help" for help.

postgres=# CREATE ROLE railsuser NOSUPERUSER CREATEDB NOCREATEROLE NOINHERIT VALID UNTIL 'infinity';
CREATE ROLE
postgres=# ALTER ROLE railsuser LOGIN ENCRYPTED PASSWORD 'password';
ALTER ROLE
postgres=# CREATE DATABASE railsdb WITH ENCODING='UTF8' OWNER=railsuser;
CREATE DATABASE
postgres=# \q

database.ymlの設定

config/database.ymlを変更して、production環境の場合にPostgreSQLを使うように設定します。

production:
  adapter: postgresql
  database: railsdb
  host: localhost
  username: railsuser
  password: password
  encoding: utf8

ここまで設定できたら、 Unicornをproduction環境で実行してみて 問題なく動作していることを確認します。

$ export SECRET_KEY_BASE=0123456789
$ bundle exec unicorn_rails -c config/unicorn.rb -E production

SystemdでUnicornを起動する設定

SystemdでOS起動時にUnicornを自動的に起動させる方法です。 まず、Unicorn HTTP server実行用のuser&groupを用意します。

$ groupadd -r unicorn
$ useradd -r -g unicorn -d /var/www/unicorn unicorn

続いて以下のような内容の/etc/systemd/system/unicorn.serviceと /var/www/unicorn/.railsenv を書きます。

/etc/systemd/system/unicorn.service

[Unit]
Description=Unicorn HTTP server
After=syslog.target network.target postgresql.service

[Service]
Type=simple
User=unicorn
Group=unicorn
WorkingDirectory=/var/www/unicorn/railsapp
EnvironmentFile=/var/www/unicorn/.railsenv
ExecStart=/usr/bin/bundle exec unicorn_rails -c config/unicorn.rb -E production
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -QUIT $MAINPID

[Install]
WantedBy=multi-user.target

/var/www/unicorn/.railsenv

SECRET_KEY_BASE=01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567

以下のようにしてsystemd経由で起動、停止を行えるか確認します。

# systemctl start unicorn.service
# systemctl status unicorn.service
# systemctl stop unicorn.service

正しく動作することが確認できれば、最後に以下のようにしてOS起動時に Unicorn HTTP serverを自動起動するように設定します。

# systemctl enable unicorn.service

更新 : 2014-01-01
ご意見、ご感想は、花房 真広 <webmaster@hanabusa.net>まで。メールする前にtop pageの注意書を読んでください。