Archive for the ‘Ruby’ Category.

Installation of Redmine 2

Installation of Redmineの続きです。

Redmineでもデータベースを使用しますが、今回はMySQLを使用したいと思います。
Redmineを展開したディレクトリの中にconfig/database.yml.exambleというファイルがあるので、それを「database.yml」にリネームして使用することにします。
まずはDatabaseを作成します。
MySQLのクライアントを起動して、「redmine」という名前のデータベースを作成します。

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

ついでに、じゃないですけど、redmineを使用するユーザーを作成します。
“set password”でちゃんとしたパスワードを設定しましょう。(例ではnewpasswordにしています。あくまで例です。)

mysql> grant create,select,insert,delete,update on redmine.* to redmine@localhost identified by 'password';
Query OK, 0 rows affected (0.08 sec)
 
mysql> set password for 'redmine'@'localhost' = password('newpassword');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.04 sec)

作成したユーザーでログインしてみます。

mysql> \q
Bye
[root@blackhole config]# mysql -u redmine -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8282
Server version: 5.0.45 Source distribution
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

一応これでOKなはず。MySQLを使用するやつはCreate Table権限が必要な場合が多いですよね。あんまり業務アプリケーションではALTER権限自体与える場合が少ないですが。

そして先ほどのdatabase.yml(ymlのyってなんだろ?)の「production」のセクションを先ほど設定した内容に書き換えます。

production:
  adapter: mysql
  database: redmine
  host: localhost
  username: redmine
  password: newpassword
  encoding: utf8

別に記載しておきますが、使用しているMySQLの一時ファイル(ソケット用?)が、 標準(/tmp/mysql.sock)と違うところに作成されているようでした。

# mysqladmin -p variables | grep socket
Enter password:
| socket                          | /var/lib/mysql/mysql.sock                               |

「/var/lib/mysql/mysql.sock」に作成されているようなので、この設定をconfig/database.ymlに追記します。

 socket: /var/lib/mysql/mysql.sock

そしてインストールのコマンドを入力します。rakeというらしい、(makeのruby版だからでしょうか?)

<code># rake db:migrate RAILS_ENV="production"
(in /www/labs/redmine-0.8.5)</code>
rake aborted!
Mysql::Error: #42000INDEX command denied to user 'redmine'@'localhost' for table 'schema_migrations': CREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`)

あれ、インデックス作成に失敗している。
createでは付与されないのかー。改めて「INDEX」権限を付与します。
あと、migrateのスクリプトで、alter table文でカラムを追加、drop文でテーブルの削除をしているようなので、alter文、drop文も実行できるようにしました。

mysql> grant index,drop alter on redmine.* to redmine@localhost;
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

あらためてスクリプトを実行します。

# rake db:migrate RAILS_ENV="production"
(in /www/labs/redmine-0.8.5)

実行するとつらつらと実行結果が表示され、エラーがなければOKです。
次に、初期データをロードするスクリプトを流します。

# rake load_default_data RAILS_ENV="production"
(in /www/labs/redmine-0.8.5)
 
Select language: bg, ca, cs, da, de, en, es, fi, fr, he, hu, it, ja, ko, lt, nl, no, pl, pt, pt-br, ro, ru, sk, sr, sv, th, tr, uk, vn, zh, zh-tw [en] ja

ここで言語を選択しろと言われるので、「ja」を指定して実行します。

====================================
Default configuration data loaded.

Note: The rake task load_default_data has been deprecated, please use the replacement version redmine:load_default_data

最後になんだか警告が出ているのですが、これはスクリプト名を「redmine:load_default_data」とすればよさそう。

# rake redmine:load_default_data RAILS_ENV="production"

これでやってみたのですが、ブラウザからアクセスしてみるとInternal Server Errorが出ており、ログを見たところ、/tmp/sessions/以下にファイルが書き込めないエラーのようでした。
これはhttp://redmine.jp/tech_note/apache-passenger/のトラブルシューティング-パーミッションの問題にあるように、config/environment.rbのオーナーがrootになっていたためのようです。
所有者を変更して再起動したらうまくいきました。以下のような画面になります。
redmine-installed


Installation of Redmine

9月は一度もブログ更新出来ませんでした。

さて、急遽バグトラッキングシステムを導入する必要が出てきたのですが、BugzillaMantisTracなどの有名どころを入れるんじゃつまらないので、Ruby on railsで動くRedmineというBTSを入れてみたいと思います。

(調べていたら、第2回:課題管理対決!Redmine vs. Tracという記事がありましたが、使い勝手にあまり差はないようです。Redmineのほうが導入が簡単だ、という話です。)

まずは、Ruby on Railsを動かせるようにしたいと思いますが、いつか手を出してみたいと思ってはいましたけど、結局何なのかはよく理解していませんでした。
要するにフレームワークね。CakePHPやStrutsのような。

まずはRubyをインストールします。環境はいつもの

# cat /etc/redhat-release
CentOS release 5.3 (Final)

YUMでのRubyのバージョンは1.8.5で、今日現在での最新安定バージョンは1.9.1らしい。
http://www.ruby-lang.org/ja/downloads/

# yum info ruby
Available Packages
Name       : ruby
Arch       : i386
Version    : 1.8.5
Release    : 5.el5_3.7
Size       : 274 k
Repo       : updates
Summary    : An interpreter of object-oriented scripting language
URL        : http://www.ruby-lang.org/
License    : Ruby License/GPL - see COPYING
Description: Ruby is the interpreted scripting language for quick and easy object-oriented programming.  It has many features to process text files
           : and to do system management tasks (as in Perl).  It is simple, straight-forward, and extensible.

違いも良くわかっていないので、まずYUMで1.8.5をインストールすることにします。

# yum install ruby
   :
# ruby -v
ruby 1.8.5 (2006-08-25) [i386-linux]

これでRubyが使用できるようになりました。
今回は、第2回 環境構築/redMineのインストール・初期設定を参考にして、データベースにはMySQLを使用します。RailsはRedmineに含まれているそうなのでここでは明示的に触れません。
MySQLはすでに以下のバージョンが入ってます。

# mysql --version
mysql  Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (i686) using readline 5.0

そして、Redmineプロジェクトのダウンロードページからダウンロードします。(ああ、wgetが面倒くさい・・・)
現時点での最新バージョンは0.8.5で、これを使用します。
とりあえず、/www/redmineというディレクトリを作成してそこを基本ディレクトリとします。

# wget http://rubyforge.org/frs/download.php/63583/redmine-0.8.5.tar.gz
--08:41:45--  http://rubyforge.org/frs/download.php/63583/redmine-0.8.5.tar.gz
rubyforge.org をDNSに問いあわせています... 205.234.109.19
rubyforge.org|205.234.109.19|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: http://files.rubyforge.vm.bytemark.co.uk/redmine/redmine-0.8.5.tar.gz [続く]
--08:41:45--  http://files.rubyforge.vm.bytemark.co.uk/redmine/redmine-0.8.5.tar.gz
files.rubyforge.vm.bytemark.co.uk をDNSに問いあわせています... 80.68.94.54
files.rubyforge.vm.bytemark.co.uk|80.68.94.54|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 3174242 (3.0M) [application/x-tar]
Saving to: `redmine-0.8.5.tar.gz'
 
100%[=============================================================================================================&gt;] 3,174,242    870K/s   in 3.6s
 
08:41:50 (870 KB/s) - `redmine-0.8.5.tar.gz' を保存しました [3174242/3174242]

RedmineもWebサーバー上で動かすので、Apacheと連携させる設定が必要なのですが、これを行ってくれるApacheモジュールがあるそうです。それがPhusion Passengerというらしいですが、
lこれを入れるにはRubyGemsというRubyのパッケージ管理ツールが必要らしいです。
RubyGemsについてはこちらにインストール方法をメモしました。

Passengerのインストールはこちらを参考にさせていただきました。

手順の通り、gem install passengerとやってみたのですが、以下のようなエラーが。

# gem install passenger
Building native extensions.  This could take a while...
ERROR:  Error installing passenger:
        ERROR: Failed to build gem native extension.
 
/usr/bin/ruby extconf.rb
can't find header files for ruby.
 
 
Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/fastthread-1.0.7 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/fastthread-1.0.7/ext/fastthread/gem_make.out

Headerファイルが見つからないとのことで、同じようなことをやっている人がいたのでそれを参考に、ruby-develパッケージを入れてやってみました。

# yum install -y ruby-devel
# gem install passenger
Building native extensions.  This could take a while...
Building native extensions.  This could take a while...
Successfully installed fastthread-1.0.7
Successfully installed passenger-2.2.5
2 gems installed
Installing ri documentation for fastthread-1.0.7...
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
Installing ri documentation for passenger-2.2.5...
Installing RDoc documentation for fastthread-1.0.7...
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
No definition for dummy_dump
No definition for dummy_dump
No definition for rb_queue_marshal_load
No definition for rb_queue_marshal_dump
Installing RDoc documentation for passenger-2.2.5...

そしてビルド

# passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v2.2.5.
 
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.

あとはEnterを押してしばらく経つとビルドが終わり、httpd.confの編集内容が出るのでコピーしておきます。(普通にやったら以下のようになると思います)

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-2.2.5/ext/apache2/mod_passenger.so
   PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5
   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 Ruby on Rails application in /somewhere. Add a virtual host
to your Apache configuration file, and set its DocumentRoot to
/somewhere/public, like this:
 
 
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public    # &lt;-- be sure to point to &#39;public&#39;!
 
 
And that's it! You may also want to check the Users Guide for security and
optimization tips and other useful information:
 
  /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5/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 &amp; Ninh Bui.

ただ、上記設定を流用すると、Redmine専用のVirtualHostが必要になってしまうので、Redmineをサブディレクトリで動作させるように設定しました。
(参照:PassengerでRedmineをサブディレクトリに公開

Apacheのバージョンは以下です。

# httpd -v
Server version: Apache/2.2.3
Server built:   Jan 21 2009 22:01:41

2.2.xはhttpd.confに直接書くのではなく、設定ごとにconfファイルを分けるのがお作法?なので、conf.d/redmine.confというファイルを作成して以下のようにしました。

LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.5
PassengerRuby /usr/bin/ruby
RailsBaseURI /redmine

DocumentRootに「redmine」でアクセス出来るシンボリックリンクをredmineの「public」ディレクトリに張って完了です。
例えば、/opt/redmine-0.8.5に展開した場合は以下のようにします。

ln -s /opt/redmine-0.8.5/public /var/www/redmine

これでhttp://server/redmineでアクセス出来ればとりあえず動作OKです。
なんだか長くなったのでMySQLの設定以降は次に。


Installation of RubyGems

Rubyのパッケージ管理ツールとして、RubyGemsというのがあります。

RubygemsはCentOS5ではYUMでインストール出来ないのでソースからインストールします。
Rubyのバージョンは

# ruby -version
ruby 1.8.5 (2006-08-25) [i386-linux]

以下は/tmpにrubygems_srcディレクトリを作成してみました。
現時点でのRubygemsの最新バージョンは1.3.5です。

# mkdir rubygems_src
# cd rubygems_src/
# wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
--09:30:27--  http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
rubyforge.org をDNSに問いあわせています... 205.234.109.19
rubyforge.org|205.234.109.19|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 302 Found
場所: http://files.rubyforge.vm.bytemark.co.uk/rubygems/rubygems-1.3.5.tgz [続く]
--09:30:27--  http://files.rubyforge.vm.bytemark.co.uk/rubygems/rubygems-1.3.5.tgz
files.rubyforge.vm.bytemark.co.uk をDNSに問いあわせています... 80.68.94.54
files.rubyforge.vm.bytemark.co.uk|80.68.94.54|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 278469 (272K) [application/x-tar]
Saving to: &apos;rubygems-1.3.5.tgz&apos;
 
100%[=============================================================================================================&gt;] 278,469      141K/s   in 1.9s
 
09:30:30 (141 KB/s) - &apos;rubygems-1.3.5.tgz&apos; を保存しました [278469/278469]

解凍してインストールします。

# tar zxvf rubygems-1.3.5.tgz
# cd rubygems-1.3.5
# ruby setup.rb

これでインストール出来たようなのですが、エラーメッセージが出てるのが気になるなぁ。

# ruby setup.rb
RubyGems 1.3.5 installed
./lib/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- rdoc/rdoc (LoadError)
        from ./lib/rubygems/custom_require.rb:31:in `require'
        from ./lib/rubygems/commands/setup_command.rb:352:in `run_rdoc'
        from ./lib/rubygems/commands/setup_command.rb:247:in `install_rdoc'
        from ./lib/rubygems/commands/setup_command.rb:120:in `execute'
        from ./lib/rubygems/command.rb:257:in `invoke'
        from ./lib/rubygems/command_manager.rb:132:in `process_args'
        from ./lib/rubygems/command_manager.rb:102:in `run'
        from ./lib/rubygems/gem_runner.rb:58:in `run'
        from setup.rb:35

気になるので、この「rdoc」というのを調べてみた。JavaDocみたいなやつね。
RDoc による自動ドキュメント生成に書かれているが、Ruby1.8.4以降では標準でRDocが入っているらしいのだが・・・。
何気にyum searchしてみると、ruby-rdoc.i386というプロジェクトがある。
これを入れなければだめなのかな?

# yum search ruby
bsf.i386 : Bean Scripting Framework
eruby.i386 : 組み込み Ruby 言語のインタラプター
eruby-devel.i386 : eRuby 用の開発ファイル。
eruby-libs.i386 : eRuby 用のライブラリ
facter.i386 : Ruby module for collecting simple facts about a host operating system
graphviz-ruby.i386 : Ruby extension tools for version 2.22.0 of graphviz
ming.i386 : SWF output library
pdumpfs.i386 : Daily backup system similar to Plan9's dumpfs
perl-Scalar-Properties.noarch : Run-time properties on scalar variables
perl-Set-Array.noarch : Arrays as objects with lots of handy methods
perl-Set-String.noarch : Strings as objects with lots of handy methods
perl-YAML-MLDBM.noarch : Use tied hash databases with Python and Ruby
ruby.i386 : An interpreter of object-oriented scripting language
ruby-devel.i386 : GNOME コントロールセンター開発環境
ruby-docs.i386 : Ruby スクリプト言語用のマニュアルと FAQ
ruby-fam.i386 : Gamin/FAM bindings for Ruby
ruby-irb.i386 : 対話式のRuby
ruby-libs.i386 : Ruby の実行に必要なライブラリ
ruby-mode.i386 : スクリプト言語 Ruby用のEmacs Lisp rubyモード
ruby-rdoc.i386 : A tool to generate documentation from Ruby source files
ruby-ri.i386 : Ruby interactive reference
ruby-rpm.i386 : Ruby bindings for RPM
ruby-rrdtool.i386 : RRDtool module for Ruby
ruby-shadow.i386 : Ruby bindings for shadow password access
ruby-tcltk.i386 : Ruby スクリプト言語用の Tcl/Tk インターフェイス
subversion-ruby.i386 : Ruby bindings to the Subversion libraries
swig.i386 : Simplified Wrapper and Interface Generator

これを入れて再度setup.rbを実行した。

# yum install -y ruby-rdoc
# ruby setup.rb
RubyGems 1.3.5 installed
 
=== 1.3.5 / 2009-07-21
 
Bug fixes:
 
* Fix use of prerelease gems.
* Gem.bin_path no longer escapes path with spaces. Bug #25935 and #26458.
 
Deprecation Notices:
 
* Bulk index update is no longer supported (the code currently remains, but not
  the tests)
* Gem::manage_gems was removed in 1.3.3.
* Time::today was removed in 1.3.3.
 
 
------------------------------------------------------------------------------
 
RubyGems installed the following executables:
        /usr/bin/gem

これでうまくいったようだ。

# gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.5
  - RUBY VERSION: 1.8.5 (2006-08-25) [i386-linux]
  - INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /usr/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
  - GEM PATHS:
     - /usr/lib/ruby/gems/1.8
     - /root/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources =&gt; true
     - :verbose =&gt; true
     - :benchmark =&gt; false
     - :backtrace =&gt; false
     - :bulk_threshold =&gt; 1000
  - REMOTE SOURCES:
     - http://gems.rubyforge.org/

yumを使っている場合は注意が必要ですね。