在ubuntu server 12.04上部署Rails程序

这篇文章主要记录的是部署nginx+unicorn服务器,同时兼顾使用自动化部署工具Capistrano合理发布新版本。

主要参考的文章是:http://ruby-china.org/topics/12033

在这里顺带提一下,如何将修改ubuntu的源:建议使用ubuntu的cn源,直接用vi打开/etc/apt/sources.list,输入如下命令:

g/us.ar/s//cn.ar/g

1.安装nginx

1)增加nginx的自主源

deb http://nginx.org/packages/mainline/ubuntu/ precise nginx
deb-src http://nginx.org/packages/mainline/ubuntu/ precise nginx

2)安装nginx

sudo apt-get update
sudo apt-get install nginx

3)启动、重启、停止nginx

nginx
nginx -s reload
nginx -s stop

2.配置nginx

1)配置nginx的多核亲缘性

查看有几个逻辑CPU:

cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 

比如有8个CPU:

worker_processes  8;  
worker_cpu_affinity 10000000 01000000 00100000 00010000 00001000 00000100 00000010 00000001;  

2)配置www跳转到不带www的URL

server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

3)配置upstream

在unicorn的配置:

listen "/tmp/unicorn.ruby-china.sock"

 那么,在nginx中配置

  upstream ruby_china_backend {
    server unix:/tmp/unicorn.ruby-china.sock fail_timeout=0;
  }

    location / {
      proxy_redirect     off;
      proxy_set_header   Host $host;
      proxy_set_header   X-Forwarded-Host $host;
      proxy_set_header   X-Forwarded-Server $host;
      proxy_set_header   X-Real-IP        $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_buffering    on;

    if (-f $request_filename) {
      break;
    }

    if (!-f $request_filename) {
      proxy_pass http://ruby_china_backend;
      break;
    }

    }

 这样就搭配nginx+unicorn,如果访问的是本身存在的文件,则直接访问public目录下的文件,否则使用unicorn访问。

启动

bundle exec unicorn -E production -c config/unicorn.rb

至于部署,暂时我们只有一台服务器,不使用自动化部署工具,准备采用git的push-pull进行代码部署,然后关闭/重启unicorn服务器:

kill -QUIT `cat #{unicorn_pid}`
kill -s USR2 `cat #{unicorn_pid}`

posted on 2013-07-02 00:50  唐明星  阅读(257)  评论(0编辑  收藏  举报