LoginSignup
17
19

More than 5 years have passed since last update.

Ruby on Rails 4.2のインストール・環境構築(mysql)

Last updated at Posted at 2015-02-24

vagrantの環境にRails4.2の環境を構築していきます。
ベースのOSはCentOSとなります。

# cat /etc/redhat-release
CentOS release 6.5 (Final)

hostsの設定

hosts関連からIPv6関連のlocalhostの設定を消しておきます。
localhostの記述をする際におかしな動作をすることがあるので。

# vi /etc/hosts
以下の行を消す
-----------------------
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
-----------------------

iptablesのoff

iptablesはOFFにしておきます。
FWとしての機能はローカル開発環境では利用しませんので。

# chkconfig iptables off
# /etc/init.d/iptables stop

gitのインストール

gitのインストールをします。
何をするにもまずgit。

# yum -y install git

rbenvのインストール

今回、Rubyはrbenvでインストールします。
rbenvはRuby環境のバージョン切り替えツールですので、インストール後などに別の固有環境を作る際、簡単バージョンの切り替えができるようになります。

# yum -y install gcc-c++ glibc-headers openssl-devel readline libyaml-devel readline-devel zlib zlib-devel libxml2 libxslt libxml2-devel libxslt-devel

# chmod 777 /var/local/

# git clone https://github.com/sstephenson/rbenv.git /usr/local/rbenv
# echo 'export RBENV_ROOT="/usr/local/rbenv"' >> /etc/profile
# echo 'export PATH="${RBENV_ROOT}/bin:${PATH}"' >> /etc/profile
# echo 'eval "$(rbenv init -)"' >> /etc/profile

いったんターミナル閉じたあとにバージョンの確認をします。

# exit
$ exit

# rbenv --version
rbenv 0.4.0-133-g98953c2

ruby-buildインストール

ruby-buildはrbenvのプラグインで、 rubyをインストールするために使われます。

# git clone https://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build

rubyインストール

# rbenv install -l
-----------------------
Available versions:
 ~省略~
  2.1.4
  2.1.5
  2.2.0-dev
  2.2.0-preview1
  2.2.0-preview2
  2.2.0-rc1
  2.2.0
  2.3.0-dev
  jruby-1.5.6
 ~省略~
-----------------------

今回は2.1系でstableな2.1.5を利用します。

# rbenv install -v 2.1.5
# rbenv rehash
# rbenv global 2.1.5

# ruby -v
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]

Rails4.2のインストール

# gem update --system

# gem install nokogiri -- --use-system-libraries
# gem install rails -v 4.2.0
# gem install bundler
# rbenv rehash

# rails -v
Rails 4.2.0

nokogiriのインストールがおかしくなり4.2系がうまく入らなかったので、「--use-system-libraries」でのインストールを行いました。

mysqlインストール

DBでmysqlを利用したいので以下の手順で入れていきます。

# yum -y install mysql-server mysql-devel
# service mysqld start
# chkconfig mysqld on

本来であれば以下のようにrootパスワードの設定が必要です。

# mysql -u root -p

mysql> use mysql;
mysql> SET PASSWORD FOR root@localhost=PASSWORD('ランダム文字列');
mysql> delete from user where User='root' and Password='';

サンプルプロジェクトの作成

「sample_project」という名前のプロジェクトの作成をしていきます。

# cd /vagrant
# rails new sample_project -d mysql
# cd sample_project

# vi Gemfile
-----------------------
gem 'therubyracer',  platforms: :rubyをコメントイン
-----------------------

# bundle install

「therubyracer」のコメントを外すのは、ほぼ初期お作法になっているのでならっておきます。

データベースの作成

データベースの作成を行います。

mysql> create database sample_project_production \
       default character set utf8 \
       collate utf8_unicode_ci;
mysql> create database sample_project_development \
       default character set utf8 \
       collate utf8_unicode_ci;

mysql> create database sample_project_test \
       default character set utf8 \
       collate utf8_unicode_ci;

mysql> create user 'sample_user'@'localhost' identified by 'samplepass';
mysql> grant all on sample_project_production.* to 'sample_user'@'localhost';
mysql> grant all on sample_project_development.* to 'sample_user'@'localhost';
mysql> grant all on sample_project_test.* to 'sample_user'@'localhost';

設定ファイルへの書き込み

# vi config/database.yml
-----------------------
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:
  socket: /var/lib/mysql/mysql.sock

development:
  <<: *default
  database: sample_project_development
  username: sample_user
  password: samplepass

# 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:
  <<: *default
  database: sample_project_test
  username: sample_user
  password: samplepass

production:
  <<: *default
  database: sample_project_production
  username: sample_user
  password: samplepass
  username: sample_project

-----------------------

初期ではproductionの環境は「password: <%= ENV['SAMPLE_PROJECT_DATABASE_PASSWORD'] %>」となっています。

環境変数にパスワードを書くことで、Gitなどでソースを管理した際にも、
パスワード情報を公開しないためとなります。

本来的には環境変数で実施しますが、今回は試験用なのでベタ打ちします。

Webrickを立ち上げて、ホストからゲストへのアクセス

Webrickの立ち上げを行います。

# rails s -b 0.0.0.0

ここで注意なのがrails4.1まではバインドIPの指定がなかったですが、rails4.2系からはこの指定をしないとアクセスが出来なくなります。

こちらで無事初期画面が出てきました。

image.png

試験環境の構築はこちらで完了です。

17
19
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
17
19