git服务部署及capistrano部署完整教程20101220

生成远程git仓库
mlzboy@mlzboy:~$ mkdir -p git/b2c2.git
mlzboy@mlzboy:~$ cd git/b2c2.git/
mlzboy@mlzboy:~/git/b2c2.git$ git init --bare
Initialized empty Git repository in /home/mlzboy/git/b2c2.git/
生成ssh公钥
将生产服务器上的公钥,放到远程git仓库所在的服务器上
ssh-keygen -t rsa
一路回车
进入到cd ~/.ssh目录下
mlzboy@mlzboy-MacBook ~/.ssh $ ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
mlzboy@mlzboy-MacBook ~/.ssh $
将id_rsa.pub命名为authorized_keys上传到git仓库所在服务器的 ~/.ssh下
如果没有该目录,则可以使用ssh-keygen -t rsa创建
将本地库添加远程仓库
mlzboy@mlzboy-MacBook ~/my/b2c2 $ git remote add vm [email protected]:/home/mlzboy/git/b2c2.git
mlzboy@mlzboy-MacBook ~/my/b2c2 $ git push vm master
使用git remote -v查看当前库所有的远程仓库
[email protected]'s password: 
Counting objects: 1363, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (1286/1286), done.
Writing objects: 100% (1363/1363), 3.40 MiB | 5.15 MiB/s, done.
Total 1363 (delta 262), reused 0 (delta 0)
To [email protected]:/home/mlzboy/git/b2c2.git
 * [new branch]      master -> master
mlzboy@mlzboy-MacBook ~/my/b2c2 $ 
使用cap deploy:setup构建初始结构
在shared下创建config文件夹,将config下的database.yml和smtp_gmail.yml放到该config下
将public下system文件夹放到对应的shared/system中
执行cap deploy
注意要启动nginx,目录ubuntu下没有设为开机自动启动nginx
注意cap deploy:migrate执行的生产环境,所以建议还是上服务器上去使用rake db:migrate去执行
deploy.rb
#require 'capistrano/ext/multistage'
#require 'capistrano-helpers/php'
#require 'capistrano-helpers/shared'
#set :repository,  "/home/mlzboy/my/haha"
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load path.
require "rvm/capistrano"                  # Load RVM's capistrano plugin.
set :rvm_ruby_string, 'ruby-1.9.2-p0'        # Or whatever env you want it to run in.
set :rvm_type, :user
set :application, "b2c2"
set :ip,"192.168.1.103"
set :repository,  "#{ip}:/home/mlzboy/git/b2c2.git"
#set :repository,  "[email protected]:/geilibuy.git"
set :use_sudo, false
default_run_options[:pty] = true
#set :scm, :none
set :scm,:git
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
#set :deploy_via, :copy
set :deploy_to, "/home/mlzboy/b2c2"
#set(:path) { Capistrano::CLI.ui.ask("Type a path to deploy: ") }
#set(:repository) { fetch(:path) }
set :user,"mlzboy"
#set :username,"mlzboy"
set :password,"zzzzzz"
role :web, ip                          # Your HTTP server, Apache/etc
role :app, ip                          # This may be the same as your `Web` server
role :db,  ip, :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
 end
namespace :my_tasks do
  desc "Create database.yml and asset packages for staging"
  task :symlink, :roles => [:app] do
    symlink_hash = {
      "#{shared_path}/config/database.yml" => "#{release_path}/config/database.yml",
      "#{shared_path}/config/smtp_gmail.yml" => "#{release_path}/config/smtp_gmail.yml",
      #"#{shared_path}/system" => "#{release_path}/public/system",
      #"#{shared_path}/log" => "#{release_path}/log"
    }
    
    symlink_hash.each do |source, target|
      run "ln -snf #{source} #{target}"
    end
    #run "cd #{deploy_to}/current && bundle install vendor/gems"
    #run "cd #{deploy_to}/current && #{try_sudo} bundle install"
    #run "cd #{deploy_to}/current && rake db:migrate"
    
  end
end
namespace :bundler do
  task :create_symlink, :roles => :app do
    shared_dir = File.join(shared_path, 'bundle')
    release_dir = File.join(current_release, '.bundle')
    run("mkdir -p #{shared_dir} && ln -s #{shared_dir} #{release_dir}")
  end
 
  task :bundle_new_release, :roles => :app do
    bundler.create_symlink
    run "cd #{release_path} && bundle install --without test"
  end
end
 
after 'deploy:update_code', 'bundler:bundle_new_release'
after "deploy:update_code", "my_tasks:symlink"
 
#after :deploy, "deploy:restart"
#使用capistrano-helper还可以改进,不过使用这个gem目前还有依赖问题,有待研究
reference
nginx+passenger在实际部署时遇到,即使sudo kill pid也没办法删除假死的passenger,只有重启服务器,不知道该如何解决
如果想在capistrano中集中migrate功能,为保险起见一定要先执行完整备份mysql的操作,以免意外情况发生
20101221後續改進更新
加入nginx開始自啟動
sudo touch /etc/init.d/nginx
sudo vi /etc/init.d/nginx
#! /bin/sh
# Author: rui ding
# Modified: Geoffrey Grosenbach http://topfunky.com
# Modified: Clement NEDELCU
# Reproduced with express authorization from its contributors
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME


# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0

d_start() {
  $DAEMON || echo -n " already running"
}

d_stop() {
  $DAEMON –s quit || echo -n " not running"
}

d_reload() {
  $DAEMON –s reload || echo -n " could not reload"
}

case "$1" in
  start)
    echo -n "Starting $DESC: $NAME"
    d_start
    echo "."
  ;;
  stop)
    echo -n "Stopping $DESC: $NAME"
    d_stop
    echo "."
  ;;
  reload)
    echo -n "Reloading $DESC configuration..."
    d_reload
    echo "reloaded."
  ;;
  restart)
  echo -n "Restarting $DESC: $NAME"
  d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
  sleep 2
  d_start
  echo "."
  ;;
  *)
  echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
  exit 3
  ;;
esac
exit 0
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults
update-rc.d: warning: /etc/init.d/nginx missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
 Adding system startup for /etc/init.d/nginx ...
   /etc/rc0.d/K20nginx -> ../init.d/nginx
   /etc/rc1.d/K20nginx -> ../init.d/nginx
   /etc/rc6.d/K20nginx -> ../init.d/nginx
   /etc/rc2.d/S20nginx -> ../init.d/nginx
   /etc/rc3.d/S20nginx -> ../init.d/nginx
   /etc/rc4.d/S20nginx -> ../init.d/nginx
   /etc/rc5.d/S20nginx -> ../init.d/nginx
mlzboy@15-1688:~$ 
cap remote_backup#备份远程库
cap sync:setup#生成sync文件夹在shared下
cap sync:up #将本地库同步到远程

你可能感兴趣的:(capistrano)