LFSトップ | Japanese Only生成:2009/12/23
更新:2009/12/25

Ruby on Rails 環境構築(1)

はじめに

構築の方針

railsユーザー生成

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 配布) インストール

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 インストール

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

MySQL-5.1.41 インストール

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アダプタインストール

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インストール

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
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
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
vi /etc/apache/httpd.conf
	...
	ServerName ServerName:80
	...
vi /etc/hosts
	...
	127.0.0.1 localhost ServerName
	...
/etc/rc.d/init.d/apache start
Starting Apache daemon...                                                        [  OK  ]

サンプルサイト構築

[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
      ...
[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
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の設定

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
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
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  ]

稼動確認



松 山 道 夫 Michio Matsuyama
michio *underscore* matsuyama *atmark* yahoo *period* co *period* jp