我正在尝试让 capistrano 将我的 rails 应用程序部署到 vps。
我登录到 vps,添加了一个新的“部署者”用户,并通过以下方式给了他 sudo 权限:
adduser deployer
并在 visudo
deployer ALL=(ALL:ALL) ALL
我安装了 rvm、ruby、postgres、postfix 和 nginx。全部使用 apt-get,最新版本的 ruby 和 rvm。在我的 gem 文件中,我正在使用:
gem 'unicorn'
# Deploy with Capistrano
gem 'capistrano'
gem 'rvm-capistrano', '>= 1.3.0.rc11'
根据 capistrano github 自述文件,尝试运行 cap deploy:setup 时,我遇到了一堆 rvm 错误,但通过在我的 deploy.rb 文件中使用以下内容在 cap deploy:setup 上重新安装 rvm 来修复它们:
set :rvm_ruby_string, 'ruby-1.9.3-p392' # use the same ruby as used locally for deployment
set :rvm_install_ruby_params, '--1.9' # for jruby/rbx default to 1.9 mode
set :rvm_install_pkgs, %w[libyaml openssl] # package list from https://rvm.io/packages
set :rvm_install_ruby_params, '--with-opt-dir=/usr/local/rvm/usr' # package support
before 'deploy:setup', 'rvm:install_rvm' # install RVM
before 'deploy:setup', 'rvm:install_pkgs' # install RVM packages before Ruby
before 'deploy:setup', 'rvm:install_ruby' # install Ruby and create gemset, or:
before 'deploy:setup', 'rvm:create_gemset' # only create gemset
before 'deploy:setup', 'rvm:import_gemset' # import gemset from file
当我运行 cap deploy:cold 时,它运行良好,直到它给出错误的结尾:
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'ruby-1.9.3-p392' -c 'cd /home/deployer/apps/blog/releases/20130301223924 && bundle install --gemfile /home/deployer/apps/blog/releases/20130301223924/Gemfile --path /home/deployer/apps/blog/shared/bundle --deployment --quiet --without development test'" on 198.211.127.46
因此,我使用以下命令将我的“部署者”用户添加到“rvm”组中:
sudo usermod -G rvm deployer
运行 cap deploy:cold 后,得到错误:
** [out :: 198.211.127.46] cannot create directory `/home/deployer/apps/blog/releases/20130301223718'
** [out :: 198.211.127.46] : Permission denied
任何见解将不胜感激。我快到了。
这是我的 deploy.rb 文件:
require "bundler/capistrano"
require "rvm/capistrano"
server "198.211.127.46", :web, :app, :db, primary: true
set :port, 46000
set :application, "blog"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, true
set :keep_releases, 5
set :scm, "git"
set :repository, "git@bitbucket.org:myusername/#{application}.git"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
######
set :rvm_ruby_string, 'ruby-1.9.3-p392' # use the same ruby as used locally for deployment
set :rvm_install_ruby_params, '--1.9' # for jruby/rbx default to 1.9 mode
set :rvm_install_pkgs, %w[libyaml openssl] # package list from https://rvm.io/packages
set :rvm_install_ruby_params, '--with-opt-dir=/usr/local/rvm/usr' # package support
before 'deploy:setup', 'rvm:install_rvm' # install RVM
before 'deploy:setup', 'rvm:install_pkgs' # install RVM packages before Ruby
before 'deploy:setup', 'rvm:install_ruby' # install Ruby and create gemset, or:
before 'deploy:setup', 'rvm:create_gemset' # only create gemset
before 'deploy:setup', 'rvm:import_gemset' # import gemset from file
########
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
sudo "mkdir -p #{shared_path}/config"
sudo "chown deployer:deployer #{shared_path}/config" # added this to get around sftp permisssions
put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
# sudo "chmod +x #{release_path}/config/unicorn_init.sh"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
# run "chmod +x #{release_path}/config/unicorn_init.sh"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end