icon Capistrano 入门

第7章 Rails应用程序的启动、停止、重启

这章介绍Rails应用程序的启动、停止、重启。。

第1章 安装提到的,在 Mongrel 1.1.4 上运行 Rails 应用程序是这篇文章的前提条件。

首先,确认在目标主机上安装的 RubyGems 版本。

$ gem --version
1.1.1

如果没有安装 RubyGems 的话,就从 RybyForge 下载最新版并安装。

$ wget http://rubyforge.org/frs/download.php/35283/rubygems-1.1.1.tgz
$ tar xzf rubygems-1.1.1.tgz
$ cd rubygems-1.1.1
$ sudo ruby setup.rb

如果是旧版本的话请更新。

$ sudo gem update --system

接下来确认 Mongrel 的版本。

$ gem list mongrel

*** LOCAL GEMS ***

mongrel (1.1.4)
mongrel_cluster (1.0.5)

如果什么都没有显示或是显示的是 Mongrel 旧版本的话,进行安装。

$ sudo gem install mongrel

同样也安装 mongrel_cluster 。

$ sudo gem install mongrel_cluster

设置生产环境用的 database.yml (代码样本省略)。

$ cd /var/rails/ballad/shared
$ sudo -u app mkdir config
$ sudo -u app vi config/database.yml

设置spin 脚本。

$ sudo -u app mkdir script
$ sudo -u app vi script/spin

spin 脚本的内容如下:

#!/bin/sh
/var/rails/ballad/current/script/process/spawner -p 3000 -i 4
<code>-p</code> 选项指定端口号,<code>-i</code> 选项指定 mongrel 实例数。在上述例中、使用3000到3003的端口号,运行了4个 mongrel 实例。还有,虽然通过<code>-e</code> 选项能指定“执行环境(development|test|production)”,但是因为默认 <code>production</code> ,通常不必指定也可以。

返回本地主机,在 config/deploy.rb 添加以下内容:

desc "Copy shared config files to current application."
task :after_update_code, :roles => :app do
  run "cp -f #{shared_path}/config/database.yml #{release_path}/config/"
  run "cp -f #{shared_path}/script/spin #{release_path}/script/"
  run "chmod u+x #{release_path}/script/spin"
end

这个 after_update_code 任务,会在执行 update_code 任务后自动执行。它指把 database.ymlspin 复制到正确的目录,并给予 spin 脚本运行权限。

那尝试执行 deploy:update 任务。在 /var/rails/ballad/current/script 目录中如果生成了 spin 文件就 OK 了。


其次,建立数据库。但是先需要完成数据库管理系统的安装。

首先,登陆远程主机并创建数据库:

$ rake db:create RAILS_ENV=production

然后迁移。通常执行 rake db:migrate RAILS_ENV=production 也可以,但在这里我们还是从本地主机通过 Capistrano 执行:

% cap deploy:migrate

到这里准备完成。可以启动Rails应用程序。

% cap deploy:start
  * executing `deploy:start'
  * executing "sh -c 'cd /var/rails/ballad/current && nohup script/spin'"
    servers: ["alpha.oiax.jp"]
    [alpha.oiax.jp] executing command
 ** [out :: alpha.oiax.jp] => Starting mongrel dispatchers
 ** [out :: alpha.oiax.jp] Checking if something is already running on 0.0.0.0:3000...NO
 ** [out :: alpha.oiax.jp] Starting dispatcher on port: 0.0.0.0:3000
 ** [out :: alpha.oiax.jp] Checking if something is already running on 0.0.0.0:3001...NO
 ** [out :: alpha.oiax.jp] Starting dispatcher on port: 0.0.0.0:3001
 ** [out :: alpha.oiax.jp] Checking if something is already running on 0.0.0.0:3002...NO
 ** [out :: alpha.oiax.jp] Starting dispatcher on port: 0.0.0.0:3002
 ** [out :: alpha.oiax.jp] Checking if something is already running on 0.0.0.0:3003...NO
 ** [out :: alpha.oiax.jp] Starting dispatcher on port: 0.0.0.0:3003
    command finished

deploy:start 换为 deploy:stop 的话就中止,换成 deploy:restart 的话就重新启动。


说一下,一旦开始程序运用,不伴随迁移的小修改会反复进行,所以将 deploy:update 任务和 deploy:restart 任务结合使用的次数会变多。因此要准备好 deploy 任务以顺利运行这两个任务。这样一来加上在应用程序上的修改,只在本地主机输入以下指令,进行一次更新源代码和重启程序的操作就行:

% cap deploy

设定变量 svn_user 的情况如下。

% svn_user=kuroda cap -q deploy

(2008/05/17)