【问题标题】:Rails Capistrano DeploymentRails Capistrano 部署
【发布时间】:2013-08-06 10:17:07
【问题描述】:

我第一次尝试不是将 Rails 应用程序部署到 Heroku,而是部署到 DigitalOcean,主要是因为价格。我对 Capistrano 以及 VPS 部署完全陌生,我完全迷失了。我创建了 1-click-Rails-droplet 并按照本教程进行操作: http://guides.beanstalkapp.com/deployments/deploy-with-capistrano.html

这是我的 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 应用程序和了解有关部署它们的所有事情之间存在巨大差距(顺便说一句:早期的 ftp 上传发生了什么?),我真的希望有人知道如何解决这个问题,并可以引导我走向一个不那么陡峭的部署学习曲线。顺便说一句:有没有“更简单”的方法来做我想做的事情?非常感谢!

编辑:还有一个问题:我真的需要在 github 上部署应用程序以到达服务器吗?

【问题讨论】:

  • Ryan Bates 在deploying to a vps 上有一个出色的 Railscast,不幸的是它不是免费的,但我强烈推荐它。他涵盖了您需要做的所有事情。
  • 感谢您的建议,但实际上我观看了有关 vps / capistrano 的 railscasts 但我仍然无法理解,因为设置太多而且我不知道其中哪些是相关的。
  • 好的,让我们一点一点地分解它以使其更容易。不要跳入深渊,让您的第一个部署运行需要一些时间 首先,我建议您跳过执行不同的登台和生产服务器,并从直接部署到生产开始。一旦你开始运行,你就可以开始修改以包括登台。我将在下面的答案中解释 Railscasts 中使用的 Capistrano 脚本。用它作为你的模板
  • 提前非常非常感谢您!

标签: ruby-on-rails deployment capistrano


【解决方案1】:
require "bundler/capistrano"

server "XXX.XX.XXX.XXX", :web, :app, :db, primary: true

这只是通知 Capistrano 您要访问哪个服务器。现在,由于您只有一台服务器,它将充当 Web、应用程序和数据库服务器。 Web 服务器是您的 nginx + 独角兽。 App server 是您的 Rails 应用程序,而 db 是您的数据库服务器。 primary true 表示一旦您扩展到更多数据库服务器,这就是您的主数据库服务器。这些称为角色,您可以定义更多,但这些是必需的主要角色。您可以指定一些任务来处理某些角色而不是其他角色,但是当您拥有超过 1 个服务器时,这就是全部。

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 的源代码管理系统,以及指向您的存储库和要部署到的分支的链接。 如果我记得接下来的 2 个选项是用 ssh 键处理一些事情。

因此,在考虑 Capistrano 的工作方式时,请考虑 Rakefile 和 Rake 任务的工作方式。

after "deploy", "deploy:cleanup"    

如果你在“XXX”、“YYY”之后看到这种说法,那只是说在任务XXX完成后做YYY。因此,在这种情况下,一旦任务部署完成,请执行部署:清理。

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}"
    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/ 你必须手动去和编辑它。这些共享配置正在symlinked 到当前文件夹。

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

其他资源

还可以查看 Railscasts Capistrano Recipes,他在其中扩展了他的食谱细节并使它们更加干燥。 Capistrano Wiki 还有一个很棒的资源,叫做from the beginning。此外Nginx & Unicorn 涵盖了更多关于这两件事的内容。我还建议阅读Github's blog post on UnicornNginx primer,它们可以帮助我消除我的其他一些疑问。

但最重要的是,如果您遇到错误,您不必担心,只需尝试一次将其分解为 1 个错误,祝您好运。

【讨论】:

  • 非常感谢您的解释!这是能够部署的最低要求吗?
  • 我不认为这是最低限度,但它是最低限度的。您还需要 3 个其他文件 unicorn.rb unicorn_init.shnginx.conf
猜你喜欢
  • 1970-01-01
  • 2015-03-07
  • 1970-01-01
  • 2011-09-05
  • 2016-10-08
  • 2015-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多