19-07-26 22:12 字数 6634 阅读 711 已编辑

Capistrano 官网


Capistrano 是由 ruby 写的因此需要安装 ruby

sudo yum install ruby


sudo yum remove ruby





Building from Source
Of course, you can install Ruby from source. Download and unpack a tarball, then just do this:

$ ./configure
$ make
$ sudo make install

By default, this will install Ruby into /usr/local. To change, pass the --prefix=DIR option to the ./configure script.

You can find more information about building from source in the Ruby README file.

Using the third-party tools or package managers might be a better idea, though, because the installed Ruby won’t be managed by any tools.


$ sudo ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby
$ sudo ln -s /usr/local/ruby/bin/gem /usr/bin/gem


rm -rf  ./test_chk_ln


rm -rf ./test_chk_ln/ (这样就会把原来test_chk下的内容删除)




General Usage The following command will install the latest released capistrano v3 revision:

$ gem install capistrano

Or grab the bleeding edge head from:

$ git clone
$ cd capistrano
$ gem build *.gemspec
$ gem install *.gem

Capistrano deploys using SSH. Thus, you must be able to SSH (ideally with keys and ssh-agent) from the deployment system to the destination system for Capistrano to work.
You can test this using a ssh client, e.g. ssh myuser@destinationserver. If you cannot connect at all, you may need to set up the SSH server or resolve firewall/network issues. Look for a tutorial (here are suggestions for Ubuntu and RedHat/CentOS).

If a password is requested when you log in, you may need to set up SSH keys. GitHub has a good tutorial on creating these (follow steps 1 through 3). You will need to add your public key to ~/.ssh/authorized_keys on the destination server as the deployment user (append on a new line).

More information on SSH and login is available via the Authentication and Authorisationsection of the guide.




$ cap install


├── Capfile
├── config
│ ├── deploy
│ │ ├── production.rb
│ │ └── staging.rb
│ └── deploy.rb
└── lib
└── capistrano
└── tasks




config valid for current version and patch releases of Capistrano lock "~> 3.11.0"

set :application 设置应用的名称随便
set :application, "shop" 
set :repo_url 设置仓库地址默认git
set :repo_url, ""
git config --global credential.helper store  git记住密码

Default branch is :master
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

Default deploy_to directory is /var/www/my_app_name;设置代码要部署的目录
set :deploy_to, "/var/www/my_app_name"
set :deploy_to, "/var/www/test_shop_web"

Default value for :format is :airbrussh.
set :format, :airbrussh

You can configure the Airbrussh format using :format_options.
These are the defaults.
set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto

Default value for :pty is false
set :pty, true

Default value for :linked_files is [];设置具体共享文件
append :linked_files, "config/database.yml"
append :linked_files, "", ""

Default value for linked_dirs is [];设置共享目录
append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"
append :linked_dirs, "data"

Default value for default_env is {}
set :default_env, { path: "/opt/ruby/bin:$PATH" }

Default value for local_user is ENV['USER']
set :local_user, -> { `git config`.chomp }

Default value for keep_releases is 5
set :keep_releases, 5   设置保留最近的版本个数以便回滚操作




server-based syntax
Defines a single server with a list of roles and multiple properties.
You can define all roles on a single server, or split them:

server "", user: "deploy", roles: %w{app db web}, my_property: :my_value
server "", user: "deploy", roles: %w{app web}, other_property: :other_value
server "", user: "deploy", roles: %w{db}

role-based syntax

Defines a role with one or multiple servers. The primary server in each
group is considered to be the first unless any hosts have the primary
property set. Specify the username and a domain or IP for the server.
Don't use `:all`, it's a meta role.

role :app, %w{}, my_property: :my_value
role :web, %w{}, other_property: :other_value
role :web, %w{aspire@}
role :db,  %w{}

You can set any configuration variable like in config/deploy.rb
These variables are then only loaded and set in this stage.
For available Capistrano configuration variables see the documentation page.
Feel free to add new variables to customise your setup.

Custom SSH Options
You may pass any option but keep in mind that net/ssh understands a
limited set of options, consult the Net::SSH documentation.

Global options

set :ssh_options, {
   keys: %w(/home/rlisowski/.ssh/id_rsa),
   forward_agent: false,
   auth_methods: %w(password)
set :ssh_options, {
  keys: %w(/home/aspire/.ssh/authorized_keys),
  forward_agent: false,
  auth_methods: %w(publickey)

set :deploy_to, "/var/www/test_shop_web"


set :ssh_options, {
  user: "uname", # overrides user setting above
  forward_agent: false,
  auth_methods: %w(password),#授权方式
  password: "1234qwer"#指定ssh用户登录密码

The server-based syntax can be used to override options:
server "",
   user: "user_name",
   roles: %w{web app},
   ssh_options: {
     user: "user_name", # overrides user setting above
     keys: %w(/home/user_name/.ssh/id_rsa),
     forward_agent: false,
     auth_methods: %w(publickey password)
     # password: "please use keys"


通常情况下需要执行cap plus deploy会因为用户没有设置密码而抛出异常,为了免密码登录和不在配置文件中显式指定用户密码,通常用ssh建立信任关系




$ adduser deploy
$ passwd -l deploy


$ ssh-keygen -t rsa -C ''

To avoid having to type this passphrase every time you need to use a key, most operating systems have a concept of a key agent.

This key agent stores SSH keys securely between uses, typically the first time a key is needed in a given time period, the SSH agent will load the key, prompt you for your passphrase and then the key agent will remember the key for a certain amount of time (on OSX it tends to be indefinite, on linux this can vary from 15 minutes upwards.)
We can see which keys are loaded in the SSH agent by running ssh-add -l

$ ssh-add -l

If you don’t see any keys listed, you can simply run ssh-add:me@localhost $ ssh-add
Identity added: /Users/me/.ssh/id_rsa (/Users/me/.ssh/id_rsa)

如果系统提示:could not open a connection to your authentication agent 则需要执行以下命令:ssh-agent bash 进入到~/.ssh中

$ cat >> ~/.ssh/authorized_keys
$ chmod 700 .ssh
$ chmod 600 .ssh/authorized_keys



关注 收藏 改进 举报
1 条评论
排序方式 时间 投票

帮们更新了排版。Markdown 使用指南