公司一直都想做自动化部署,但是一直没有去做.最近空出来一台服务器,所以想把这台服务器做一下自动化部署.

虽然现在是用rsync的文件列表方式更新上线.但是不可避免也会产生一些问题.

这里结合SVN使用的,所以前提是安装有subversion

一.安装ruby.网上大神都强烈安装1.9以上,所以我就直接下载了个最新版本.

编译安装ruby:

$./configure

$make && make install

二.安装capistrano.因为3.x以上版本已经取消了对SVN等版本管理的支持,所以只能安装2.x了.

$gem install capistrano --version "2.8.0"

这里它会自动安装一些其他程序.

其中有一个"net-ssh",我这里在线安装了2.8.0版本.连接到SSH的时候报错:

connection failed for: 192.168.1.2 (Net::SSH::AuthenticationFailed: Authentication failed for user user@192.168.1.2)

安装其他版本就可以了.我这里安装了2.7.0版本:

卸载掉2.8版:

$gem uninstall net-ssh --version "2.8.0"

安装2.7版:

$gem install net-ssh --version "2.7.0"

到这里,环境就已经安装好了.

三.然后就是capistrano的配置了.

首先,创建一个目录.这个目录是用来生成capistrano配置文件的目录.跟应用程序目录没有半点关系.并且这个目录是在capistrano服务端的.

比如是在/tmp/up/下,运行命令:

$cap .

完成后会生成Capfile和config/deploy.rb两个文件.其中capfile,这个是Capistrano需要的主要文件.deploy.rb这个文件包含你的应用程序部署所需的设置.主要就是得配置这个文件.

贴上我的配置:

set :application, "project"   #应用程序名称

set :repository,  "SVN项目地址"


set :scm, :subversion    #告诉capistrano使用SVN

# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`


set :scm_username, "user" #SVN用户名

set :scm_password, "passwd" #SVN用户的密码

set :scm_checkout, "export"    #SVN导出代码的方式(无版本控制信息)

set :deploy_to, "/www/webdata"    #导出到远程服务器上的代码存放路径

set :document_root, "/www/#{application}"    #WEB应用程序路径

role :web, "192.168.1.2"                          # WEB服务器地IP地址,下面的几个注释掉了.

#role :app, "your app-server here"                          # This may be the same as your `Web` server

#role :db,  "your primary db-server here", :primary => true # This is where Rails migrations will run

#role :db,  "your slave db-server here"


# SSH Settings

#

set :port, "port"    #SSH的端口号

set :user, "user"    #SSH到WEB服务器的用户名

set :password, "passwd"    #SSH到WEB服务器的密码

set :use_sudo, false    #不允许使用sudo

set :ssh_options, {:forward_agent => true}



# if you're still using the script/reaper helper you will need

# these http://github.com/rails/irs_process_scripts


# If you are using Passenger mod_rails uncomment this:

# namespace :deploy do

#   task :start do ; end

#   task :stop do ; end

#   task :restart, :roles => :app, :except => { :no_release => true } do

#     run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"

#   end

# end


namespace :deploy do


task :update do

   transaction do

       update_code    

       symlink

   end

end


task :finalize_update do    #更新完之后要做的

   transaction do

       run "chmod -R g+w #{releases_path}/#{release_name}" #修改权限,其中releases_path相对于我的项目来说,真实路径是:/www/webdata/releases/,release_name就是从SVN导出的版本目录

   end

end


task :symlink do     #软链接

   transaction do

       run "ln -nfs #{current_release} #{deploy_to}/#{current_dir}" #将导出的代码目录链接到/www/webdata/current/

       run "ln -nfs #{deploy_to}/#{current_dir} #{document_root}"    #再将上一步产生的current链接为网站根目录.这里做了两部连接是为了回滚.后面说.

   end

end


task :migrate do

   # nothing

end


task :restart do

   # nothing

end


end

到这里简单的配置就完成了,还可以添加其他命令.

四.执行命令

初始化:

$cap deploy:setup

完成后,会在WEB服务器上的/www/webdata/目录下面生成 两个目录:releases与shared

再执行检查命令:

$cap deploy:check

该命令将检查你的本机环境及服务器,并定位问题。如果你看到错误消息,修复后再运行此命令。一旦你执行 cap deploy:check 没有错误,则可继续处理。

下一步就可以更新代码到远程WEB服务器了;

$cap deploy:update

如果没有错误产生,那代码就更新到了远程服务器了.

在远程WEB服务器上面的/www目录下面:

drwxrwxr-x 8 www  www  4096 03-19 15:53 releases

drwxrwxr-x 5 root root 4096 03-19 15:45 shared

同时也会把releases里面最后导的版本连接到/www目录下面,名称是你的应用名称.

执行一次update就会生成一个版本目录.并且会将最新生成的目录链接到WEB根目录.

五.回滚

回滚命令:

$gem deploy:rollback

这个命令执行的操作会把把current这个软链接重新链接到上一个版本目录上,实现回滚.

基于这个应该可以实现很多文件的部署及回滚.比如配置文件等等

其他的参数及功能以后慢慢研究再补上

主要参考这个文章:http://www.claytonlz.com/2008/08/php-deployment-with-capistrano/