RubyOnRails on CentOS

RubyOnRails installation on CentOS 4.4
based on
http://wiki.rubyonrails.org/rails/pages/RailsOnRHEL
http://wiki.rubyonrails.org/rails/pages/Rails+on+CentOS+4.4+with+Apache+and
+FastCGI+Simply
http://wiki.rubyonrails.com/rails/pages/HowtoDeployMoreThanOneRailsAppOnOne
Machine

Install CentOS
* choose "server" option during install, or just grab the "CentOS-Server" CD.
* firewall: open http/https ports
* selinux: disable
* select minimum install

Make sure your centos is uptodate
# yum update

Install Apache2 and MySQL (and also PHP if you want it)
# yum install httpd-devel httpd apr apr-devel apr-util-devel mysql-server mysql-client mysql-devel
# chkconfig httpd on
# chkconfig mysqld on

Install fastcgi (To download latest version visit http://www.fastcgi.com )
# cd /usr/local/src
# wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
# tar xzvf fcgi-2.4.0.tar.gz
# cd fcgi-2.4.0
# ./configure
# make
# make install
# cd ../

Install mod_fcgid (To download latest version visit http://fastcgi.coremail.cn/ )
# wget http://prdownloads.sourceforge.net/mod-fcgid/mod_fcgid.2.0.tar.gz?download
# tar xzvf mod_fcgid.2.0.tar.gz
# cd mod_fcgid.2.0
# vi Makefile
top_dir = /usr/lib/httpd
INCLUDES=-I /usr/include/httpd -I /usr/include/apr-0
# make
# make install

Add CentOS 4 Testing Repos
# cd /etc/yum.repos.d/
# wget http://dev.centos.org/centos/4/CentOS-Testing.repo

Install all Ruby packages
# yum --enablerepo=c4-testing install ruby ruby-devel ruby-docs ruby-irb ruby-libs ruby-mode ruby-mysql ruby-rdoc ruby-ri ruby-tcltk

Install Ruby Gems (To download latest version visit http://rubyforge.org/frs/?group_id=126 )
# cd /usr/local/src
# wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
# tar xzf rubygems-0.9.4.tgz
# cd rubygems-0.9.4
# ruby setup.rb
# cd

Install Rails, FastCGI support, and Mysql support
# gem install rails --include-dependencies
# gem install fcgi
# gem install mysql -- --with-mysql-config=/usr/bin/mysql_config
Select option 3 ? mysql 2.7 (ruby)
# vi /etc/ld.so.conf
add a line :
/usr/local/lib
# /sbin/ldconfig

Create Test Application
# mkdir /var/www/rails
# cd /var/www/rails/
# rails cookbook

Set permission on Test Application
# chgrp -R apache cookbook/
# chmod -R g+r cookbook/
# chmod -R g+w cookbook/log/
# chmod -R g+w cookbook/tmp/
# find /var/www/rails/cookbook/ -type d -exec chmod g+x {} \;

Configure Apache2
# vi /etc/httpd/conf.d/fcgid.conf
LoadModule fcgid_module /usr/lib/httpd/modules/mod_fcgid.so
<IfModule mod_fcgid.c>
SocketPath /tmp/fcgid_sock/
AddHandler fcgid-script .fcgi
</IfModule>

# vi /etc/httpd/conf.d/hosts.conf
<VirtualHost *:80>
SetEnv RAILS_ENV development
ServerName dnsnameoripaddress
DocumentRoot /var/www/rails/cookbook/public/
ErrorLog /var/www/rails/cookbook/log/apache.log

<Directory /var/www/rails/cookbook/public/>
Options ExecCGI FollowSymLinks
AddHandler fcgid-script .fcgi
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

# vi /var/www/rails/cookbook/public/.htaccess
Change line:
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
to
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

And change:
AddHandler fastcgi-script .fcgi
to
AddHandler fcgid-script .fcgi

Hello, World! Script
# cd /var/www/rails/cookbook/
# ruby script/generate controller mytest
# vi app/controllers/my_test_controller.rb
Change file to read:
class MytestController < ApplicationController
def index
render:text => "Hello World!"
#render_text => "Hello World!"
end
end

(Re-)Start Apache2
# service httpd start (or Service httpd restart)

Access http://dnsnameoripaddress/mytest, "Hello World" should be displayed.

--------------------------

Optional:
This guide will setup Apache for 1 Rails App.
To support multi Rails application on your server do below:
First, change the default Apache Rails app
Replace contents of hosts.conf with these
# vi /etc/httpd/conf.d/hosts.conf
<VirtualHost *>
ServerName localhost
DocumentRoot /var/www/html/
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/cookbook/public
RewriteRule ^/cookbook(/.*)?$ /cookbook/public$1
<Directory /var/www/html/cookbook/public/>
Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
Order allow,deny
</Directory>
</VirtualHost>

Configure Cookbook as one of Apache folder
# ln -s /var/www/rails/cookbook /var/www/html/cookbook
# vi /var/www/html/cookbook/public/.htaccess
Change:
RewriteEngine On
into
RewriteEngine On
RewriteBase /cookbook

Restart Apache
# service httpd restart

Access http://dnsnameoripaddress/cookbook/mytest, "
;Hello World&
quot; should be displayed.

--------------------------

Next if you want to create another Rails Application deployment (myapp)
# cd /var/www/rails/
# rails myapp
# chgrp -R apache myapp/
# chmod -R g+r myapp/
# chmod -R g+w myapp/log/
# chmod -R g+w myapp/tmp/
# find /var/www/rails/myapp/ -type d -exec chmod g+x {} \;
# ln -s /var/www/rails/myapp /var/www/html/myapp
# vi /var/www/html/myapp/public/.htaccess
Change line:
AddHandler fastcgi-script .fcgi
to
AddHandler fcgid-script .fcgi

Change line:
RewriteEngine On
to
RewriteEngine On
RewriteBase /myapp

Change line:
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
to
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]


Add new directory on hosts.conf just before </VirtualHost>
# vi /etc/httpd/conf.d/hosts.conf

RewriteCond %{REQUEST_URI} !^/myapp/public
RewriteRule ^/myapp(/.*)?$ /myapp/public$1
<Directory /var/www/html/myapp/public/>
Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
Order allow,deny
</Directory>

Restart Apache
# service httpd restart