• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

Still_Walking

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

nginx unicorn 来运行rails

nginx,unicorn

一、安装nginx

  sudo apt-get install nginx

  安装完成后查看一下:nginx -v

  

  说明安装成功。

  ubuntu系统里的安装目录是在/etc/nginx/下,启动程序文件在/usr/sbin/nginx

二、新建项目

  rails new app --skip-bundle

  完成后修改Gemfile文件:vim Gemfile

  把source 修改成taobao或者ruby-china的源。

  在这个文件里加入:gem 'unicorn'

  然后运行:bundle install

  这样项目就新建完成了。

三、配置nginx

  修改nginx的配置文件

  在http里加入:

  # App Server        
        upstream app_server{
                server unix:/path/to/.unicorn.sock fail_timeout=0;
        }  

        server {
                listen 3008;
                server_name localhost;
                root /项目路径/public;
                location / {
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_set_header Host $http_host;
                        proxy_redirect off;
                        proxy_pass http://127.0.0.1:3001;
                }
        }

 

四、配置unicorn

  加入如下配置:

  worker_processes 4
  working_directory "/path/to/app/current"
  listen "/path/to/.unicorn.sock", :backlog => 64
  listen 3001, :tcp_nopush => true
  timeout 30
  pid "/path/to/app/shared/pids/unicorn.pid"
  preload_app true
  check_client_connection false
  run_once = true
  before_fork do |server, worker|
    defined?(ActiveRecord::Base) and
      ActiveRecord::Base.connection.disconnect!
    if run_once
      run_once = false # prevent from firing again
    end
  end

  after_fork do |server, worker|
    defined?(ActiveRecord::Base) and
      ActiveRecord::Base.establish_connection
  end

五、启动项目

  在项目下启动unicorn:

  bundle exec unicorn_rails -c ./config/unicorn.rb  -D

  启动nginx: sudo nginx, 如果 已经启动会报:

  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

  这时停掉它:sudo nginx -s stop,然后重新启动: sudo nginx

  启动完成后,在浏览器里输入:localhost:3008

  就可以看到:

  

  到这里,说明已经配置成功了。

  

 

  

posted on 2016-07-30 21:57  limanxian  阅读(822)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3