0

我第一次尝试不是将 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 上部署应用程序到服务器吗?

4

1 回答 1

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/ 你必须手动去和编辑它。这些共享配置被符号链接到当前文件夹。

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

其他资源

还可以查看 Railscasts Capistrano 食谱,其中他扩展了他的食谱的细节并使它们更加干燥。Capistrano Wiki 也有一个很好的资源,从一开始就叫做。此外,Nginx 和 Unicorn涵盖了更多关于这两件事的内容。我还建议阅读Github 关于 Unicorn 的博客文章Nginx 入门,它们帮助我消除了我的其他一些疑问。

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

于 2013-08-06T11:05:57.970 回答