kenter自述Linux下使用Apache,FastCGI构建Ruby On Rails环境

   之前安装过Apache,所以这里主要介绍安装Ruby, RubyGems, Rails,和FastCGI的方法。
安装Ruby
把ruby-1.8.5.tar.gz下载到 /usr/local/src 下,然后解压缩
1 shell> cd /usr/local/src    2 shell> tar -zxvf ruby-1.8.5.tar.gz   3 shell> cd ruby-1.8.5   4 shell> ./configure --prefix=/opt/ruby   5 shell> make   6 shell> make test   7 shell> make install   8 shell> cd ..    view plain | print | copy to clipboard | ? shell> cd /usr/local/src
shell> tar -zxvf ruby-1.8.5.tar.gz
shell> cd ruby-1.8.5
shell> ./configure --prefix=/opt/ruby
shell> make
shell> make test
shell> make install
shell> cd ..

设置Ruby的环境变量,别忘记先logout再login
1 RUBY_HOME=/opt/ruby   2 export RUBY_HOME    view plain | print | copy to clipboard | ? RUBY_HOME=/opt/ruby
export RUBY_HOME

安装RubyGems
把rubygems-0.9.0.tgz下载到 /usr/local/src 下,然后解压缩到
1 shell> cd /usr/local/src    2 shell> tar -zxvf rubygems-0.9.0.tgz   3 shell> cd rubygems-0.8.11   4 shell> ruby setup.rb   5 shell> cd ..    view plain | print | copy to clipboard | ? shell> cd /usr/local/src
shell> tar -zxvf rubygems-0.9.0.tgz
shell> cd rubygems-0.8.11
shell> ruby setup.rb
shell> cd ..

这个过程中可能缺包,如果是RH就自己找RPM包吧
安装Rails
这块是远程安装,如果速度慢建议直接去下载这些包
1 shell> gem install rails   2 ....   3 Install required dependency rake? [Yn]  y   4 Install required dependency activesupport? [Yn]  y   5 Install required dependency activerecord? [Yn]  y   6 Install required dependency actionpack? [Yn]  y   7 Install required dependency actionmailer? [Yn]  y   8 Install required dependency actionwebservice? [Yn]  y   9 ....    view plain | print | copy to clipboard | ? shell> gem install rails
....
Install required dependency rake? [Yn]  y
Install required dependency activesupport? [Yn]  y
Install required dependency activerecord? [Yn]  y
Install required dependency actionpack? [Yn]  y
Install required dependency actionmailer? [Yn]  y
Install required dependency actionwebservice? [Yn]  y
....

安装Apache的FastCGI模块
FastCGI这个老家伙在Rails火热起来后也开始返老还童了,虽然有SCGI + lighttpd这个竞争对手,但Apache + FastCGI才是王道,当然FastCGI的Apache模块有一些问题,比如会烂开进程,挂掉后也杀不掉,导致很不稳定,好在有FCGID这个解决办法,不至于让FCGI走向末路^^
把fcgi-2.4.0.tar.gz下载到 /usr/local/src 下,然后解压缩
1 shell> cd /usr/local/src    2 shell> tar -zxvf fcgi-2.4.0.tar.gz    3 shell> cd fcgi-2.4.0   4 shell> ./configure --prefix=/opt/fastcgi   5 shell> make   6 shell> make install    view plain | print | copy to clipboard | ? shell> cd /usr/local/src
shell> tar -zxvf fcgi-2.4.0.tar.gz
shell> cd fcgi-2.4.0
shell> ./configure --prefix=/opt/fastcgi
shell> make
shell> make install

把fcgi安装到Rails中
1 shell> gem install fcgi  -- --with-fcgi-include=/opt/fastcgi/include --with-fcgi-lib=/opt/fastcgi/lib   2   3    view plain | print | copy to clipboard | ? shell> gem install fcgi  -- --with-fcgi-include=/opt/fastcgi/include --with-fcgi-lib=/opt/fastcgi/lib



把mod_fastcgi-2.4.2.tar.gz下载到 /usr/local/src 下,然后解压缩
1 shell> cd /usr/local/src    2 shell> tar -zxvf mod_fastcgi-2.4.2.tar.gz   3 shell> cd mod_fastcgi-2.4.2  4 #如果是Apache2   5 shell> cp Makefile.AP2 Makefile  6 #这里不需要configure 但需要修改Makefile中的Apache目录   7 shell> make   8 shell> make install    view plain | print | copy to clipboard | ? shell> cd /usr/local/src
shell> tar -zxvf mod_fastcgi-2.4.2.tar.gz
shell> cd mod_fastcgi-2.4.2
#如果是Apache2
shell> cp Makefile.AP2 Makefile
#这里不需要configure 但需要修改Makefile中的Apache目录
shell> make
shell> make install

修改Apache的配置文件httpd.conf,追加如下内容
1 #Apache主目录   2 <Directory /var/www/>   3     AllowOverride all   4 </Directory>  5 #加载的模块   6 LoadModule fastcgi_module modules/mod_fastcgi.so   7   8 AddHandler fastcgi-script .fcgi  9 #虚拟主机   10 <VirtualHost *:80>   11     DocumentRoot /var/www/weblog.zhangzhang.net/public  12     ServerName weblog.zhangzhang.net   13     Options Indexes ExecCGI FollowSymLinks   14     RewriteEngine On   15 </VirtualHost>    view plain | print | copy to clipboard | ? #Apache主目录
<Directory /var/www/>
    AllowOverride all
</Directory>
#加载的模块
LoadModule fastcgi_module modules/mod_fastcgi.so

AddHandler fastcgi-script .fcgi
#虚拟主机
<VirtualHost *:80>
    DocumentRoot /var/www/weblog.zhangzhang.net/public
    ServerName weblog.zhangzhang.net
    Options Indexes ExecCGI FollowSymLinks
    RewriteEngine On
</VirtualHost>

前面说过很多mod_fastcgi模块的缺陷,所以这里我们安装一个新的选择FCGID.具体细节请访问http://fastcgi.coremail.cn
把mod_fcgid.1.10.tar.gz下载到 /usr/local/src 下,然后解压缩
1 shell> cd /usr/local/src    2 shell> tar -zxvf mod_fcgid.1.10.tar.gz   3 shell> cd mod_fcgid.1.10  4 #这里不需要configure 但需要修改Makefile中的Apache目录   5 shell> make   6 shell> make install    view plain | print | copy to clipboard | ? shell> cd /usr/local/src
shell> tar -zxvf mod_fcgid.1.10.tar.gz
shell> cd mod_fcgid.1.10
#这里不需要configure 但需要修改Makefile中的Apache目录
shell> make
shell> make install

如果选择使用FCGID,那么也要修改Apache的配置文件httpd.conf,将刚才的FastCGI的配置改为
1 LoadModule fcgid_module modules/mod_fcgid.so  2 # in case of mod_fcgid you may want to add   3 <IfModule mod_fcgid.c>   4     AddHandler fcgid-script .fcgi .fpl   5     IPCCommTimeout 40   6     IPCConnectTimeout 10   7     DefaultInitEnv RAILS_ENV production   8     SocketPath /tmp/fcgidsock   9 </IfModule>   10   11 <VirtualHost *:80>   12   ServerName weblog.zhangzhang.net   13   DocumentRoot /var/www/weblog.zhangzhang.net/public  14   <Directory "/var/www/weblog.zhangzhang.net/public">   15     Options ExecCGI FollowSymLinks   16     AllowOverride all   17     Allow from all   18     Order allow,deny   19   </Directory>   20   RewriteEngine on   21 </VirtualHost>    Options ExecCGI FollowSymLinks AllowOverride all Allow from all Order allow,deny RewriteEngine on " originalcode="LoadModule fcgid_module modules/mod_fcgid.so # in case of mod_fcgid you may want to add AddHandler fcgid-script .fcgi .fpl IPCCommTimeout 40 IPCConnectTimeout 10 DefaultInitEnv RAILS_ENV production SocketPath /tmp/fcgidsock ServerName weblog.zhangzhang.net DocumentRoot /var/www/weblog.zhangzhang.net/public Options ExecCGI FollowSymLinks AllowOverride all Allow from all Order allow,deny RewriteEngine on ">view plain | print | copy to clipboard | ? LoadModule fcgid_module modules/mod_fcgid.so
# in case of mod_fcgid you may want to add
<IfModule mod_fcgid.c>
    AddHandler fcgid-script .fcgi .fpl
    IPCCommTimeout 40
    IPCConnectTimeout 10
    DefaultInitEnv RAILS_ENV production
    SocketPath /tmp/fcgidsock
</IfModule>

<VirtualHost *:80>
  ServerName weblog.zhangzhang.net
  DocumentRoot /var/www/weblog.zhangzhang.net/public
  <Directory "/var/www/weblog.zhangzhang.net/public">
    Options ExecCGI FollowSymLinks
    AllowOverride all
    Allow from all
    Order allow,deny
  </Directory>
  RewriteEngine on
</VirtualHost>

让FastCGI开始工作
先在Apache中建立一个Rails应用,相当的简单^^
1 shell> cd /var/www   2 shell> rails test.zhangzhang.net  3 #这样就可以生成一个标准的Rails应用了   4 shell> cd test.zhangzhang.net   5 shell> script/generate controller blog article  6 #新建一个控制器   7 #通过 http://test.zhangzhang.net/blog/article 访问    view plain | print | copy to clipboard | ? shell> cd /var/www
shell> rails test.zhangzhang.net
#这样就可以生成一个标准的Rails应用了
shell> cd test.zhangzhang.net
shell> script/generate controller blog article
#新建一个控制器
#通过 http://test.zhangzhang.net/blog/article 访问

其实现在可以启动script/server脚本用http://IP:3000来访问Rails应用程序
修改应用public目录中的.htaccess文件
1 dispatch.*cgi*  2 #修改为   3 dispatch.*fcgi*    view plain | print | copy to clipboard | ? dispatch.*cgi*
#修改为
dispatch.*fcgi*

修改应用public目录中的dispatch.fcgi文件
1 require 'fcgi' 2 #修改为   3 require 'rubygems'  4 require_gem 'fcgi'   view plain | print | copy to clipboard | ? require 'fcgi'
#修改为
require 'rubygems'
require_gem 'fcgi'

修改/opt/ruby/lib/gems/1.8/gems/rails-1.1.6/lib/fcgi_handler.rb
1 require 'cgi'  2 require 'rubygems'  3 require_gem 'fcgi'  4 require 'logger'  5 require 'dispatcher'  6 require 'rbconfig'  7   8 class RailsFCGIHandler   9 ...    view plain | print | copy to clipboard | ? require 'cgi'
require 'rubygems'
require_gem 'fcgi'
require 'logger'
require 'dispatcher'
require 'rbconfig'

class RailsFCGIHandler
...

启动Apache
现在就可以用test.zhangzhang.net访问Rails应用了,当然是使用FastCGI来做解释器^^
1 shell> service httpd restart    view plain | print | copy to clipboard | ? shell> service httpd restart

FastCGI Hello World!
既然安装了FCGI,当然要秀一下CGI啦,我用Perl写了一个Hello World,如果机器里没有PL的FCGI包,可能就需要安装一下。
1 #!/usr/bin/perl   2 use FCGI;   3 my $request = FCGI::Request();   4 while($request->Accept() >= 0)   5 {   6   print "Content-type: text/html\n\n";   7   print "<H1><b>Hello World!</b></H1>";   8 }   9 exit 0;    Accept() >= 0) { print "Content-type: text/html\n\n"; print "Hello World!"; } exit 0; ">view plain | print | copy to clipboard | ? #!/usr/bin/perl
use FCGI;
my $request = FCGI::Request();
while($request->Accept() >= 0)
{
  print "Content-type: text/html\n\n";
  print "<H1><b>Hello World!</b></H1>";
}
exit 0;

当然不要忘记把这个文件的权限设置为755

你可能感兴趣的:(apache,linux,Ruby,Rails,rubygems)