Ruby
Rails
Apache
rbenv
passenger

RailsとApacheの連携について

More than 3 years have passed since last update.

 VagrantとVirtualBoxとCentOS6を使って、apacheを通してrailsを使えるようにする勉強をしています。

 質問投稿でしたが、無事動作したので、修正した内容に変更いたします。
 エラーの原因は、passengerをsudoでインストールしたため、systemのruby 1.8.7対象にapacheがビルドされてしまったことでした。

 全ての操作。

> vagrant box add centos664 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130731.box
> mkdir centos6
> cd centos6
> vagrant init centos664

Vagrantfileを編集
# using a specific IP.
 config.vm.network :private_network, ip: "192.168.33.30"

> vagrant up
> vagrant ssh

yumのアップデートとvimのインストール

$ sudo yum -y update
$ sudo yum -y install vim

日本語化

$ sudo vim /etc/sysconfig/i18n

LANG="ja_JP.UTF-8"

$ sudo reboot

root権限を持たせたユーザ作成

$ sudo usermod -G wheel vagrant
$ sudo visudo

## Allows people in group wheel to run all commands

%wheel  ALL=(ALL)       ALL
wheelグループを有効化する

apacheのインストール

$ sudo yum -y install httpd
$ sudo service httpd start
$ sudo chkconfig httpd on

必要なツール類のインストール

$ sudo yum -y install gcc gcc-c++ openssl-devel zlib-devel make patch git gettext perl rpm-build
$ sudo yum -y install httpd-devel curl-devel ncurses-devel gdbm-devel readline-devel sqlite-devel ruby-devel

rbenvのインストール

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l

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

ruby 1.9.3のインストール

$ rbenv install -v 1.9.3-p484
$ rbenv rehash

ruby 1.9.3をglobalに設定

$ rbenv global 1.9.3-p484
$ rbenv versions
$ sudo chmod o+x /home/vagrant

passengerのインストール

$ gem install passenger --no-ri --no-rdoc -V
$ rbenv rehash

apacheにpassengerモジュールをインストール

$ passenger-install-apache2-module
The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /home/vagrant/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25/buildout/apache2/mod_passenger.so
   PassengerRoot /home/vagrant/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25
   PassengerDefaultRuby /home/vagrant/.rbenv/versions/1.9.3-p484/bin/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Press ENTER to continue.


--------------------------------------------
Deploying a Ruby on Rails application: an example

Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

   <VirtualHost *:80>
      ServerName www.yourhost.com
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /somewhere/public
      <Directory /somewhere/public>
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews
      </Directory>
   </VirtualHost>

And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:

  /home/minao/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25/doc/Users guide Apache.html
  http://www.modrails.com/documentation/Users%20guide%20Apache.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
https://www.phusionpassenger.com

Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.

とりあえず、rbenv rehash

$ rbenv rehash

passenger.confの作成

$ sudo vim /etc/httpd/conf.d/passenger.conf

以下を書き込む

LoadModule passenger_module /home/vagrant/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25/buildout/apache2/mod_passenger.so
PassengerRoot /home/vagrant/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25
PassengerDefaultRuby /home/vagrant/.rbenv/versions/1.9.3-p484/bin/ruby

Header always unset "X-Powered-By"
Header always unset "X-Rack-Cache"
Header always unset "X-Content-Digest"
Header always unset "X-Runtime"

httpd.confの修正

$ sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
$ sudo vim /etc/httpd/conf/httpd.conf

修正項目

# 44行目 OS情報を表示しないようにする
ServerTokens Prod

#277行目
ServerName 192.168.33.30:80

# 331行目 -(ハイフン)を追加
Options -Indexes FollowSymLinks

# 536行目 OS情報を表示しないようにする
ServerSignature Off

# キャッシュ強化(以下の行を追加)
# Apache Expire Header
ExpiresActive On
ExpiresByType application/javascript "access plus 1 days"
ExpiresByType application/x-javascript "access plus 1 days"
ExpiresByType text/javascript "access plus 1 days"
ExpiresByType text/css "access plus 1 days"
ExpiresByType image/jpeg "access plus 3 days"
ExpiresByType image/png "access plus 3 days"
ExpiresByType image/gif "access plus 3 days"
ExpiresByType image/x-icon "access plus 3 days"

# ETags
FileETag MTime Size

apache設定のチェック

$ apachectl configtest
> Syntax OK

Apacheを起動及び、自動起動を設定。

$ sudo service httpd restart
$ sudo chkconfig httpd on

$ sudo chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

Bundlerのインストール

$ sudo gem install bundler --no-ri --no-rdoc -V
$ rbenv rehash

Railsのインストール

$ gem install rails -v 3.2.15 --no-ri --no-rdoc -V
$ rbenv rehash

Railsでsampleを生成

$ mkdir rails_app
$ cd rails_app
$ rails new sample

Gemfileの修正

$ cd sample
$ vim Gemfile

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  gem 'therubyracer', :platforms => :ruby


$ bundle install
$ rbenv rehash

scaffoldで簡単なMVCサンプル作成

$ rails g scaffold foo name:string
$ rake db:migrate

httpd.confにVirtualHost 設定追加

$ sudo vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
      RailsEnv development
      PassengerEnabled on
      ServerName 192.168.33.30
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /home/vagrant/rails_app/sample/public
      <Directory /home/vagrant/rails_app/sample/public>
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews
      </Directory>
</VirtualHost>

apache設定のチェック

$ apachectl configtest
> Syntax OK
$ sudo service httpd restart

public/index.htmlからfoosを呼び出すようにroutes.rbを修正

$ vim config/routes.rb
  root :to => 'foos#index'

$ mv public/index.html public/index.html.bak

とりあえず、Firewallを止める

$ sudo service iptables stop
$ sudo chkconfig iptables off
$ sudo chkconfig --list iptables
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off

rails_app以下の実行権限を修正

$ cd ~
$ chmod -R o+x rails_app

 これで、http://192.168.33.30 にアクセスすると、無事に動作しました。