1:我的Application,是基于rails 2.2.2. 如果采用最新的rails的2.3.2版本。记得迁移程序。

迁移命令:

rake rails:update:application_controller

(具体请参考:[url]http://guides.rubyonrails.org/2_3_release_notes.html[/url])



2:运行环境基于production,在运行环境上面请配置好数据库。并运行数据迁移任务:



命令:

rake RAILS_ENV=production db:schema:load




3:部署环境基于Apache+mod_balancer + mongrel + mongrel_cluster. 首先请安装

mongrel/mongrel_cluser

gem.



4:使用capistrano部署必须明白的概念就是当前的机器和要部署的机器。接下来的操作都是在当前的机器上做的。你在这个机器上设置好所有的事情。包括源码的更新svn服务器,登录远程需要部署的机器的信息等等。早些年的书刊都会告诉你使用


cap --apply-to



命令。但是现在的capistrano都已经不支持这个option了。



5:部署设置步骤(都是在当前机器。也就是说开发机器,在开发环境下面做所有这些操作)


1>: 设置mongrel。运行


mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1



在applicaion的跟目录下运行),生成名为config/mongrel_cluster.yml的文件。


其内容大概如下:


---


cwd: /var/www/linknovel/current


log_file: log/mongrel.log


port: "8000"


environment: production


address: 127.0.0.1


pid_file: tmp/pids/mongrel.pid


servers: 4



或者你可以不通过上面那个命令,直接手写这个文件。注意第一个cwd的参数是的应用部署在远程服务器上的路径。而且最后一段必须是current。(因为capistrano每次checkout的代码都是放在current目录下的。)



2>: 你可以通过

mongrel_rails cluster::start

来启动rails程序了。(在本地开发机器上就可以跑跑看)



3>:早些版本的capistrano可能都是通过类似cap --apply-to来启动cap部署的。现在已经改了。 使用

capify

直接开始部署。运行这个命令会生成量个文件。


分别是:Capfile(用以放置cap task的地方)


config/deploy.rb. 这个里面是一些远程机器的地址以及svn信息等等。


我的deployv.rb的信息如下:



#User setting


set :application, "linknovel"


set :repository, "https://linknovel.googlecode.com/svn/trunk"


set :svn_username, "234aini"


set :svn_password, "*****************"



# If you aren't deploying to /u/apps/#{application} on the target


# servers (which is the default), you can specify the actual location


# via the :deploy_to variable:


set :deploy_to, "/var/www/#{application}"


set :user, "xye"


set :password, "xye123"


set :use_sudo,false



# If you aren't using Subversion to manage your source code, specify


# your SCM below:


# set :scm, :subversion



role :app, "192.168.1.6"


role :web, "192.168.1.6"


role :db, "192.168.1.6", :primary => true



task :chmod_spin do


#给启动脚本加权限


path = "/var/www/#{application}/current/script/spin"


run "chmod a+x #{path}"


end



很容易看懂这个文件。



4>设置好后。就是使用cap的任务来做一些操作了。


cap deploy:update

checkout源码到远程机器上。并放在你设定的路径的名为current的目录下。


cap deploy:check

检查所有部署设置是否都已经ok。


cap deploy:start

启动rails服务。 这个启动默认会调用script目录下的spin脚本。所以需要修改脚本为合适的启动命令。


我的是:


#!/bin/sh


mongrel_rails cluster::start -C /var/www/linknovel/current/config/mongrel_cluster.yml



到此为止。cap的设置都ok了。使用cap就可以部署了。 详细请参考:[url]http://www.capify.org/getting-started/from-the-beginning/[/url]



6:结合Apache。这处让我有点迷惑。


1>首先确保apache已经加载了mod_proxy mod_http_proxy mod_balance_proxy.


2>在你的apache配置中加如下代码:



<VirtualHost *>


# Server name


ServerName linknovel.com



# Proxy ACL


<Proxy *>


Order allow,deny


Allow from all


</Proxy>



# Proxy directives


ProxyPass / http://127.0.0.01:8000/


ProxyPassReverse / http://127.0.0.1:8000/


ProxyPreserveHost on


#更多请自己配置



# Logfiles


ErrorLog /var/log/apache2/linknovel.com.error.log


CustomLog /var/log/apache2/linknovel.com.access.log combined



</VirtualHost>




好了。启动apache.打开浏览器访问,应该没有问题了。



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



最近用一个国内的rails主机来测试应用。[url]http://zoomtype.info/[/url] 这个空间可以有一个月的试用期。蛮不错的。



部署很简单。我的应用测试URL如下:[url]http://liusu.zoomtype.info[/url](很简陋的程序,为学习用的)



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


性能测试:



没有采用很复杂的测试方法,用ab随便测试了主页的访问速度,测试结果如下:



从深圳主机访问:


ab -n 3000 -c 6 -q -v -w http://liusu.zoomtype.info/




测试结果:



Benchmarking liusu.zoomtype.info (be patient).....done




Server Software: Apache/2.2.3


Server Hostname: liusu.zoomtype.info


Server Port: 80



Document Path: /novel/list


Document Length: 4133 bytes



Concurrency Level: 6


Time taken for tests: 916.887348 seconds


Complete requests: 3000


Failed requests: 0


Write errors: 0


Total transferred: 13962009 bytes


HTML transferred: 12399000 bytes


Requests per second: 3.27 [#/sec] (mean)


Time per request: 1833.775 [ms] (mean)


Time per request: 305.629 [ms] (mean, across all concurrent requests)


Transfer rate: 14.87 [Kbytes/sec] received



Connection Times (ms)


min mean[+/-sd] median max


Connect: 174 458 893.4 218 10372


Processing: 202 1350 5723.4 254 182001


Waiting: 194 783 3803.3 242 141086


Total: 377 1809 5912.7 486 185888



Percentage of the requests served within a certain time (ms)


50% 486


66% 528


75% 624


80% 1284


90% 3562


95% 7332


98% 16495


99% 21143


100% 185888 (longest request)



----香港那边的机器做的测试:



Benchmark test for http://liusu.zoomtype.info/novel/list


This is ApacheBench, Version 2.0.41-dev <$Revision: 1.141 $> apache-2.0


Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/


Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/



Benchmarking liusu.zoomtype.info (be patient).....done




Server Software: Apache/2.2.3


Server Hostname: liusu.zoomtype.info


Server Port: 80



Document Path: /novel/list


Document Length: 4133 bytes



Concurrency Level: 10


Time taken for tests: 74.892236 seconds


Complete requests: 300


Failed requests: 0


Write errors: 0


Total transferred: 1404888 bytes


HTML transferred: 1245983 bytes


Requests per second: 4.01 [#/sec] (mean)


Time per request: 2496.408 [ms] (mean)


Time per request: 249.641 [ms] (mean, across all concurrent requests)


Transfer rate: 18.31 [Kbytes/sec] received



Connection Times (ms)


min mean[+/-sd] median max


Connect: 37 583 1722.0 42 21034


Processing: 52 1592 2479.2 987 25875


Waiting: 52 423 1955.7 59 23208


Total: 91 2175 3033.8 1242 25919



Percentage of the requests served within a certain time (ms)


50% 1242


66% 2071


75% 3098


80% 3602


90% 4696


95% 6504


98% 11543


99% 14411


100% 25919 (longest request)



------并发大了好像就比较慢了。。 不过总体还可以吧




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



使用rails开发一个简单的网站是很快呢! 可是不熟悉的人可能会被“通用方式”迷惑吧。 就比如,每添加一个 controller,每添加一个页面,我都会想,这样是不是rails做事的标准方式。甚至连给action取名,我都会去犹豫,这个操作的名字是不是符合惯例,符合rails的惯例。其实也蛮折腾的。 后来熟悉点了,慢慢会好点。做这个学习的例子,中间有过一次直接全部删除了重来,感觉更快。呵呵。整个网站例子代码是后来一次用两周的下班时间完成的。



不过我的审美观很差,网站实在是不美观啊!