当前位置: 首页 > 工具软件 > FCGI for Ruby > 使用案例 >

CentOS5.2 + Apache + lighttpd下搭建Ruby on Rails开发环境

封景曜
2023-12-01
最近看上了Ruby on Rails的敏捷开发技术了,虽然早已不是什么新鲜玩意了,

不过看着它越来越流行了,还是想看一看里面的究竟啊, 先从搭建Ruby on Rails的环境入手吧。

1.安装Ruby

rails框架也是Ruby做的,自然要先安装Ruby了,
yum install ruby -y

确认一下
Ruby -v

2.安装RubyGems

RubyGems是Ruby函数库的管理系统

在此之前先要导入dlutter软件仓库

# 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


确认
# yum --enablerepo=dlutter list

然后直接用命令安装RubyGems

# yum --enablerepo=dlutter -y install rubygems.noarch
确认版本
# gem --version
1.2.0

安装好应该是1.2.0的版本,最新是1.3.0的,有兴趣的朋友可以用源码编译升级


3.rails框架的导入

这个比较简单,直接用RubyGems来安装

# 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

[code="html"]
# wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
# tar zxvf fcgi-2.4.0.tar.gz
:
# cd fcgi-2.4.0
# ./configure
:插入代码
# gem install fcgi
# make
:
# make install
:

# cd
# rm -rf fcgi-2.4.0*
[/code]

下面用RubyGems把Ruby的FastCGI的包导进来
接下去就要安装lighttpd服务器了,先导入RPMforge 软件仓库

[code="html"]
# wget http://dag.wieers.com/packages/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
# rpm -Uhv rpmforge-release-0.3.6-1.el5.rf.i386.rpm
# vi /etc/yum.repos.d/rpmforge.repo
enabled = 1

enabled = 0
# wget http://dag.wieers.com/packages/RPM-GPG-KEY.dag.txt
# rpm --import RPM-GPG-KEY.dag.txt

确认
# yum --enablerepo=rpmforge list
[/code]

开始安装
# yum --enable=rpmforge install lighttpd
:
# yum --enable=rpmforge install lighttpd-fastcgi
:

一般系统已经有Apache服务了,为了避免冲突,还要配置一下lighttpd,

[code="html"]

# 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"
[/code]

自启动
# chkconfig lighttpd on

Rails应用程序的基本设置


# cd /srv/www/lighttpd
# mkdir rails
# rails ./rails

赋予lighttpd用户的权限
# cd
# chown -R lighttpd. /srv/www/lighttpd/rails
# chmod +x /srv/www/lighttpd/rails/public/dispatch.*

Apache+lighttpd的设定


# vi /etc/lighttpd/lighttpd.conf

加在末尾
[code="html"]
$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
)
)
)
}
# /etc/init.d/lighttpd start
[/code]

下面还要设置一下Apache把对Rails的请求转发到lighttpd


[code="html"]
# 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>
[/code]
大功告成,重载一下设定即可


# /etc/init.d/httpd reload

最后导入图像处理库,和数据库SQLite3,这些都比较简单,一笔带过了,


[code="html"]
# yum install freetype
:
# yum install libpng
:
# yum install gd-devel
:
# gem install ruby-gd -- --build-flag --with-freetype
:
# 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
[/code]

终于完成了,把rails的服务打开来,看看成果吧,注意跑的是Apache的80端口哦


[code="html"]
# cd /srv/www/lighttpd/rails
# ruby script/server
[/code]

[img]/upload/attachment/43667/b89eddd3-2e7a-3011-8831-aa61d00e6566.png[/img]
 类似资料: