简体   繁体   English

Rails Capistrano部署

[英]Rails Capistrano Deployment

for the first time I'm trying to deploy a rails app not to Heroku but to DigitalOcean, mainly because of the price. 第一次,我试图将Rails应用程序不是部署到Heroku而是部署到DigitalOcean,主要是因为价格。 I'm completely new to Capistrano as well as VPS deployment and I'm totally lost. 我对Capistrano和VPS部署都是全新的,我完全迷失了。 I've created the 1-click-Rails-droplet and followed this tutorial: http://guides.beanstalkapp.com/deployments/deploy-with-capistrano.html 我创建了1-click-Rails-droplet,并遵循了本教程: http : //guides.beanstalkapp.com/deployments/deploy-with-capistrano.html

This are the settings in my deploy.rb file: 这是我的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" 

The staging.rb file: staging.rb文件:

server "xx.xxx.x.xxx", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/xx.xxx.x.xxx_staging"

And the production.rb file 和production.rb文件

server "xx.xxx.x.xxx", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/xx.xxx.x.xxx"

Now, when I run 现在,当我跑步

cap deploy:check

in the terminal I get the following: 在终端中,我得到以下信息:

[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)

I've googled for a long time now but can't fix this. 我已经在Google上搜索了很长时间,但是无法解决此问题。 As I'm totally new to this and feel like there is a huge gap between learning to create a rails app and knowing all the things about deploying them (btw: what happened to ftp upload from earlier days?), I really hope someone knows how to fix this and could guide me into a direction to a not so steep learning curve for deployment. 由于我是一个新手,因此觉得学习创建Rails应用程序与了解有关部署它们的所有知识之间存在巨大差距(顺便说一句:早期的ftp上传发生了什么?),我真的希望有人知道如何解决此问题,并可以指导我朝着不太陡峭的学习曲线进行部署的方向。 By the way: are there "easier" ways to do what I'm trying to do? 顺便说一句:是否有“更轻松”的方式来做我想做的事情? Thank you very much! 非常感谢你!

Edit: One more question: Do I really need to deploy the app at github on the way to the server? 编辑:另一个问题:我真的需要在到服务器的途中在github上部署应用程序吗?

require "bundler/capistrano"

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

This is just informing Capistrano which server you want to be accessing. 这只是通知Capistrano您要访问哪个服务器。 For now since you have only one server it will be acting as a web, app and db server. 目前,由于您只有一台服务器,它将充当Web,应用程序和数据库服务器。 The web server is your nginx + unicorn. Web服务器是您的nginx +独角兽。 App server is your rails application and db is your database server. 应用服务器是您的Rails应用程序,数据库是您的数据库服务器。 The primary true indicates that this is your primary database server once you expand to more database servers. primary true表示一旦扩展到更多数据库服务器,这就是您的主数据库服务器。 These are called roles you can define more but these are the main ones that are required. 这些称为角色,您可以定义更多,但这些是必需的。 You can specify some tasks to work on some roles and not others, but that's all when you have more than 1 server. 您可以指定要在某些角色上执行的某些任务,而不是其他角色,但是只有一台以上的服务器时,就是这些。

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

These just set up some variables for Capistrano to use. 这些只是设置了一些变量供Capistrano使用。 Your application name, the user that you will be deploying as (don't use root create another user), where you want to deploy, whether to use sudo when running commands (Ryan says sometimes he encounters permission errors if sudo is used). 您的应用程序名称,您将要部署为的用户(不要使用root创建另一个用户),您要在其中部署的用户,在运行命令时是否使用sudo(Ryan说,如果使用sudo,有时他会遇到权限错误)。 Then the source code management system as git / subversion, and a link to your repository and the branch with which you want to deploy to. 然后是git / subversion的源代码管理系统,以及指向您的存储库和您要部署到的分支的链接。 The next 2 options If I recall were to handle some things with the ssh keys. 如果我记得,接下来的2个选项是使用ssh键处理某些事情。

So when it comes to thinking of how Capistrano works think of exactly how a Rakefile and Rake tasks work. 因此,在考虑Capistrano的工作方式时,请仔细考虑Rakefile和Rake任务的工作方式。

after "deploy", "deploy:cleanup"    

if you see this kind of statement after "XXX", "YYY", it's just saying after task XXX is done do YYY. 如果您在“ XXX”,“ YYY”之后看到这种说法,那就是说在完成任务XXX之后再执行YYY。 So in this case once task deploy is done, do deploy:cleanup. 因此,在这种情况下,一旦任务部署完成,请执行deploy:cleanup。

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

This task is just creating tasks to be able to stop start and restart the unicorn server. 该任务只是创建任务,以能够停止启动和重新启动独角兽服务器。 Therefore this task can be used in conjunction later as soon as the site is deployed to restart the unicorn server. 因此,在部署站点以重新启动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"

So this is a very complex looking task, but it's actually not doing much. 因此,这是一个看起来很复杂的任务,但实际上并没有做很多事情。 Since in the railscasts Ryan was not putting his database.yml file in git (with production values), he just created a template database.example.yml file which is being put in folder apps/APPNAME/shared/ you have to manually go and edit it. 由于Ryan并没有将他的database.yml文件放入git(带有生产值)中,因此他只是创建了一个模板database.example.yml文件,并将其放在文件夹apps / APPNAME / shared /中,您必须手动执行以下操作:编辑它。 These shared configurations are being symlinked to the current folder. 这些共享的配置被符号链接到当前文件夹。

In the other files nginx.conf unicorn.rb and unicorn_init.sh you'd want to edit the paths to point to correct path. 在其他文件nginx.conf unicorn.rb和unicorn_init.sh中,您需要编辑路径以指向正确的路径。

Additional Resources 其他资源

Also take a look at the Railscasts Capistrano Recipes where he expands on the details of his recipes and makes them more DRY. 还可以看看Railscasts Capistrano食谱 ,他在其中详细介绍了食谱的细节,并使它们更加干燥。 The Capistrano Wiki also has a great resource called from the beginning . Capistrano Wiki也从一开始就拥有大量资源。 In addition Nginx & Unicorn covers more on those 2 things. 此外, Nginx和Unicorn还介绍了这两件事。 I'd also suggest reading Github's blog post on Unicorn and this Nginx primer they were help me clear up some other doubts I had. 我也建议阅读Github在Unicorn上的博客文章,并阅读这篇Nginx入门文章,它们可以帮助我消除我的其他疑问。

But most of all you don't worry if you get errors just try breaking it down 1 error at a time and good luck. 但是,最重要的是,即使遇到错误,您也不必担心,只需一次将其分解为1个错误,祝您好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM