记录一个新手用 unicorn 和 nginx 部署 rails 应用的注意点。
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:~#
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路径*
}
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
访问正常了
先修改 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
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