最近看上了Ruby on Rails的敏捷开发技术了,虽然早已不是什么新鲜玩意了,
不过看着它越来越流行了,还是想看一看里面的究竟啊, 先从搭建Ruby on Rails的环境入手吧。
1.安装Ruby
rails框架也是Ruby做的,自然要先安装Ruby了,
yum install ruby -y
确认一下
Ruby -v
2.安装RubyGems
RubyGems是Ruby函数库的管理系统
在此之前先要导入dlutter软件仓库
[root@host1 ~]# vi /etc/yum.repos.d/dlutter.repo
修改成
[dlutter]
name=Unsupported RHEL5 packages (lutter)
baseurl=http://people.redhat.com/dlutter/yum/rhel/5/$basearch/
enabled=0
gpgcheck=0
确认
[root@host1 ~]# yum --enablerepo=dlutter list
然后直接用命令安装RubyGems
[root@host1 ~]# yum --enablerepo=dlutter -y install rubygems.noarch
确认版本
[root@host1 ~]# gem --version
1.2.0
安装好应该是1.2.0的版本,最新是1.3.0的,有兴趣的朋友可以用源码编译升级
3.rails框架的导入
这个比较简单,直接用RubyGems来安装
[root@host1 ~]# gem install rails
4.最关键也是比较复杂的一步是Apache+lighttpd两个Web服务器的导入
关于Web服务器的选择,通常有很多争议,就Ruby on Rails而言,很多都是采用lighttpd
当作Web Server,当作FastCGI应用程序来跑,但是lighttpd还是很难代替Apache的地位,
特别是Rails程序以外还有其他Web程序的时候、不大可能完全移植到lighttpd上的,所以在这里,只是Rails程序跑在lighttpd上,对Rails程序的请求通过Apache转送过来。
先安装FastCGI
[root@host1 ~]# wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
[root@host1 ~]# tar zxvf fcgi-2.4.0.tar.gz
:
[root@host1 ~]# cd fcgi-2.4.0
[root@host1 fcgi-2.4.0]# ./configure
:插入代码
[root@host1 ~]# gem install fcgi
[root@host1 fcgi-2.4.0]# make
:
[root@host1 fcgi-2.4.0]# make install
:
[root@host1 fcgi-2.4.0]# cd
[root@host1 ~]# rm -rf fcgi-2.4.0*
下面用RubyGems把Ruby的FastCGI的包导进来
接下去就要安装lighttpd服务器了,先导入RPMforge 软件仓库
[root@host1 ~]# wget http://dag.wieers.com/packages/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
[root@host1 ~]# rpm -Uhv rpmforge-release-0.3.6-1.el5.rf.i386.rpm
[root@host1 ~]# vi /etc/yum.repos.d/rpmforge.repo
enabled = 1
↓
enabled = 0
[root@host1 ~]# wget http://dag.wieers.com/packages/RPM-GPG-KEY.dag.txt
[root@host1 ~]# rpm --import RPM-GPG-KEY.dag.txt
确认
[root@host1 ~]# yum --enablerepo=rpmforge list
开始安装
[root@host1 ~]# yum --enable=rpmforge install lighttpd
:
[root@host1 ~]# yum --enable=rpmforge install lighttpd-fastcgi
:
一般系统已经有Apache服务了,为了避免冲突,还要配置一下lighttpd,
[root@host1 ~]# vi /etc/lighttpd/lighttpd.conf
# "mod_rewrite",
# "mod_redirect",
# "mod_alias",
↓
"mod_rewrite",
"mod_redirect",
"mod_alias",
# "mod_fastcgi",
↓
"mod_fastcgi",
#server.port = 81
↓
server.port = 3000 ←端口设置成3000不要与apache的重复
#server.bind = "127.0.0.1"
↓
server.bind = "localhost"
自启动
[root@host1 ~]# chkconfig lighttpd on
Rails应用程序的基本设置
[root@host1 ~]# cd /srv/www/lighttpd
[root@lighttpd]# mkdir rails
[root@lighttpd]# rails ./rails
赋予lighttpd用户的权限
[root@host1 ~]# cd
[root@host1 ~]# chown -R lighttpd. /srv/www/lighttpd/rails
[root@host1 ~]# chmod +x /srv/www/lighttpd/rails/public/dispatch.*
Apache+lighttpd的设定
[root@host1 ~]# vi /etc/lighttpd/lighttpd.conf
加在末尾
$HTTP["host"] =~ "ithurricane.dip.jp" {
server.document-root = "/srv/www/lighttpd/rails/public"
alias.url = ( "/" => "/srv/www/lighttpd/rails/public/" )
server.indexfiles = ( "index.html", "dispatch.fcgi" )
accesslog.filename = "/var/log/lighttpd/access.log"
server.errorlog = "/var/log/lighttpd/server.log"
server.error-handler-404 = "/dispatch.fcgi"
url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )
fastcgi.server = (
".fcgi" => (
"localhost" => (
"socket" => "/tmp/rails.fcgi.socket",
"bin-path" => "/srv/www/lighttpd/rails/public/dispatch.fcgi",
"bin-environment" => (
"RAILS_ENV" => "production",
"RAILS_ROOT" => "/srv/www/lighttpd/rails"
),
"idol-timeout" => 10,
"check-local" => "disable",
"min-procs" => 1,
"max_procs" => 20
)
)
)
}
[root@host1 ~]# /etc/init.d/lighttpd start
下面还要设置一下Apache把对Rails的请求转发到lighttpd
[root@host1 ~]# vi /etc/httpd/conf.d/virtualhost.conf
<VirtualHost *:80>
ServerName ithurricane.dip.jp
<IfModule mod_proxy.c>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</IfModule>
</VirtualHost>
大功告成,重载一下设定即可
[root@host1 ~]# /etc/init.d/httpd reload
最后导入图像处理库,和数据库SQLite3,这些都比较简单,一笔带过了,
[root@host1 ~]# yum install freetype
:
[root@host1 ~]# yum install libpng
:
[root@host1 ~]# yum install gd-devel
:
[root@host1 ~]# gem install ruby-gd -- --build-flag --with-freetype
:
[root@host1 ~]# gem install sqlite3-ruby
Select which gem to install for your platform (i686-linux)
1. sqlite3-ruby 1.2.1 (ruby)
2. sqlite3-ruby 1.2.1 (mswin32)
3. sqlite3-ruby 1.2.0 (mswin32)
4. sqlite3-ruby 1.2.0 (ruby)
5. Skip this gem
6. Cancel installation
> 1
终于完成了,把rails的服务打开来,看看成果吧,注意跑的是Apache的80端口哦
[root@host1 ~]# cd /srv/www/lighttpd/rails
[root@rails ~]# ruby script/server