railsをapacheで動作させる

Webサーバ(WEBrick)を起動

script/serverでなく、script/railsに変更されたらしい…。

$ ruby script/rails -h
Usage: rails COMMAND [ARGS]

The most common rails commands are:
 generate    Generate new code (short-cut alias: "g")
 console     Start the Rails console (short-cut alias: "c")
 server      Start the Rails server (short-cut alias: "s")
 dbconsole   Start a console for the database specified in config/database.yml
             (short-cut alias: "db")
 new         Create a new Rails application. "rails new my_app" creates a
             new application called MyApp in "./my_app"

In addition to those, there are:
 application  Generate the Rails application code
 destroy      Undo code generated with "generate" (short-cut alias: "d")
 benchmarker  See how fast a piece of code runs
 profiler     Get profile information from a piece of code
 plugin       Install a plugin
 runner       Run a piece of code in the application environment (short-cut alias: "r")

All commands can be run with -h (or --help) for more information.

それではWebサーバ起動。

$ ruby script/rails server
/usr/lib/ruby/gems/1.8/gems/execjs-1.3.0/lib/execjs/runtimes.rb:50:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
        from /usr/lib/ruby/gems/1.8/gems/execjs-1.3.0/lib/execjs.rb:5
        from /usr/lib/ruby/gems/1.8/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'
        from /usr/lib/ruby/gems/1.8/gems/coffee-script-2.2.0/lib/coffee_script.rb:1
        from /usr/lib/ruby/gems/1.8/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require'
        from /usr/lib/ruby/gems/1.8/gems/coffee-script-2.2.0/lib/coffee-script.rb:1
        from /usr/lib/ruby/gems/1.8/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1:in `require'
        from /usr/lib/ruby/gems/1.8/gems/coffee-rails-3.2.2/lib/coffee-rails.rb:1
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `require'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require'
        from /usr/lib/ruby/gems/1.8/gems/bundler-1.0.21/lib/bundler.rb:122:in `require'
        from /home/sai/src/rails/myapp/config/application.rb:7
        from /usr/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:53:in `require'
        from /usr/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:53
        from /usr/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:50:in `tap'
        from /usr/lib/ruby/gems/1.8/gems/railties-3.2.1/lib/rails/commands.rb:50
        from script/rails:6:in `require'
        from script/rails:6

execjsというjavascriptのパッケージが足りないようだ。

http://moyolab.blog57.fc2.com/blog-entry-115.html
ここを見ると、作成したrubyアプリケーションのGemfileにexecjsと記述して、
bundle installすればよいとのこと。

$ vi Gemfile

gem 'execjs'
gem 'therubyracer'
を追加して保存

$ bundle install
Fetching source index for https://rubygems.org/
Using rake (0.9.2.2)
Using i18n (0.6.0)
Using multi_json (1.0.4)
Using activesupport (3.2.1)
Using builder (3.0.0)
Using activemodel (3.2.1)
Using erubis (2.7.0)
Using journey (1.0.1)
Using rack (1.4.1)
Using rack-cache (1.1)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.2)
Using actionpack (3.2.1)
Using mime-types (1.17.2)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.4.1)
Using actionmailer (3.2.1)
Using arel (3.0.0)
Using tzinfo (0.3.31)
Using activerecord (3.2.1)
Using activeresource (3.2.1)
Using bundler (1.0.21)
Using coffee-script-source (1.2.0)
Using execjs (1.3.0)
Using coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.6.5)
Using rdoc (3.12)
Using thor (0.14.6)
Using railties (3.2.1)
Using coffee-rails (3.2.2)
Using jquery-rails (2.0.0)
Using libv8 (3.3.10.4)
Using rails (3.2.1)
Using sass (3.1.13)
Using sass-rails (3.2.4)
Using sqlite3 (1.3.5)
Installing therubyracer (0.9.9) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb
creating Makefile

make
g++ -I. -I/usr/lib/ruby/gems/1.8/gems/libv8-3.3.10.4-x86-linux/lib/libv8/v8/include -I/usr/lib/ruby/1.8/i686-linux -I/usr/lib/ruby/1.8/i686-linux -I. -D_FILE_OFFSET_BITS=64  -Wall -g -rdynamic -fPIC -g -O2    -c v8_context.cpp
make: g++: コマンドが見つかりませんでした
make: *** [v8_context.o] エラー 127


Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/therubyracer-0.9.9 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/therubyracer-0.9.9/ext/v8/gem_make.out
An error occured while installing therubyracer (0.9.9), and Bundler cannot continue.
Make sure that `gem install therubyracer -v '0.9.9'` succeeds before bundling.

make: g++: コマンドが見つかりませんでした

と言われた。

gcc-c++をインストール。

$ sudo yum install gcc-c++

再度、bundle install

$ bundle install
Fetching source index for https://rubygems.org/
Using rake (0.9.2.2)
Using i18n (0.6.0)
Using multi_json (1.0.4)
Using activesupport (3.2.1)
Using builder (3.0.0)
Using activemodel (3.2.1)
Using erubis (2.7.0)
Using journey (1.0.1)
Using rack (1.4.1)
Using rack-cache (1.1)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.1.2)
Using actionpack (3.2.1)
Using mime-types (1.17.2)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.4.1)
Using actionmailer (3.2.1)
Using arel (3.0.0)
Using tzinfo (0.3.31)
Using activerecord (3.2.1)
Using activeresource (3.2.1)
Using bundler (1.0.21)
Using coffee-script-source (1.2.0)
Using execjs (1.3.0)
Using coffee-script (2.2.0)
Using rack-ssl (1.3.2)
Using json (1.6.5)
Using rdoc (3.12)
Using thor (0.14.6)
Using railties (3.2.1)
Using coffee-rails (3.2.2)
Using jquery-rails (2.0.0)
Using libv8 (3.3.10.4)
Using rails (3.2.1)
Using sass (3.1.13)
Using sass-rails (3.2.4)
Using sqlite3 (1.3.5)
Installing therubyracer (0.9.9) with native extensions
Using uglifier (1.2.3)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

できた!

もう一度Webサーバ起動。

$ ruby script/rails server
=> Booting WEBrick
=> Rails 3.2.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-02-05 03:49:45] INFO  WEBrick 1.3.1
[2012-02-05 03:49:45] INFO  ruby 1.8.7 (2008-05-31) [i686-linux]
[2012-02-05 03:49:45] INFO  WEBrick::HTTPServer#start: pid=29722 port=3000

WEBrick起動成功!

ブラウザでIPアドレス:3000にアクセスして、このようなページが表示されればオッケー。

WEBrickを起動したコンソールで、Ctrl+Cで停止。

Webサーバをpassenger(mod_rails)に変更

passengerをインストール。

$ sudo gem install passenger
Fetching: fastthread-1.0.7.gem (100%)
Building native extensions.  This could take a while...
Fetching: daemon_controller-1.0.0.gem (100%)
Fetching: passenger-3.0.11.gem (100%)
Successfully installed fastthread-1.0.7
Successfully installed daemon_controller-1.0.0
Successfully installed passenger-3.0.11
3 gems installed
Installing ri documentation for fastthread-1.0.7...
Installing ri documentation for daemon_controller-1.0.0...
Installing ri documentation for passenger-3.0.11...
Installing RDoc documentation for fastthread-1.0.7...
Installing RDoc documentation for daemon_controller-1.0.0...
Installing RDoc documentation for passenger-3.0.11...

passenger-install-apache2-moduleを実行。

$ sudo passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v3.0.11.

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

Here's what you can expect from the installation process:

 1. The Apache 2 module will be installed for you.
 2. You'll learn how to configure Apache.
 3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.


                                                                                      • -
Checking for required software... * GNU C++ compiler... found at /usr/bin/g++ * Curl development headers with SSL support... not found * OpenSSL development headers... found * Zlib development headers... found * Ruby development headers... found * OpenSSL support for Ruby... found * RubyGems... found * Rake... found at /usr/bin/rake * rack... found * Apache 2... found at /usr/sbin/httpd * Apache 2 development headers... found at /usr/sbin/apxs * Apache Portable Runtime (APR) development headers... found at /usr/bin/apr-1-config * Apache Portable Runtime Utility (APU) development headers... found at /usr/bin/apu-1-config Some required software is not installed. But don't worry, this installer will tell you how to install them. Press Enter to continue, or Ctrl-C to abort.
                                                                                      • -
Installation instructions for required software * To install Curl development headers with SSL support: Please run yum install curl-devel as root. If the aforementioned instructions didn't solve your problem, then please take a look at the Users Guide: /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11/doc/Users guide Apache.html

curl-develがないと怒られたので、yumでインストール。

$ sudo yum install curl-devel

再度、passenger-install-apache2-moduleを実行。

$ sudo passenger-install-apache2-module

…

                                                                                      • -
The Apache 2 module was successfully installed. Please edit your Apache configuration file, and add these lines: LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11/ext/apache2/mod_passenger.so PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11 PassengerRuby /usr/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: ServerName www.yourhost.com DocumentRoot /somewhere/public # <-- be sure to point to 'public'! AllowOverride all # <-- relax Apache security settings Options -MultiViews # <-- MultiViews must be turned off And that's it! You may also want to check the Users Guide for security and optimization tips, troubleshooting and other useful information: /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11/doc/Users guide Apache.html Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-) http://www.modrails.com/ Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.

passenger.confを作成。

先ほどのpassenger-install-apache2-moduleを実行して、終わりのほうに表示された
設定の指示どおりに記述して保存。

# cd /etc/httpd/conf.d/
# touch passenger.conf
# vi passenger.conf
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-3.0.11
PassengerRuby /usr/bin/ruby

httpd.confに設定

# vi /etc/httpd/conf/httpd.conf

コメントアウトはずす
NameVirtualHost *:80

このような設定を追加

     ServerName rails.yourhost.com
     DocumentRoot /home/username/src/rails/myapp/public
     
          AllowOverride all
          Options -MultiViews
     

これで、ブラウザからIPアドレス:80にアクセスして、WEBrickのページが確認できたらオッケー。

railsをPassengerでなくCGIで動かす。

というのも、WEBrickのトップページの、リンク、About your application’s environment

をクリックすると、Internal Server Errorとなっている。

apacheのログを確認。

# tail -f /var/log/httpd/error_log
[ pid=17987 thr=3086321424 file=ext/apache2/Hooks.cpp:817 time=2012-02-05 14:05:29.537 ]: The backend application (process 18014) did not send a valid HTTP response; instead, it sent nothing at all. It is possible that it has crashed; please check whether there are crashing bugs in this application.
[Sun Feb 05 14:05:30 2012] [error] [client 192.168.129.43] Premature end of script headers: rails, referer: http://192.168.129.38/

どうやらPassengerとapacheの連携ができてないようだ…。

こんなサイトを発見した。
http://d.hatena.ne.jp/takuya_1st/20120108/132604301

suExecが有効だとこんなログが吐かれているので、

# tail -f /var/log/httpd/error_log
[Sun Feb 05 16:45:40 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Sun Feb 05 16:45:40 2012] [notice] Digest: generating secret for digest authentication ...
[Sun Feb 05 16:45:40 2012] [notice] Digest: done
[Sun Feb 05 16:45:40 2012] [notice] Apache/2.2.3 (CentOS) configured -- resuming normal operations
[Sun Feb 05 16:45:42 2012] [error] [client 192.168.129.43] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
# cd /usr/sbin/
# mv suexec suexec.disabled