LoginSignup
4
6

More than 5 years have passed since last update.

Vagrant (CentOS7.2) + Ruby on Rails 5.0

Last updated at Posted at 2017-04-17

vagrant環境でRuby on Railsの環境を構築してみます。
目標:http://localhost:3000 で初期画面の表示

1. 環境

  • macOS Sierra
  • Vagrant 1.9.3

2. 仮想マシン起動

仮想マシンを起動してログインするまで

CentOS 7.2のbox追加

boxの一覧は以下
http://www.vagrantbox.es/

Box名はCentOS72とします。

$ vagrant box add CentOS72 {boxファイルのURL}

利用可能なboxの確認

$ vagrant box list
CentOS72          (virtualbox, 0)

Vagrantfile作成

$ mkdir CentOS72
$ cd CentOS72
$ vagrant init CentOS72

共有フォルダ作成

$ mkdir projects

Vagrantfile編集

Vagrantfile
 ︙
# ネットワーク設定
# config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 3000, host: 3000
 ︙
# 共有フォルダ設定
# config.vm.synced_folder "../data", "/vagrant_data"
  config.vm.synced_folder "./projects", "/home/vagrant/projects"

プロキシ環境下の場合

vagrant-proxyconfをインストール

$ vagrant plugin install vagrant-proxyconf

Vagrantfileに以下を追加

Vagrantfile
Vagrant.configure("2") do |config|
  if Vagrant.has_plugin?("vagrant-proxyconf")
    config.proxy.http = "http://<server>:<port>"
    config.proxy.https = "http://<server>:<port>"
  end

仮想マシン起動

$ vagrant up

仮想マシンログイン

$ vagrant ssh

3. Ruby on Rails 環境構築

事前準備

$ sudo yum update
$ sudo yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker readline-devel sqlite-devel

gitインストール

gitの最新版を入れていきます。
最新版の確認はこちらから。
https://git-scm.com/

ダウンロードURLはこちらからコピーしてください。
https://www.kernel.org/pub/software/scm/git/
執筆時は2.12.2が最新なので、git-2.12.2.tar.gzのリンクアドレスをコピーします。

$ cd /usr/local/src/
$ sudo curl -o git-2.12.2.tar.gz https://www.kernel.org/pub/software/scm/git/git-2.12.2.tar.gz
$ sudo tar vfx git-2.12.2.tar.gz
$ cd git-2.12.2
$ sudo make prefix=/usr/local all
$ sudo make prefix=/usr/local install
$ git --version
git version 2.12.2

※ Gitのバージョンを確認しても2.12.2と表示されない場合は、再度ログインし直してください。

$ exit
$ vagrant ssh

$ git --version
git version 2.12.2

rbenvインストール

リポジトリをclone
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

PATHを通す
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

bash実行時に rbenv initを実行するよう設定
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

.bash_profile更新
$ source ~/.bash_profile

インストール確認
$ rbenv --version
rbenv 1.1.0-2-g4f8925a

ruby-buildのインストール

$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

rubyのインストール

最新バージョンの確認



$ rbenv install -l

(略)
  2.3.3
  2.3.4
  2.4.0-dev
  2.4.0-preview1
  2.4.0-preview2
  2.4.0-preview3
  2.4.0-rc1
  2.4.0
  2.4.1
  2.5.0-dev
  jruby-1.5.6
  jruby-1.6.3

最新版である2.4.1をインストールします

$ rbenv install 2.4.1
Installing ruby-2.4.1...
Installed ruby-2.4.1 to /home/vagrant/.rbenv/versions/2.4.1

インストールされているRubyのバージョンを確認
$ rbenv versions
2.4.1

使用するRubyのバージョンを2.4.1に設定
$ rbenv global 2.4.1

使用中のRubyのバージョン確認
$ ruby --version
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]

Railsのインストール

※ この方法はグローバルにインストールするものなので、Railsのバージョン管理はできません。

$ gem install bundler --no-rdoc --no-ri
$ gem install rails

$ rails --version
Rails 5.0.2

アプリケーション作成

プロジェクトフォルダへ移動してアプリケーション作成

$ cd ~/projects/
$ rails new sample

Gemfileを編集

$ cd sample
$ vi Gemfile

以下の行のコメントアウトを外す

Gemfile
# gem 'therubyracer', platforms: :ruby
gem 'therubyracer', platforms: :ruby

Gemfileを更新したのでbundle install

$ bundle install

Bundle complete! 16 Gemfile dependencies, 65 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

Railsサーバー起動

$ rails s -b 0.0.0.0
=> Booting Puma
=> Rails 5.0.2 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.8.2 (ruby 2.4.1-p111), codename: Sassy Salamander
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

以下のURLへアクセスして確認
http://localhost:3000

アクセス時に次のようなメッセージが出る場合があります

Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Rails::WelcomeController#index as HTML

その場合はdevelopment.rbに以下を追加

config/environments/development.rb
config.web_console.whitelisted_ips = '0.0.0.0/0'

-b 0.0.0.0について

何も付けず、単に$ rails sとすると、localhost(127.0.0.1)にバインドされ、自分以外からのアクセスを弾きます。
-b 0.0.0.0あるいは--binding 0.0.0.0を付けることで、外部からのアクセスを許可させます。

Firewallについて

CentOS7など、バージョンによってはFirewallのサービスによりhttp://localhost:3000へアクセスできない場合があります。その場合は以下のコマンドを実行してポート3000を開けてください

$ firewall-cmd --permanent --add-port=3000/tcp

vagrant upでエラー

3000ポートが既に使われていたりするとここでエラーになります。LISTENとして使用されているポートの確認は以下のコマンドからできます。

$ sudo lsof -i -P | grep "LISTEN"
4
6
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
4
6