over 7 years ago

使用mina+puma来部署rails项目,在最开始的时候本来想使用Capistrano+unicorn,不过在看了一些ruby-china上的帖子说Cap+unicorn很复杂和占内存,所以就选了比较新且较为简单的组合。不过刚开始弄的时侯还是走了些弯路。

gem的添加

gem 'mina'
gem 'mina-puma', :require => false  # puma运行脚本
gem 'puma' # > 2.6

配置mina

初始化部署文件

mina init # 生成 config/deploy.rb

mina的配置文件

require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm'    # for rvm support. (http://rvm.io)
require 'mina/puma'

set :domain, 'domain.com'
set :deploy_to, '/path/www/project_name'
set :repository, ''
set :branch, 'master'

set :shared_paths, ['log'] # 这个网上很多都加了database.yml这类数据库文件路径,不过我实际用内容为空,就删了
set :stage, 'production'

task :environment do
  invoke :'rvm:use[ruby-2.0.0-p353@rails]'
end

task :setup => :environment do
  queue! %[mkdir -p "#{deploy_to}/shared/log"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]

  queue! %[mkdir -p "#{deploy_to}/shared/config"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]

  queue! %[mkdir -p "#{deploy_to}/shared/tmp/pids"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/pids"]

  queue! %[mkdir -p "#{deploy_to}/shared/tmp/sockets"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/sockets"]
end

desc "Deploys the current version to the server."
task :deploy => :environment do
  deploy do
    # Put things that will set up an empty directory into a fully set-up
    # instance of your project.
    invoke :'git:clone'
    invoke :'deploy:link_shared_paths'
    invoke :'bundle:install'
    invoke :'rake:db:migrate' # 如果是mongoid的话,可以注释掉
    invoke :'rails:assets_precompile'

    to :launch do
      queue "touch #{deploy_to}/tmp/restart.txt"
    end
  end
end

配置puma

puma.rb配置文件

#!/usr/bin/env puma

environment "production"
basedir = "/path/www/project_name"
daemonize true
threads 2,16

bind  "unix:///path/www/project_name/shared/tmp/sockets/puma.sock"
pidfile  "#{basedir}/current/tmp/puma/pid"
state_path "#{basedir}/current/tmp/puma/state"
preload_app!
activate_control_app

puma的运行脚本

https://github.com/puma/puma/tree/master/tools/jungle
具体安装看 upstart和init.d 的文件夹里的README.md

Nginx的配置可以参考unicorn+nginx的配置

upstream project_name_domain {
  server unix:/tmp/project_name.unicorn.sock;
}

server {
  listen       80;
  server_name  domain.com;
  root /path/www/project_name/public;

  client_max_body_size 30m;
  access_log  /data/logs/host.access.log; # 记得自己创建文件夹

  location ~ ^/assets/ {
    expires 1y;
    add_header Cache-Control public;
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }

  try_files $uri/index.html $uri @unicorn;

  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://project_name_domain;
  }

  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   html;
  }

}

部署完后运行puma

mina puma:phased_restart  # Restart puma (with zero-downtime)
mina puma:restart         # Restart puma
mina puma:start           # Start puma
mina puma:stop            # Stop puma

https://github.com/sandelius/mina-puma

← 团队协作 linux-top →
 
comments powered by Disqus