By ZUIFU Blogger 2012年1月11日 2 Comments

Ruby_on_Rails_logo1

Ruby on rails単体でも、DB、Webも含まれているが、以下の構成のシステム構築を行った際のメモ。

■ OS : CentOS5.5
■ DB : MySQL5.0
■ AP : ruby 1.9.3p0, Rails 3.1.3
■ Web : nginx1.0.11-1.el5.ngx

■手順概要
以下の手順で環境を確認しました。
1. mysqlserver インストール(yum)
2. ruby インストール
3. gem インストール
4. rails インストール
5. サンプルアプリを作成して、rails動作確認。
6. nginx + passenger インストール

セットアップ時のエラー対応も合せて記述すると長くなるため、上記を3回に分けて掲載する。
 今回は、5.を掲載。
 1.~4.は、「Ruby on railsを使ったシステム環境構築(CentOS5.5,MySQL5.0,nginx)【1】」に掲載。

■手順詳細
5. サンプルアプリを作成して、rails動作確認。

# rails new testapp -d mysql

  ・
  ・
  ・
Installing mysql2 (0.3.11) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /usr/local/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for mysql.h... no
checking for mysql/mysql.h... no
-----
mysql.h is missing.  please check your installation of mysql and try again.
-----
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/local/bin/ruby
        --with-mysql-config
        --without-mysql-config


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

残念。。。

> checking for mysql.h... no
> checking for mysql/mysql.h... no

とのことで、mysql-develをインストール。

# yum -y install mysql-devel

インストール後、先ほど失敗したアプリを削除して、再度作成

# rm -Rf testapp
# rails new testapp -d mysql
   ・
   ・
   ・
   ・
Installing mysql2 (0.3.11) with native extensions
Using rails (3.1.3)
Installing sass (3.1.12)
Installing sass-rails (3.1.5)
Installing turn (0.8.3)
Installing uglifier (1.2.1)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

で作成OK。

作成された、testapp/config/database.yml を確認。

# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: testapp_development
  pool: 5
  username: root
  password:
  socket: /var/lib/mysql/mysql.sock

‘testapp_development’のデータベースをMySQLに作成。

# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database testapp_development;
Query OK, 1 row affected (0.04 sec)

mysql> \q

続いて、scaffold機能を使って、適当にテーブルと画面を生成。
今回は、下記のテーブルを用意。
テーブル名:client
 name:string
 email:string

# rails g scaffold client name:string email:string
/usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.2.13/lib/execjs/runtimes.rb:47:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
        from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.2.13/lib/execjs.rb:5:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/execjs-1.2.13/lib/execjs.rb:4:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee_script.rb:1:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-script-2.2.0/lib/coffee-script.rb:1:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-rails-3.1.1/lib/coffee-rails.rb:1:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/coffee-rails-3.1.1/lib/coffee-rails.rb:1:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `block in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.21/lib/bundler.rb:122:in `require'
        from /opt/testapp/config/application.rb:7:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands.rb:21:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands.rb:21:in `'
        from script/rails:6:in `require'
        from script/rails:6:in `
'

またまた、残念。。。。

# vi Gemfile

> …
>gem ‘execjs’
>gem ‘therubyracer’

# bundle install
   ・
   ・
   ・
   ・
Installing therubyracer (0.9.9) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /usr/local/bin/ruby extconf.rb
creating Makefile

make
compiling v8_function.cpp
make: g++: Command not found
make: *** [v8_function.o] Error 127


Gem files will remain installed in /usr/local/lib/ruby/gems/1.9.1/gems/therubyracer-0.9.9 for inspection.
Results logged to /usr/local/lib/ruby/gems/1.9.1/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++: Command not found”から下記を実行。

# yum -y install gcc-c++.i386

再度、”bundle install”

# bundle install
Fetching source index for http://rubygems.org/
Using rake (0.9.2.2)
Using multi_json (1.0.4)
Using activesupport (3.1.3)
Using builder (3.0.0)
    ・
    ・
    ・
Installing therubyracer (0.9.9) with native extensions
Using turn (0.8.3)
Using uglifier (1.2.1)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

で再度、scaffoldを試す。

# rails g scaffold client name:string email:string
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require': cannot load such file -- openssl (LoadError)
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `block in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `block in load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/rack-1.3.6/lib/rack/session/cookie.rb:1:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `block in require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `block in load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency'
        from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/session/cookie_store.rb:4:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application/configuration.rb:131:in `const_get'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application/configuration.rb:131:in `session_store'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:172:in `block in default_middleware_stack'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:146:in `tap'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:146:in `default_middleware_stack'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/engine.rb:446:in `app'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application/finisher.rb:37:in `block in '
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `instance_exec'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `run'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:55:in `block in run_initializers'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `each'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `run_initializers'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:96:in `initialize!'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
        from /opt/testapp/config/environment.rb:5:in `'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:83:in `require'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/application.rb:83:in `require_environment!'
        from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.3/lib/rails/commands.rb:22:in `'
        from script/rails:6:in `require'
        from script/rails:6:in `
'

はぁ・・・・--;下記を実行。

# cd /usr/local/src/ruby-1.9.3-p0/ext/openssl
# ruby ruby extconf.rb
# make
# make install

scaffold再度実行。

# rails g scaffold client name:string email:string
      invoke  active_record
      create    db/migrate/20120110095033_create_clients.rb
      create    app/models/client.rb
      invoke    test_unit
      create      test/unit/client_test.rb
      create      test/fixtures/clients.yml
       route  resources :clients
      invoke  scaffold_controller
      create    app/controllers/clients_controller.rb
      invoke    erb
      create      app/views/clients
      create      app/views/clients/index.html.erb
      create      app/views/clients/edit.html.erb
      create      app/views/clients/show.html.erb
      create      app/views/clients/new.html.erb
      create      app/views/clients/_form.html.erb
      invoke    test_unit
      create      test/functional/clients_controller_test.rb
      invoke    helper
      create      app/helpers/clients_helper.rb
      invoke      test_unit
      create        test/unit/helpers/clients_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/clients.js.coffee
      invoke    scss
      create      app/assets/stylesheets/clients.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

で、やっとOK。続いて、テーブル作成。

# rake db:migrate
==  CreateClients: migrating ==================================================
-- create_table(:clients)
   -> 0.0098s
==  CreateClients: migrated (0.0120s) =========================================

アプリの起動。

# rails s -e development
=> Booting WEBrick
=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-01-10 04:55:50] INFO  WEBrick 1.3.1
[2012-01-10 04:55:50] INFO  ruby 1.9.3 (2011-10-30) [i686-linux]
[2012-01-10 04:55:50] INFO  WEBrick::HTTPServer#start: pid=4146 port=3000

ブラウザから、http://[IPアドレス]:3000/clientsにアクセスして、データを入力してDBも確認し、
MySQLにデータが登録されている事を確認し、railsの動作確認OK。

※Could not find a JavaScriptのエラー対応で、下記のブログを参考にさせて頂き、
 SELinuxをdisabledにして上記手順を対応しました。

「”Could not find a JavaScript runtime” と Rails 3.1 起動に失敗する件」
http://moyolab.blog57.fc2.com/blog-entry-115.html

※ブラウザからアクセスして確認する際に、iptablesの設定も必要。とりあえず無効にして確認
 しました。

次回は、nginxの設定を掲載。

以上




Leave a Reply

(required)

(required)