15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rbenv + rbenv-gemset + apache + passenger を Mac に入れる

Last updated at Posted at 2014-04-03

最後にエラーが出て悩んだのでメモ代わりに残します。

rbenv, ruby-build, rbenv-gemset のインストール

Homebewを使ってrbenvの環境を構築します。
途中で依存関係等でエラーが出た場合は、都度対処してください。

bash
~ lassy423$ brew install rbenv ruby-build rbenv-gemset
==> Downloading https://github.com/sstephenson/rbenv/archive/v0.4.0.tar.gz
######################################################################## 100.0%
==> Caveats
To use Homebrew's directories rather than ~/.rbenv add to your profile:
  export RBENV_ROOT=/usr/local/var/rbenv

To enable shims and autocompletion add to your profile:
  if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
==> Summary
?  /usr/local/Cellar/rbenv/0.4.0: 31 files, 152K, built in 3 seconds
==> Downloading https://github.com/sstephenson/ruby-build/archive/v20140225.tar.gz
######################################################################## 100.0%
==> ./install.sh
?  /usr/local/Cellar/ruby-build/20140225: 114 files, 496K, built in 4 seconds
==> Downloading https://github.com/jf/rbenv-gemset/archive/v0.5.6.tar.gz
######################################################################## 100.0%
?  /usr/local/Cellar/rbenv-gemset/0.5.6: 12 files, 52K, built in 3 seconds
~ lassy423$ 

~/.bashrc と ~/.profile に追記します

~/.bashrc
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
~/.profile
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

追記した内容を読み込み直します。

bash
~ lassy423$ source ~/.profile

ruby のインストール

任意のバージョンの ruby をインストールします。例として 2.1.1 を使用します。

bash
~ lassy423$ rbenv install --list
Available versions:
  1.8.6-p383
  1.8.6-p420
  1.8.7-p249
(中略)
  2.1.0-rc1
  2.1.1
  2.2.0-dev
(中略)
  ree-1.8.7-2012.01
  ree-1.8.7-2012.02
  topaz-dev
~ lassy423$ rbenv install 2.1.1
Downloading ruby-2.1.1.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/e57fdbb8ed56e70c43f39c79da1654b2
Installing ruby-2.1.1...
Installed ruby-2.1.1 to /Users/Yoshihiko/.rbenv/versions/2.1.1

~ lassy423$ rbenv rehash
~ lassy423$ rbenv versions
* system (set by /Users/Yoshihiko/.rbenv/version)
  2.1.1
~ lassy423$ rbenv global 2.1.1
~ lassy423$ rbenv versions
  system
* 2.1.1 (set by /Users/Yoshihiko/.rbenv/version)
~ lassy423$ 

gemset を作成する

rbenv-gemset を使ってアプリケーションごとの gemset を作成します。

bash
~ lassy423$ rbenv gemset
version 0.5.6
rbenv-gemset [command] [options]

possible commands are:
  active
  create [version] [gemset]
  delete [version] [gemset]
  file
  init [gemset]
  list
  version

For full documentation, see: https://github.com/jf/rbenv-gemset#readme
~ lassy423$ rbenv gemset list
~ lassy423$ rbenv gemset create 2.1.1 demosite-gemset
created demosite-gemset for 2.1.1
~ lassy423$ rbenv gemset list
2.1.1:
  demosite-gemset
~ lassy423$ 

作成した gemset をアプリケーション配下で有効になるよう設定します。

bash
~ lassy423$ cd $DEMOSITE_HOME
demosite_home lassy423$ echo demosite-gemset > .rbenv-gemsets
demosite_home lassy423$ rbenv gemset active
demosite-gemset global
demosite_home lassy423$ 

gems のインストール

今回は既存の Rails アプリを動かしたいので、まず bundler だけインストールします。

bash
demosite_home lassy423$ gem install bundler
Fetching: bundler-1.6.1.gem (100%)
Successfully installed bundler-1.6.1
1 gem installed
demosite_home lassy423$ 

続いて、bundleコマンドでその他の gems をインストールします。

bash
demosite_home lassy423$ bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
(中略)
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!
demosite_home lassy423$ 

最後にインストールした gems を有効にします。

bash
demosite_home lassy423$ rbenv rehash

この時点で、Rails アプリケーション単体で動くようになります。
rails sを実行して正常に動かないようであれば、対処してください。

passenger のインストール

apache は既に入ってる前提で話を進めますが、
入っていない場合はbrew info httpdでインストールしてください。
途中のrbenv rehashを忘れないようにしてください。

bash
demosite_home lassy423$ gem install passenger
Fetching: daemon_controller-1.2.0.gem (100%)
Successfully installed daemon_controller-1.2.0
Fetching: passenger-4.0.40.gem (100%)
Building native extensions.  This could take a while...
Successfully installed passenger-4.0.40
2 gems installed
demosite_home lassy423$ rbenv rehash
demosite_home lassy423$ passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v4.0.40.
(中略)
Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /Users/lassy423/.rbenv/versions/2.1.1/gemsets/demosite-gemset/gems/passenger-4.0.40/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /Users/lassy423/.rbenv/versions/2.1.1/gemsets/demosite-gemset/gems/passenger-4.0.40
     PassengerDefaultRuby /Users/lassy423/.rbenv/versions/2.1.1/bin/ruby
   </IfModule>

After you restart Apache, you are ready to deploy any number of web
applications on Apache, with a minimum amount of configuration!

Press ENTER to continue.

途中で apache の設定が表示されるのでコピーしておきます。

apache の設定を修正(失敗パターン)

passenger インストール時に表示された apache の設定を追記します。

/usr/local/etc/apache2/httpd.conf
LoadModule passenger_module /Users/lassy423/.rbenv/versions/2.1.1/gemsets/demosite-gemset/gems/passenger-4.0.40/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /Users/lassy423/.rbenv/versions/2.1.1/gemsets/demosite-gemset/gems/passenger-4.0.40
  PassengerDefaultRuby /Users/lassy423/.rbenv/versions/2.1.1/bin/ruby
</IfModule>

RailsEnv development
<VirtualHost *:80>
    ServerName somedomain.com
    # !!! Be sure to point DocumentRoot to 'public'!
    DocumentRoot /Users/lassy423/demosite/public
    <Directory "/Users/lassy423/demosite/public">
        # This relaxes Apache security settings.
        AllowOverride none
        # MultiViews must be turned off.
        Options -MultiViews
        Allow from all
    </Directory>
</VirtualHost>

sudo apachectl startで apache を起動し、
ブラウザでアクセスすると以下のようなエラー画面が表示されます。
(表示されない場合は構築は終了です)

20140403_passenger_error.png

apache の設定を修正(成功パターン)

Stack Overflow を参考に、以下の設定を追加します。

/usr/local/etc/apache2/httpd.conf
SetEnv GEM_HOME /Users/lassy423/.rbenv/versions/2.1.1/gemsets/demosite-gemset/

最終的な設定は以下のようになりました。

/usr/local/etc/apache2/httpd.conf
LoadModule passenger_module /Users/lassy423/.rbenv/versions/2.1.1/gemsets/demosite-gemset/gems/passenger-4.0.40/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /Users/lassy423/.rbenv/versions/2.1.1/gemsets/demosite-gemset/gems/passenger-4.0.40
  PassengerDefaultRuby /Users/lassy423/.rbenv/versions/2.1.1/bin/ruby
</IfModule>

RailsEnv development
<VirtualHost *:80>
	ServerName somedomain.com
    # !!! Be sure to point DocumentRoot to 'public'!
    DocumentRoot /Users/lassy423/demosite/public
	SetEnv GEM_HOME /Users/lassy423/.rbenv/versions/2.1.1/gemsets/demosite-gemset/
    <Directory "/Users/lassy423/demosite/public">
        # This relaxes Apache security settings.
        AllowOverride none
        # MultiViews must be turned off.
        Options -MultiViews
        Allow from all
    </Directory>
</VirtualHost>

参考

15
13
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
15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?