今天在我的小服务器上配置了一下Ruby的Rails开发框架,发现Ruby的确非常好用。
我的小机器上安装的是
Novell SLES 11.0,参考
Configuring Ruby Rails for Apache on SUSE Linux Enterprise Server、
Ruby on Rails 2.1.x Scaffolding及
Rails, Apache2 and SUSE,采用Apache的fastcgi模块访问实验虚拟主机www.dws.cn。
首先通过YaST安装必要的软件包,主要是以下几个:
FastCGI
ruby
ruby-devel
ruby-fcgi
ruby-mysql
rubygem-rails
rubygem-rake
rubygems
另外,还需要通过下载rpm安装apache2-mod_fastcgi,下载链接为:
apache2-mod_fastcgi-2.4.2-39.1.i586.rpm。下载完成后直接安装即可:
rpm -ivh apache2-mod_fastcgi-2.4.2-39.1.i586.rpm
一切就绪后,准备运行第一个Rails应用:
dws:~ # cd /srv/www/htdocs/
dws:/srv/www/htdocs # rails www.dws.cn
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_rails_defaults.rb
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/console
create script/dbconsole
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/performance/request
create script/process/reaper
create script/process/spawner
create script/process/inspector
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/422.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
如果一切正常,第一个Rails应用就成功布置完成了,运行以下命令运行这个应用的服务器脚本:
dws:/srv/www/htdocs/www.dws.cn # ruby script/server
=> Booting WEBrick...
=> Rails 2.1.2 application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2009-07-21 12:31:22] INFO WEBrick 1.3.1
[2009-07-21 12:31:22] INFO ruby 1.8.7 (2008-08-11) [i586-linux]
[2009-07-21 12:31:22] INFO WEBrick::HTTPServer#start: pid=3547 port=3000
可以通过
http://localhost:3000/来访问示例页面,如果不是从服务器本机访问,请先暂时停止SLES 11.0的防火墙,否则通过其他主机无法验证刚才创建的Rails应用是否已经能够正常运行。下图为通过ip由其他主机访问刚才创建的示例页面的屏幕截图:
通过Yast启用Apache的rewite、vhost_alias服务器模块:
上图显示默认这两项是禁用的,将其状态转换为启用即可。
服务器模块fastcgi可能无法直接通过YaST启用,可以直接在配置文件
/etc/sysconfig/apache2的第103行的最后添加fastcgi来启用:
上图显示添加后的结果。
可以通过YaST来添加vhost,也可以手动添加,配置文件(/etc/apache2/vhosts.d/yast2_vhosts.conf)的内容如下:
<VirtualHost 10.15.76.105>
DocumentRoot /srv/www/htdocs/www.dws.cn/public
ServerName www.dws.cn
ServerAdmin opentiss@gmail.com
AddHandler fastcgi-script .fcgi
AddHandler cgi-script .cgi
<Directory /srv/www/htdocs/www.dws.cn/public>
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly."
Options ExecCGI FollowSymLinks
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
为使手动添加的虚拟主机选项生效,重启Apache:
/etc/init.d/apache2 restart
修改浏览主机的/etc/hosts文件,添加如下内容,以使其能够通过域名访问虚拟主机:
10.15.76.105 www.dws.cn
如果一切正常,浏览器的显示应该如下图所示:
这就表明通过Apache的服务器模块fastcgi访问Rails应用已经配置成功了。
下面我们来创建一个简单的MySQL数据库交互应用。首先,创建MySQL数据库脚本文件dws.sql,内容如下:
create database `dws` default character set utf8 collate utf8_general_ci;
grant all privileges on dws.* to 'dws'@'localhost' identified by 'dws' with grant option;
use dws;
create table websites (
id int(8) not null auto_increment,
siteName varchar(128) ,
domainName varchar(128) ,
onlineDate date ,
introduce text ,
primary key(id)
);
insert into websites values(0, 'opentiss', 'opentiss.net', '2007-9-2', 'Opentiss\'s website.');
insert into websites values(0, 'Wikan translation', 'wikan.opentiss.net', '2009-7-11', 'Wikan translation - 维勘翻译');
insert into websites values(0, 'openJDiary', 'openjdiary.opentiss.net', '2009-7-18', 'Project page of openJDiary.');
执行此脚本文件:
mysql -u root < dws.sql
为测试方便,将原来的Rails应用更名,以生成一个自动配置好的MySQL数据库交互应用:
dws:/srv/www/htdocs # mv www.dws.cn dws
dws:/srv/www/htdocs # rails -d mysql www.dws.cn
将/srv/www/htdocs/www.dws.cn/config/database.yml中的development配置更新为刚才创建的数据库,例如:
development:
adapter: mysql
encoding: utf8
database: dws
username: root
password:
socket: /var/lib/mysql/mysql.sock
主要是更改了数据库名称设置database。然后创建对表websites查看、修改、删除的示例页面(scaffold操作):
dws:/srv/www/htdocs # cd www.dws.cn
dws:/srv/www/htdocs/www.dws.cn # ruby script/generate scaffold website siteName:string domainName:string onlineDate:date introduce:text
此时在浏览器中看到的效果如下:
点击第二条的Edit,你就可以修改内容并保存到数据库了:
Ruby是一种动态的、面向对象的开源编程语言,简洁易学,如果你想到
Ruby Forum注册一个用户,会被问及一个简单的Ruby问题,例如:
What does the following ruby code print out?
puts (('11' * 2).to_i/2)
Please type the answer:
其实这是Ruby Forum在确认您是否真的是Ruby爱好者,故意考考你的,这也不难,只需要将以上puts语句写入到文件rf.rb中,例如:
puts (('11' * 2).to_i/2)
然后执行:
ruby rf.rb
结果是什么呢?相信如果你是Ruby爱好者的话,已经知道结果了。Ruby就是这么简单,爱好学习新技术的你赶快加入到Ruby的行列中来吧。