| LFSトップ | Japanese Only | 生成:2009/12/23 更新:2009/12/25 |
Ruby on Rails 環境構築(1)
はじめに
- 何度も {B}LFS にて Ruby on Rails 環境を作っている。ここでまとめておこう。
- ベースは LFS SVN-20091216. openssl-0.9.8k, openssh-5.1p1 は導入済。
- ちなみに LFS ビルド時に glibc にて localedef -i ja_JP -f UTF-8 ja_JP.UTF-8 を実行済。
構築の方針
- 方針はあるようで、ないようで・・・ まずは以下とする。
- DB は MySQL、WEBサーバーは Apache、細かなところで Mongrel とか、ロードバランスとかどうするかを画策する必要があるが、とりあえず Passenger でサンプルサイトのアクセスまで。FastCGI は未着手。
- 本当はこれを書き始めた際には Mongrel_cluster 作りを書こうとしていたのであるが、書いている最中に方針変更。Passenger に切り替えた。こっちの方が楽かもしれない。
railsユーザー生成
- railsユーザーを作成。グループは Apache とする。だからあらかじめ Apache グループも作っておく。
groupadd -g 25 apache
useradd -m -s /bin/bash -g apache rails
passwd rails
rails のパスワードを変更しています
新しいパスワードを入れてください (最短 5 文字)
大文字・小文字・数字を混ぜて使うようにしてください。
新しいパスワード: XXXXX
不正なパスワードです: 単純すぎます
警告: 弱いパスワードです (どうしてもこれを使いたければもう一度入力)。
新しいパスワード: XXXXX
新規パスワード再入力: XXXXX
passwd: password changed.
Ruby-1.8.7-p174 (2009.06.15 配布) インストール
- BLFSには Ruby-1.8.7-p72 が示されているが、より最新の Ruby-1.8.7-p174 を導入する。バージョンの違いを除けば BLFSと一緒。
- Ruby 1.9系は選ばない。Ruby on Rails では推奨されていないし、そもそもまだそこまで対応していない(?)
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p174.tar.bz2
tar xf ruby-1.8.7-p174.tar.bz2
cd ruby-1.8.7-p174
./configure --prefix=/usr \
--enable-shared \
--enable-pthread \
--enable-install-doc
make
make install
RubyGems-1.3.5 (2009.07.21 配布) インストール
wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
tar xf rubygems-1.3.5.tgz
cd rubygems-1.3.5
ruby setup.rb
Ruby On Rails 2.3.5 インストール
- Rails (Ruby on Rails) の最新をインストール。
- まじめなインストールは gem install rails とするが、結構時間がかかる。
ri や RDoc を省略すれば少しは時間短縮になる。その場合はオプション --no-ri --no-rdoc を指定。試しごとの場合はこれで進める。
gem install rails --no-ri --no-rdoc
Successfully installed rake-0.8.7
Successfully installed activesupport-2.3.5
Successfully installed activerecord-2.3.5
Successfully installed rack-1.0.1
Successfully installed actionpack-2.3.5
Successfully installed actionmailer-2.3.5
Successfully installed activeresource-2.3.5
Successfully installed rails-2.3.5
8 gems installed
- Ruby on Rails のインストールと言えば、これで終わりなのであるが、それではあまりに実践的でない。しっかりと実用的なサイト作りに至るまでを示す。
- 上記はネットワーク越しに gem ファイルを取ってくるものなので、もしネットワークオフラインの場合は、別途 gem ファイルを事前入手しておいた上で、gem install rake-0.8.7.gem などを実行すればよい。ただし依存関係の順番どおりに進めないと正しくインストールできない。
MySQL-5.1.41 インストール
- BLFS は MySQL 5.1.30 を説明。最新の 5.1.41 をインストールする。手順は BLFS の通り。
- エンコーディングは UTF-8 を選定。そのため my.cnf にエンコーディング指定追記。
vi /etc/my.cnf
...
[mysqld]
...
default-character-set=utf8
[mysqldump]
...
default-character-set=utf8
...
[mysql]
...
default-character-set=utf8
...
/etc/rc.d/init.d/mysql start
Starting MySQL daemon... [ OK ]
mysql -u root -p
Enter password: XXXXX
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.41-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.01 sec)
mysql> \q
Bye
MySQLアダプタインストール
- MySQL アダプタをインストール。ハイフンが変な風に見えるが、こうするらしい。理由は解釈せず。
- 1つだけの gem なら処理も早いから --no-ri --no-rdoc はあってもなくてもよい。
- --with-mysql-dir=/usr がミソで、これをやっておかないとエラーになったはず。
gem install mysql --no-ri --no-rdoc -- --with-mysql-dir=/usr
Building native extensions. This could take a while...
Successfully installed mysql-2.8.1
1 gem installed
Apacheインストール
- Apache をインストール。BLFS では httpd-2.2.11 なのであるが、より最新の httpd-2.2.14 を用いる。
- パッチ httpd-2.2.11-config-1.patch が 2.2.11 用。でも 2.2.14 で問題なく当たる。実は中身を見ていない。試しごとなので、あまり気にしない。
- モジュールとして proxy, proxy_balancer, proxy_http を用いたいから、configure オプションとして --enable-proxy を指定する。
とは言うものの、以降では proxy を用いるところまで至らず。まぁ後々必ず必要になるであろうから。
- --enable-proxy 指定時は proxy モジュール類が出来上がるはずなので、インストール後に確認。
ls -ltr /usr/lib/apache/*proxy*
-rwxr-xr-x 1 root root 71589 2009-12-23 11:56 /usr/lib/apache/mod_proxy_ftp.so
-rwxr-xr-x 1 root root 29528 2009-12-23 11:56 /usr/lib/apache/mod_proxy_connect.so
-rwxr-xr-x 1 root root 175788 2009-12-23 11:56 /usr/lib/apache/mod_proxy.so
-rwxr-xr-x 1 root root 41053 2009-12-23 11:56 /usr/lib/apache/mod_proxy_scgi.so
-rwxr-xr-x 1 root root 73227 2009-12-23 11:56 /usr/lib/apache/mod_proxy_http.so
-rwxr-xr-x 1 root root 55831 2009-12-23 11:56 /usr/lib/apache/mod_proxy_balancer.so
-rwxr-xr-x 1 root root 100597 2009-12-23 11:56 /usr/lib/apache/mod_proxy_ajp.so
- --enable-proxy を忘れた場合は後からインストール。Apache 本体のインストールの後に、ソースディレクトリ内にて以下のように。
- ftp とか ajp とかは、とりあえず無視。
cd modules/proxy
apxs -c mod_proxy.c proxy_util.c
apxs -c mod_proxy_balancer.c
apxs -c mod_proxy_http.c
apxs -i -a -n proxy mod_proxy.la
apxs -i -a -n proxy_balancer mod_proxy_balancer.la
apxs -i -a -n proxy_http mod_proxy_http.la
- 上で proxy モジュール関連を後追いインストールした場合は念のため確認。ちゃんと生成、インストールされている。rewrite モジュールは元から出来ている。
ls -ltr /usr/lib/apache | tail -5
-rwxr-xr-x 1 root root 129857 2009-12-22 21:37 mod_rewrite.so
drwxr-xr-x 2 root root 4096 2009-12-22 21:37 build
-rwxr-xr-x 1 root root 175416 2009-12-22 21:39 mod_proxy.so
-rwxr-xr-x 1 root root 55695 2009-12-22 21:39 mod_proxy_balancer.so
-rwxr-xr-x 1 root root 73091 2009-12-22 21:39 mod_proxy_http.so
/etc/rc.d/init.d/apache start
Starting Apache daemon...
httpd: apr_sockaddr_info_get() failed for ServerName
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.
0.1 for ServerName [ FAIL ]
less /var/log/apache/error_log
...
[Wed Dec 23 12:08:11 2009] [alert] (EAI 2)Name or service not known: mod_unique_id: unable
to find IPv4 address of "<ServerName>"
Configuration Failed
- /etc/apache/httpd.conf に ServerName の設定を追記。
vi /etc/apache/httpd.conf
...
ServerName ServerName:80
...
- たぶん Dhcpcd 接続なので、まだうまくいかない様子。/etc/hosts にホスト名を明示。
vi /etc/hosts
...
127.0.0.1 localhost ServerName
...
/etc/rc.d/init.d/apache start
Starting Apache daemon... [ OK ]
- 念のため、ブラウザで http://<サーバー名> にアクセス。「It Works!」ページが表示され正常。
サンプルサイト構築
- rails ユーザーのホームディレクトリ配下に、サンプルサイト (sample) 構築。
[railsユーザー]
mkdir -pv ~/sites
cd ~/sites
rails sample -d mysql
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create config/locales
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
...
- DB設定。rails ユーザーでデータベースを作ることに。
- 以下では development の設定のみ示す。production, test についても同様。
[railsユーザー]
cd ~/sites/sample
vi config/database.yml
development:
adapter: mysql
encoding: utf8
reconnect: false
database: sample_development
pool: 5
username: rails
password: XXXXX
host: localhost
[railsユーザー]
mysql -u root -p
Enter password: XXXXX
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.41-log Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database sample_development;
Query OK, 1 row affected (0.01 sec)
mysql> create database sample_production;
Query OK, 1 row affected (0.01 sec)
mysql> create database sample_test;
Query OK, 1 row affected (0.01 sec)
mysql> grant all on sample_development.* to rails@localhost identified by '<パスワード>';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on sample_production.* to rails@localhost identified by '<パスワード>';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on sample_test.* to rails@localhost identified by '<パスワード>';
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye
Passenger インストール
gem install passenger --no-ri --no-rdoc
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.8
2 gems installed
- passenger-install-apache2-module の実行。すべてを認識して動いてくれた。
passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v2.2.8.
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.
...
--------------------------------------------
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.8/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8
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-2.2.8/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.
Apacheの設定
- Virtual Host の設定。元々の httpd-vhosts.conf を参考にして新たに作成。
mv /etc/apache/extra/httpd-vhosts.conf{,.orig}
cat > /etc/apache/extra/httpd-vhosts.conf << "EOF"
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any block.
#
<VirtualHost *:80>
ServerAdmin root@ServerName
DocumentRoot "/home/rails/sites/sample/public"
ServerName ServerName
ErrorLog "/home/rails/sites/sample/log/error.log"
CustomLog "/home/rails/sites/sample/log/com-access.log" common
<Directory "/home/rails/sites/sample/public/">
AllowOverride all
Order allow,deny
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
EOF
- 上で passenger-install-apache2-module を実行した際に出力された内容に基づいて httpd-passenger.conf ファイルを新規生成。
cat > /etc/apache/extra/httpd-passenger.conf << "EOF"
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8
PassengerRuby /usr/bin/ruby
RailsEnv development
EOF
- 上の2つの extra な conf ファイルを読み込むよう httpd.conf に設定。
vi /etc/apache/httpd.conf
...
Include /etc/apache/extra/httpd-passenger.conf
Include /etc/apache/extra/httpd-vhosts.conf
...
/etc/rc.d/init.d/apache restart
Restarting Apache daemon... [ OK ]
稼動確認
- WEBブラウザで http://ServerName にアクセス。無事 Welcome aboard ページ表示。
- 念のため HTTP 通信確認: Apache/2.2.14 (Unix) DAV/2 Phusion_Passenger/2.2.8 となっている。ん? DAV/2 って WebDAV とかのあれ? 必要なのか不要なのか分かっていない。不要ならば削りたいが、とりあえず後々確認しよう。
|
|
| 松 山 道 夫 Michio Matsuyama |
|