部署 UNICORN 和 NGINX 手动部署 rails 应用笔记

caiqinghua · June 11, 2015 · Last by hbin replied at June 11, 2015 · 3426 hits

内容概要

记录一个新手用 unicorn 和 nginx 部署 rails 应用的注意点。

一、unicorn 配置

root@iZ94ra5tl2iZ:~# cat /home/deploy/weishop_origin/config/unicorn/origin.rb 
app_path = File.expand_path( File.join(File.dirname(__FILE__), '..', '..'))
worker_processes   1
timeout            180
listen             '/tmp/unicorn_origin.sock' #**unicorn配置中要用到这个配置**
pid                "#{app_path}/tmp/pids/unicorn.pid"
stderr_path        "log/unicorn.log"
stdout_path        "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| # 修正无缝重启unicorn后更新的Gem未生效的问题,原因是config/boot.rb会优先从ENV中获取BUNDLE_GEMFILE,而无缝重启时ENV['BUNDLE_GEMFILE']的值并未被清除,仍指向旧目录的Gemfile
  ENV["BUNDLE_GEMFILE"] = "#{app_path}/Gemfile"
end
root@iZ94ra5tl2iZ:~# 

二、nginx 配置

root@iZ94ra5tl2iZ:~# cat /etc/nginx/conf.d/
origin.conf      production.conf  staging.conf     
root@iZ94ra5tl2iZ:~# cat /etc/nginx/conf.d/origin.conf 
server {
  listen 80;
  server_name xxx.com;
  root /home/deploy/weishop/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @weishop;
  location @weishop {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://weishop;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 20M;
  keepalive_timeout 10;
}

upstream weishop {
    server unix:/tmp/unicorn_production.sock fail_timeout=0;    #*来自unicorn配置的socket路径*
}

三、注意点

  1. @weishop要不和已有的配置冲突
  2. unicorn 启动命令 unicorn -c config/unicorn/origin.rb -D
  3. rails s -b 0.0.0.0 运行正常,但是用 unicorn+nginx 运行,访问没有 css 效果,估计是 asset 没有安装
  4. 重新执行如下命令,访问正常了。
bundle install --without development:test --path "./vendor/bundle" --deployment
RAILS_ENV="production" bundle exec rake db:migrate
RAILS_ENV="production" bundle exec rake assets:precompile RAILS_GROUPS=assets

访问遇到如下问题 Missing secret_token and secret_key_base for 'production' environment, set these values in config/secrets.yml 安装 figaro 在 Gemfile 中增加下行 gem figaro 在/config/application.yml 中加入 secret_key_base

执行 bundle install 再次执行 bundle install --without development:test --path "./vendor/bundle" --deployment kill unicorn 进程 启动 unicorn 进程 RAILS_ENV="production" unicorn -c /home/deploy/weishop_origin/config/unicorn/origin.rb -E deployment -D

访问正常了

更改 sqlite 数据库为 mysql 数据库

先修改 config/database.yml

RAILS_ENV="production" rake db:create
RAILS_ENV="production" rake db:migrate
RAILS_ENV="production" rake db:seed
RAILS_ENV="production" rake spree_sample:load

更改 logo

Overriding images

Finally, images can be replaced by substituting the required file into the same path within your application or extension as the file you would like to replace.

For example, to replace the Spree logo you would simply copy your logo to: your_app/vendor/assets/images/logo/spree_50.png.

重新执行编译资源 RAILS_ENV="production" bundle exec rake assets:precompile RAILS_GROUPS=assets

*内存不够 deploy@iZ94ra5tl2iZ:~/weishop_origin$ bundle install --without development:test --path "./vendor/bundle" --deployment Fetching source index from http://ruby.taobao.org/ /home/deploy/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/bundler-1.9.9/lib/bundler/source/git/git_proxy.rb:118:in ``': Cannot allocate memory - git (Errno::ENOMEM)

kill 几个 unicorn 进程即可解决问题

还有很多知识待理解

bundle update --source spree_wechat_pay

不建议把 Nginx 配置文件放在 /etc/nginx/conf.d/ 文件夹下,应该放在 /etc/nginx/site-available/ 中,然后 ln 到 /etc/nginx/site-enable/ 里。

You need to Sign in before reply, if you don't have an account, please Sign up first.