Mina 部署 Rails (rbenv + nginx + mysql + puma)

nginx 和 mysql 环境需要事先搭好

  1. 服务器上创建用户,修改登录shell为 bash chsh deploy -s /bin/bash

  2. 添加操作机的 ssh key 到服务器上实现免密码连接

  3. 安装 rbenv (请查看官方文档)

    git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
    echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
    exec $SHELL
    git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
    rbenv install 2.2.3

因为 cdn 被墙,所以需要修改下载地址

find . | grep 2.2.3
vi ./.rbenv/plugins/ruby-build/share/ruby-build/2.2.3

可以顺便禁掉 rdoc

echo "gem: --no-ri --no-rdoc" > ~/.gemrc

ruby-build 依赖

apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev

还要安装 bundler

gem install bundler
  1. 如果部署在非用户目录,创建目录并设置权限(如 /var/www/
    执行 mina init 并修改配置文件

  2. mina setup,然后编辑生产环境的配置文件 (如 database.yml, secrt.yml, puma.rb 等)
    执行 rake secret 生成密钥

  3. 添加服务器的 ssh key 到 git 仓库获取访问权限

  4. 数据库手动创建

    CREATE DATABASE rails_app DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

修改字符集

ALTER DATABASE report
   CHARACTER SET utf8
   COLLATE utf8_general_ci
  1. 配置 nginx 和 puma

  2. mina deploy

Puma

# Gemfile
gem 'mina-puma', require: false

# deploy.rb
require 'mina/puma'

必须 puma.rb 中添加下面的配置才能够通过 mina 来重启 puma

state_path "#{application_path}/shared/tmp/sockets/puma.state"
activate_control_app "unix://#{application_path}/shared/tmp/sockets/pumactl.sock"

参考

  • http://ruby-journal.com/digesting-pumactl/
  • https://github.com/sandelius/mina-puma/issues/11

自定义任务

通过依赖 environment 加载 ruby 和 bundle

desc "rake db:seed"
task :seed => :environment do
    queue "cd #{deploy_to}/#{current_path}"
    queue "bundle exec rake db:seed RAILS_ENV=production"
end

你可能感兴趣的:(Mina 部署 Rails (rbenv + nginx + mysql + puma))