首先要:yum install sudo which #如果有就算了
1,我们需要关心的主要是config/deploy.rb这个文件,我们先来看看程序自动为我们生成了些什么:
[root@master blog]# cat config/deploy.rb
set :application, "set your application name here"
set :repository, "set your repository location here"
set :scm, :subversion
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
role :web, "your web-server here" # Your HTTP server, Apache/etc
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"
# If you are using Passenger mod_rails uncomment this:
# if you're still using the script/reapear helper you will need
# these http://github.com/rails/irs_process_scripts
# 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
可以看到系统已经提示我们一些基本的设置了
首先,设置application
set :application, "blog" #在这里我给他起名字叫blog,当然这个根据具体情况了
设置repository,我用的是git:
set :repository, "ssh://[email protected]/usr/local/system/repository/blog.git"
set :scm, :git
set :scm_command, "/usr/local/git/bin/git" #这个也很关键啊,否则会找不到git命令了
#set :scm_passphrase, "xxxx" 没有密码的就不用设置了
接下来就是设置你的server,我的是在同一台机器上面,所以都相同了:
role :web, "www.weekface.info" # Your HTTP server, Apache/etc
role :app, "www.weekface.info" # This may be the same as your `Web` server
role :db, "www.weekface.info", :primary => true # This is where Rails migrations will run
再接下来是部署的位置:
set :deploy_to, "/usr/local/system/www/#{application}"
部署的时候是用ssh public key方式连接合服务器,这个在以前的文章中说明过
下面是使用者相关的配置:
set :user, "root"
set :runner, "root"
大部分部署指令会以:user设定的身份去运行,但和启动应用服务器相关的指令会以:runner设置的身份去运行.
最后,由于我用的是passenger,所以来设置一下启动passenger的相关设置吧:
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
2,部署
第一次部署需要运行下面的命令:
cap deploy:setup
报错了:
sh: sudo: command not found
#或者
sudo: sorry, you must have a tty to run sudo
google了一下,可以这样解决:
[root@li96-10 ~]# vim /etc/sudoers
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.
# You have to run "ssh -t hostname sudo <cmd>".
#
#Defaults requiretty #将这行注释掉
继续执行:
[root@master blog]# cap deploy:setup
* executing `deploy:setup'
* executing "sudo -p 'sudo password: ' mkdir -p /usr/local/system/www/blog /usr/local/system/www/blog/releases /usr/local/system/www/blog/shared /usr/local/system/www/blog/shared/system /usr/local/system/www/blog/shared/log /usr/local/system/www/blog/shared/pids && sudo -p 'sudo password: ' chmod g+w /usr/local/system/www/blog /usr/local/system/www/blog/releases /usr/local/system/www/blog/shared /usr/local/system/www/blog/shared/system /usr/local/system/www/blog/shared/log /usr/local/system/www/blog/shared/pids"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
command finished
这个命令会在服务器上配置好相关的目录结构.然后再运行:,以检查目录结构是否配置正确:
[root@master blog]# cap deploy:check
* executing `deploy:check'
* executing "test -d /usr/local/system/www/blog/releases"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
command finished
* executing "test -w /usr/local/system/www/blog"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
command finished
* executing "test -w /usr/local/system/www/blog/releases"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
command finished
* executing "which git"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
www.weekface.info: sh: which: command not found
command finished
The following dependencies failed. Please check them and try again:
--> `git' could not be found in the path (www.weekface.info) #见上面的解决办法:set :scm_command, "/usr/local/git/bin/git"
再试一下:
[root@master blog]# cap deploy:check
* executing `deploy:check'
* executing "test -d /usr/local/system/www/blog/releases"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
command finished
* executing "test -w /usr/local/system/www/blog"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
command finished
* executing "test -w /usr/local/system/www/blog/releases"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
command finished
* executing "which /usr/local/git/bin/git"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
command finished
You appear to have all necessary dependencies installed
这就代表检查成功了!
下面就开始第一次部署:
[root@master blog]# cap deploy:cold
* executing `deploy:cold'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
executing locally: "/usr/local/git/bin/git ls-remote ssh://www.weekface.info/usr/local/system/repository/blog.git HEAD"
* executing "/usr/local/git/bin/git clone -q ssh://www.weekface.info/usr/local/system/repository/blog.git /usr/local/system/www/blog/releases/20100107141957 && cd /usr/local/system/www/blog/releases/20100107141957 && /usr/local/git/bin/git checkout -q -b deploy a2546709a9109af3d6f098030c2fb7e3acc89b12 && (echo a2546709a9109af3d6f098030c2fb7e3acc89b12 > /usr/local/system/www/blog/releases/20100107141957/REVISION)"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
** [www.weekface.info :: err] Host key verification failed. #这里这里
** [www.weekface.info :: err] fatal: The remote end hung up unexpectedly
command finished
*** [deploy:update_code] rolling back
* executing "rm -rf /usr/local/system/www/blog/releases/20100107141957; true"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
command finished
failed: "sh -c '/usr/local/git/bin/git clone -q ssh://www.weekface.info/usr/local/system/repository/blog.git /usr/local/system/www/blog/releases/20100107141957 && cd /usr/local/system/www/blog/releases/20100107141957 && /usr/local/git/bin/git checkout -q -b deploy a2546709a9109af3d6f098030c2fb7e3acc89b12 && (echo a2546709a9109af3d6f098030c2fb7e3acc89b12 > /usr/local/system/www/blog/releases/20100107141957/REVISION)'" on www.weekface.info
解决办法是修改config/deploy.rb,加入:
set :branch, 'master'
set :deploy_via, :copy
继续:
[root@master blog]# cap deploy:cold
...
...
* executing "cd /usr/local/system/www/blog/releases/20100107143738; rake RAILS_ENV=production db:migrate"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
*** [err :: www.weekface.info] sh: rake: command not found
command finished
failed: "sh -c 'cd /usr/local/system/www/blog/releases/20100107143738; rake RAILS_ENV=production db:migrate'" on www.weekface.info
google了一下,在config/deploy.rb加入:
set :rake, "/usr/local/system/ruby/rake"
然后再继续:
[root@master blog]# cap deploy:cold
...
...
library: no such file to load -- system_timer #解决办法 : gem install system_timer
呵呵,以后再部署就可以直接用:
cap deploy
如果发生问题要rollback可以用:
cap deploy:rollback
到此为止…睡觉去
Rails启动时有个错:uninitialized constant User::Authentication,结果发现是restful_authentation这个插件目录里是空的,不知道怎么搞的,重新把文件拷贝过去就行了.
接下来还有在deploy.rb文件里自定义一些任务(task),完成一些其他任务,比如建立public/photos的连结等等:
首先,从git中删除不需要版本控制的文件:
在本机的blog目录下新建文件.gitignore:
config/database.yml
config/environment.rb
public/photos
log
nbproject
*~
*.cache
*.log
*.pid
tmp/**/*
以上这些文件都不需要版本控制,然后push,同时在服务器上要将config/database.yml,config/environment.rb,public/photos/*文件给他复制到shared目录下,并且将这些文件目录从git中删除,一会我们会给他 在部署的时候作link
[root@master blog]# git add .gitignore
[root@master blog]# git commit -am "add .gitignore"
[root@master blog]# git push
编辑deploy.rb文件,加入一下tasks:
desc "Symlink shared configs and folders on each release."
task :symlink_shared do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/config/environment.rb #{release_path}/config/environment.rb"
run "ln -nfs #{shared_path}/public/photos #{release_path}/public/photos"
#run "export RAILS_ENV=production && #{release_path}/script/workling_client start"
end
after 'deploy:update_code', 'deploy:symlink_shared'
基本就是这样了,以后还会介绍在实际使用过程中遇到的一些问题:
Rails Error: Unable to access log file. Please ensure that /usr/local/system/www/blog/releases/20100109050547/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
这个问题主要是因为shared/log目录没有可写权限,解决办法:
[root@li96-10 shared]# chmod -R 0666 log
还有一个问题:重启workling时报错:/usr/bin/env: ruby no such file or directory
* executing "cd /usr/local/system/www/blog/releases/20100109062537 && RAILS_ENV=production ./script/workling_client stop"
servers: ["www.weekface.info"]
[www.weekface.info] executing command
*** [err :: www.weekface.info] /usr/bin/env: ruby
*** [err :: www.weekface.info] : No such file or directory
原因是找不到ruby,下面是暂时解决办法:
[root@master blog]# vim script/workling_client
#将第一行的 :#!/usr/bin/env ruby
#修改为: #!/usr/local/system/ruby-1.8.7-p72/bin/ruby
#就是hard code了,呵呵
[root@master blog]# git commit -am "xx" #然后提交到git
就可以了