簡體   English   中英

Rails Capistrano部署

[英]Rails Capistrano Deployment

第一次,我試圖將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)

我已經在Google上搜索了很長時間,但是無法解決此問題。 由於我是一個新手,因此覺得學習創建Rails應用程序與了解有關部署它們的所有知識之間存在巨大差距(順便說一句:早期的ftp上傳發生了什么?),我真的希望有人知道如何解決此問題,並可以指導我朝着不太陡峭的學習曲線進行部署的方向。 順便說一句:是否有“更輕松”的方式來做我想做的事情? 非常感謝你!

編輯:另一個問題:我真的需要在到服務器的途中在github上部署應用程序嗎?

require "bundler/capistrano"

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

這只是通知Capistrano您要訪問哪個服務器。 目前,由於您只有一台服務器,它將充當Web,應用程序和數據庫服務器。 Web服務器是您的nginx +獨角獸。 應用服務器是您的Rails應用程序,數據庫是您的數據庫服務器。 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的源代碼管理系統,以及指向您的存儲庫和您要部署到的分支的鏈接。 如果我記得,接下來的2個選項是使用ssh鍵處理某些事情。

因此,在考慮Capistrano的工作方式時,請仔細考慮Rakefile和Rake任務的工作方式。

after "deploy", "deploy:cleanup"    

如果您在“ XXX”,“ YYY”之后看到這種說法,那就是說在完成任務XXX之后再執行YYY。 因此,在這種情況下,一旦任務部署完成,請執行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

該任務只是創建任務,以能夠停止啟動和重新啟動獨角獸服務器。 因此,在部署站點以重新啟動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"

因此,這是一個看起來很復雜的任務,但實際上並沒有做很多事情。 由於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個錯誤,祝您好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM