Vagrantの環境にRailsをインストールしてみる

Vagrantを試してみる
の続き

Vagrantの環境にRailsをインストールしてみる。

Vagrantの環境作成

centos64のBoxが無ければ追加しておく。

mac$ vagrant box add centos64 https://github.com/2creatives/vagrant-centos/releases/download/v0.1.0/centos64-x86_64-20131030.box

vagrant用のディレクトリを作成してinitし、

mac$ mkdir sample_vagrant
mac$ cd sample_vagrant
mac$ vagrant init centos64

IPアドレスを設定する。

mac$ vi Vagrantfile
config.vm.network "private_network", ip: "IPアドレス"	←コメントを解除して、IPアドレスを設定する

起動して、

mac$ vagrant up

sshで接続する。

mac$ vagrant ssh

Railsのインストール

rootになる。

$ su -

以下のコマンドで、Railsをインストールする。
コピペして実行すればOK。

yum -y install gcc gcc-c++ make openssl-devel zlib-devel readline-devel wget httpd curl-devel httpd-devel sqlite-devel
chkconfig httpd on

cd /tmp
wget ftp://core.ring.gr.jp/pub/lang/ruby/2.0/ruby-2.0.0-p353.tar.gz
tar xzf ruby-2.0.0-p353.tar.gz
cd ruby-2.0.0-p353
./configure --with-opt-dir=/usr/local --enable-shared --enable-option-checking
make
make test
make install

echo "options single-request-reopen" >> /etc/resolv.conf
gem install --no-ri --no-rdoc passenger rails sqlite3
passenger-install-apache2-module --auto
passenger-install-apache2-module --snippet > /etc/httpd/conf.d/passenger.conf

echo "RailsEnv development" >> /etc/httpd/conf/httpd.conf

exit
railsのinstallに時間がかかるので、resolv.confを設定している。
【参考】
Vagrant+VirtualBox(CentOS6)で「gem install rails」がすっごい遅い時の対処法 | IDEA*IDEA
http://www.ideaxidea.com/archives/2013/08/resolv.html

Railsアプリの作成

vagrantの共有ディレクトリに移動して、railsのアプリをnewする。

$ cd /vagrant
$ rails new sample_rails

Gemfileを修正し、bundle installする。

$ cd sample_rails
$ vi Gemfile
gem 'therubyracer', platforms: :ruby	←コメントアウトを解除する
$ bundle install

DocumentRootlの設定を設定して、Apacheを起動する。

$ sudo vi /etc/httpd/conf/httpd.conf
#DocumentRoot "/var/www/html" ←コメントアウト
DocumentRoot "/vagrant/sample_rails/public" ←追加
$ sudo service httpd start

以上で、
http://ipアドレス/
Railsのページを表示できる。

また、mac側から
…/sample_vagrant/sample_rails
のソースを編集できる。