Ruby on rails Rails Capistrano部署

Ruby on rails Rails Capistrano部署,ruby-on-rails,deployment,capistrano,Ruby On Rails,Deployment,Capistrano,我第一次尝试将rails应用程序部署到DigitalOcean,而不是Heroku,主要是因为价格。我对Capistrano和VPS部署完全陌生,我完全迷路了。我创建了1-click-Rails-droplet,并遵循了本教程: 以下是my deploy.rb文件中的设置: require 'capistrano/ext/multistage' set :stages, ["staging", "production"] set :default_stage, "staging" set

我第一次尝试将rails应用程序部署到DigitalOcean,而不是Heroku,主要是因为价格。我对Capistrano和VPS部署完全陌生,我完全迷路了。我创建了1-click-Rails-droplet,并遵循了本教程:

以下是my deploy.rb文件中的设置:

require 'capistrano/ext/multistage'

set :stages, ["staging", "production"]
set :default_stage, "staging"

set :application, "myAppName"
set :scm, :git
set :repository, "myGitRepository"
set :use_sudo, :false

set :deploy_via, :remote_cache

set :scm_passphrase, "myPassword"
set :user, "root"
role :web, "xx.xxx.x.xxx" 
role :app, "xx.xxx.x.xxx" 
staging.rb文件:

server "xx.xxx.x.xxx", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/xx.xxx.x.xxx_staging"
以及production.rb文件

server "xx.xxx.x.xxx", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/xx.xxx.x.xxx"
现在,当我跑的时候

cap deploy:check
在终端中,我得到以下信息:

[xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 88ms
  * executing "test -w /var/www/xx.xxx.x.xxx_staging"
    servers: ["xx.xxx.x.xxx"]
    [xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 79ms
  * executing "test -w /var/www/xx.xxx.x.xxx_staging/releases"
    servers: ["xx.xxx.x.xxx"]
    [xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 72ms
  * executing "which git"
    servers: ["xx.xxx.x.xxx"]
    [xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 85ms
  * executing "test -w /var/www/xx.xxx.x.xxx_staging/shared"
    servers: ["xx.xxx.x.xxx"]
    [xx.xxx.x.xxx] executing command
xx.xxx.x.xxx: env: 
xx.xxx.x.xxx: sh
xx.xxx.x.xxx: : No such file or directory
xx.xxx.x.xxx: 
    command finished in 81ms
The following dependencies failed. Please check them and try again:
--> `/var/www/xx.xxx.x.xxx_staging/releases' does not exist. Please run `cap staging deploy:setup'. (xx.xxx.x.xxx)
--> You do not have permissions to write to `/var/www/xx.xxx.x.xxx_staging'. (xx.xxx.x.xxx)
--> You do not have permissions to write to `/var/www/3xx.xxx.x.xxx_staging/releases'. (xx.xxx.x.xxx)
--> `git' could not be found in the path (xx.xxx.x.xxx)
--> `/var/www/xx.xxx.x.xxx_staging/shared' is not writable (xx.xxx.x.xxx)
我已经在谷歌上搜索了很长一段时间,但无法解决这个问题。由于我对这一点完全陌生,并且感觉在学习创建rails应用程序和了解部署rails应用程序的所有知识之间存在着巨大的差距(顺便说一句:早些时候ftp上传发生了什么事?),我真的希望有人知道如何解决这个问题,并能引导我朝着不太陡峭的部署学习曲线前进。顺便问一下:有没有“更简单”的方法来做我想做的事情?多谢各位

编辑:还有一个问题:我真的需要在通往服务器的途中在github部署应用程序吗

require "bundler/capistrano"

server "XXX.XX.XXX.XXX", :web, :app, :db, primary: true
这只是通知Capistrano您要访问哪个服务器。目前,由于您只有一台服务器,它将充当web、应用程序和db服务器。web服务器是您的nginx+unicorn。appserver是您的rails应用程序,db是您的数据库服务器。primary true表示一旦扩展到更多数据库服务器,这就是您的主数据库服务器。这些被称为角色,您可以定义更多,但这些是所需的主要角色。您可以指定某些任务在某些角色上工作,而不是在其他角色上工作,但当您有多个服务器时,仅此而已

set :application, "applicationName"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
set :scm, :git 
set :repository,  "GIT REPO URL"
set :branch, "master"
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
这些只是为Capistrano设置了一些变量。您的应用程序名称、您将部署为的用户(不要使用root创建另一个用户)、您要部署的位置、运行命令时是否使用sudo(Ryan说,如果使用sudo,有时他会遇到权限错误)。然后将源代码管理系统命名为git/subversion,并提供指向存储库和要部署到的分支的链接。 如果我记得的话,接下来的两个选项是使用ssh密钥处理一些事情

因此,当谈到Capistrano如何工作时,请确切地考虑Rakefile和Rake任务是如何工作的

after "deploy", "deploy:cleanup"    
如果你在“XXX”、“YYY”之后看到这种语句,它只是在任务XXX完成后说doyyy。因此,在本例中,一旦任务部署完成,就执行部署:清理

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
此任务只是创建能够停止、启动和重新启动unicorn服务器的任务。因此,在部署站点以重新启动unicorn服务器之后,可以结合使用此任务

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}"
    run "mkdir -p #{shared_path}/config"
    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"
  task :symlink_config, roles: :app do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end
  after "deploy:finalize_update", "deploy:symlink_config"
所以这是一项看起来非常复杂的任务,但实际上做的并不多。因为在railscasts中,Ryan没有将其database.yml文件放入git(带有生产值),他只是创建了一个模板database.example.yml文件,该文件被放入apps/APPNAME/shared/文件夹中,您必须手动进行编辑。正在将这些共享配置添加到当前文件夹

在其他文件nginx.conf unicorn.rb和unicorn_init.sh中,您需要编辑路径以指向正确的路径

其他资源

还可以看看Railscapsts,他在那里详细介绍了他的食谱,并使它们更加干燥。Capistrano Wiki还有一个很棒的资源,叫做。此外,还介绍了这两个方面的更多内容。我还建议阅读,这本书帮助我澄清了一些其他的疑问


但最重要的是,如果你遇到错误,你不必担心,只要试着一次解决一个错误,祝你好运。

Ryan Bates有一个出色的Railscast,不幸的是,它不是免费的,但我强烈推荐它。他涵盖了你需要做的所有事情。谢谢你的建议,但实际上我看了关于vps/capistrano的Railscaps,但我仍然无法理解,因为有太多的设置,我不知道它们中哪些是相关的。好吧,让我们一点一点地分解一下,让它变得更简单。不要跳到最深处--首先运行第一个部署需要一些时间--我建议您跳过不同的暂存和生产服务器,从直接部署到生产服务器开始。一旦开始运行,您就可以开始修改以包含暂存。我将在下面的回答中解释Railscasts中使用的Capistrano脚本。用它作为你的模板非常非常提前谢谢你!非常感谢您的解释!这是能够部署的最低要求吗?我不认为这是最低要求,但这是最低要求。您还需要另外3个文件
unicorn.rb
unicorn\u init.sh
nginx.conf