Rails Mina Unicorn 部署 Rails 应用, 遇到小问题,请教。

void_dawn · 2018年10月16日 · 最后由 void_dawn 回复于 2018年10月17日 · 1281 次阅读

Mina 部署 Rails 应用程序,服务器选择 unicorn

有过自己尝试 capistrano 成功的案例,但是还是想尝试下 mina,确实配置少,速度快,但是 unicorn 貌似有点问题

此处快速略过 mina 基础姿势

Gemfile 添加

  • gem 'mina'
  • gem 'mina-sidekiq',:require => false
  • gem 'mina-unicorn', :require => false

bundle 安装

执行mina init,生成 config/deploy.rb,我的配置如下:

require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm'    # for rvm support. (https://rvm.io)  
require 'mina/unicorn'
require "mina_sidekiq/tasks"

set :application_name, 'baller'
set :domain, 'xx.xx.xx.xx'
set :deploy_to, '/home/chenxi/xxx_app'
set :user, 'chenxi'
set :port, '22'
set :forward_agent, true


set :repository, '项目xxx.git'
set :branch, 'master'

set :sidekiq_pid, "#{fetch(:deploy_to)}/shared/tmp/pids/sidekiq.pid"
set :unicorn_pid, "#{fetch(:deploy_to)}/shared/tmp/pids/unicorn.pid"
set :unicorn_env, 'production'


set :rvm_use_path, '/usr/local/rvm/scripts/rvm'


task :remote_environment do
  invoke :'rvm:use', 'ruby-2.2.1@baller4116'
end

set :shared_dirs, fetch(:shared_dirs, []).push('public/assets', 'public/system', 'public/uploads')
set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml','config/config.yml','vendor/bundle')

task :setup do

  ['log', 'config', 'public/upload','public/system','tmp/pids', 'tmp/sockets'].each do |dir|
    command %{mkdir -p "#{fetch(:deploy_to)}/shared/#{dir}"}
    command %{chmod g+rx,u+rwx "#{fetch(:deploy_to)}/shared/#{dir}"}
  end

  ['config/database.yml', 'config/secrets.yml', 'config/config.yml','log/unicorn.log','log/unicorn_error.log'].each do |file|
    command %{touch "#{fetch(:deploy_to)}/shared/#{file}"}
    comment %{Be sure to edit 'shared/#{file}'.}
  end

end

desc "Deploys the current version to the server."
task :deploy  do
  # to :before_hook do
  #   # Put things to run locally before ssh
  # end
  deploy do
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rails:db_migrate'
    invoke :'rails:assets_precompile'
    invoke :'deploy:cleanup'

    on :launch do
      # sidekiq stop accepting new workers
      invoke :'sidekiq:quiet'
      invoke :'sidekiq:restart'
      invoke :'unicorn:restart'
    end
  end
end

执行mina setup 生成相关目录依赖等,此处不多说

执行mina deploy部署,此处也不多说哈,如我所预期,bundle, 静态资源文件编译,migratie 等等都正常通过了,Unicorn 也成功启动了😆 😆 😆,以下是 unicorn.rb

app_path = File.expand_path( File.join(File.dirname(__FILE__), '..', '..'))
worker_processes   1
timeout            180
listen 3000, tcp_nopush: false
listen             "#{app_path}/shared/tmp/sockets/unicorn.sock"
pid                "#{app_path}/shared/tmp/pids/unicorn.pid"
stderr_path        "#{app_path}/shared/log/unicorn_error.log"
stdout_path        "#{app_path}/shared/log/unicorn.log"

before_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
  end

  old_pid = "#{server.config[:pid]}.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end
end

after_fork do |server, worker|
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  end
end

before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "#{app_path}/current/Gemfile"
end

😆 😆 😆下面是 Mina deploy 的 console

-----> Creating a temporary build path
-----> Using RVM environment "ruby-2.2.1@baller4116"
       Using /usr/local/rvm/gems/ruby-2.2.1 with gemset baller4116
-----> Fetching new git commits
-----> Using git branch 'master'
       Initialized empty Git repository in /home/chenxi/baller_app/tmp/build-153967253627970/.git/
-----> Using this git commit
       chenxi (57820a4):
       > unicorn updated
-----> Symlinking shared paths
-----> Installing gem dependencies using Bundler
       Using rake 12.3.1
       Using concurrent-ruby 1.0.5
       Using i18n 0.9.5
       Using json 1.8.6
       Using minitest 5.11.3
       Using thread_safe 0.3.6
       Using tzinfo 1.2.5
       Using activesupport 4.1.16
       Using builder 3.2.3
       Using erubis 2.7.0
       Using actionview 4.1.16
       Using rack 1.5.5
       Using rack-test 0.6.3
       Using actionpack 4.1.16
       Using mini_mime 1.0.1
       Using mail 2.7.0
       Using actionmailer 4.1.16
       Using activemodel 4.1.16
       Using arel 5.0.1.20140414130214
       Using activerecord 4.1.16
       Using multi_json 1.13.1
       Using thor 0.20.0
       Using railties 4.1.16
       Using activerecord-session_store 1.1.1
       Using public_suffix 3.0.3
       Using addressable 2.5.2
       Using execjs 2.7.0
       Using autoprefixer-rails 9.1.4
       Using bootstrap-daterangepicker-rails 0.1.8
       Using rb-fsevent 0.10.3
       Using ffi 1.9.25
       Using rb-inotify 0.9.10
       Using sass-listen 4.0.0
       Using sass 3.5.7
       Using bootstrap-sass 3.3.5.1
       Using bootstrap-switch-rails 3.0.2
       Using momentjs-rails 2.20.1
       Using bootstrap3-datetimepicker-rails 4.17.47
       Using mime-types-data 3.2018.0812
       Using mime-types 3.2.2
       Using carrierwave 1.2.3
       Using chronic 0.10.2
       Using coffee-script-source 1.12.2
       Using coffee-script 2.4.1
       Using coffee-rails 4.1.1
       Using commonjs 0.2.7
       Using connection_pool 2.2.2
       Using unf_ext 0.0.7.5
       Using unf 0.1.4
       Using domain_name 0.5.20180417
       Using font-awesome-rails 4.3.0.0
       Using grease 0.3.1
       Using highcharts-rails 6.0.3
       Using http-cookie 1.0.3
       Using jbuilder 2.6.4
       Using mixlib-shellout 2.4.0
       Using mini_portile2 2.1.0
       Using nokogiri 1.6.8.1
       Using socksify 1.7.1
       Using unicode-display_width 1.4.0
       Using terminal-table 1.8.0
       Using jenkins_api_client 1.4.3
       Using jquery-rails 3.1.5
       Using jquery-ui-rails 4.2.1
       Using kaminari-core 1.1.1
       Using kaminari-actionview 1.1.1
       Using kaminari-activerecord 1.1.1
       Using kaminari 1.1.1
       Using kgio 2.11.2
       Using less 2.6.0
       Using sprockets 3.7.2
       Using tilt 2.0.8
       Using less-rails 3.0.0
       Using net-http-digest_auth 1.4.1
       Using net-http-persistent 3.0.0
       Using ntlm-http 0.1.1
       Using webrobots 0.1.2
       Using mechanize 2.7.6
       Using mysql2 0.3.21
       Using net-ssh 4.2.0
       Using net-scp 1.2.1
       Using pdfkit 0.8.2
       Using protected_attributes 1.1.4
       Using quiet_assets 1.1.0
       Using rack-cors 1.0.2
       Using rack-protection 2.0.3
       Using bundler 1.8.4
       Using sprockets-rails 2.3.3
       Using rails 4.1.16
       Using raindrops 0.19.0
       Using rdoc 4.3.0
       Using redis 3.3.5
       Using redis-store 1.5.0
       Using redis-rack 2.0.4
       Using redis-actionpack 5.0.2
       Using redis-activesupport 5.0.7
       Using redis-namespace 1.6.0
       Using redis-rails 5.0.2
       Using require_all 2.0.0
       Using rmmseg-cpp-new 0.3.1
       Using rubyzip 1.2.2
       Using roo 2.7.1
       Using ruby-ntlm 0.0.4
       Using ruby-pinyin 0.5.0
       Using rubycas-client 2.3.10.tj.2
       Using sass-rails 5.0.7
       Using sdoc 0.4.2
       Using select2-rails 4.0.3
       Using sidekiq 4.2.10
       Using soft_deletion 1.3.1
       Using sshkit 1.12.0
       Using subexec 0.2.3
       Using turbolinks-source 5.2.0
       Using turbolinks 5.2.0
       Using twitter-bootstrap-rails 3.2.2
       Using uglifier 4.1.19
       Using unicorn 5.4.1
       Using webhdfs 0.8.0
       Using whenever 0.10.0
       Bundle complete! 54 Gemfile dependencies, 119 gems now installed.
       Gems in the groups development and test were not installed.
       Bundled gems are installed into ./vendor/bundle.
-----> DB migrations unchanged; skipping DB migration
-----> Skipping asset precompilation
-----> Cleaning up old releases (keeping 5)
       /home/chenxi/baller_app/tmp/build-153967253627970
-----> Deploy finished
-----> Building
-----> Moving build to /home/chenxi/baller_app/releases/13
-----> Build finished
-----> Launching
-----> Updating the /home/chenxi/baller_app/current symlink
-----> Using RVM environment "ruby-2.2.1@baller4116"
       Using /usr/local/rvm/gems/ruby-2.2.1 with gemset baller4116
-----> Quiet sidekiq (stop accepting new work)
       /home/chenxi/baller_app/current
-----> Stop sidekiq
       Sidekiq shut down gracefully.
       /home/chenxi/baller_app/current
-----> Start sidekiq
       /home/chenxi/baller_app/current
-----> Starting Unicorn...
-----> Done. Deployed version 13
       Connection to xxx.xxx.xxx.xxx closed.

       Elapsed time: 24.54 seconds

查看 unicorn 确实已经起来了,如下:

unicorn 进程没问题

unicorn 配置 listen 的 3000 没问题

unicorn_error.log 显示无异常

但是!当我访问 xx.xx.xx.xxx:3000 一直呈现该页无法显示,查看 unicorn_error.log 显示无异常😰😰😰

肯定是一个很小的问题...被我忽略了,哈哈哈,请教大神围观分析!!😢😢😢

服务器上 curl 一下本地端口能不能访问?云服务有没有安全组设置?

Rei 回复

就是安全组...大意了😅 3Q

需要 登录 后方可回复, 如果你还没有账号请 注册新账号