nginx unicorn 来运行rails

一、安装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

  就可以看到:

  

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

  

  

时间: 2024-08-07 00:13:16

nginx unicorn 来运行rails的相关文章

Capistrano+Nginx+Unicorn 自动部署Rails

配置你的服务器 你需要在你的服务器上安装Ruby的环境,你可以使用RVM或者是rbenv. 上传到github 这步需要将你的应用上传到github,在你的github上创建新的repository,然后在你本机代码位置执行下面的命令,初始化git仓库. git init git add . git commit -m"<message>" git remote add origin [email protected]:<username>/<git re

nginx + unicorn 部署rails项目

nginx + unicorn  部署 rails 项目,顾名思义要先安装nginx和unicorn 一 安装 安装nginx: sudo apt-get install nginx 安装unicorn: gem install unicorn 二 配置 1 在你项目的config文件夹下添加一个unicorn.rb文件,添加代码(样列:http://unicorn.bogomips.org/examples/unicorn.conf.rb) 我的如下: 1 module Rails 2 cla

使用Nginx + unicorn搭建ruby on rails的生产环境

有三台机器,操作系统都是CentOS 6.3 64位:其中172.16.9.100,安装Nginx服务器:另外的两台172.16.9.101和172.16.9.102安装unicorn,作为RoR的应用服务器.在这里先保证ruby及rails已经在101和102两台机器上配置好,这是前提.如何安装Nginx及RoR的环境就不在这里说了,很多文章都介绍得很详细. 这里假设项目在/var/www/demo_project文件夹中 在101上使用 gem install unicorn 命令安装uni

nginx指定路径运行

在日常运维过程中,偶尔出现一台机器上运行多个nginx,一般运行多个nginx需要重新编译,配置路径,但通过摸索,发现可以通过运行时指定参数可以实现不需要重新编译,能正常运行. 具体命令如下: 启动:/usr/local/nginx8094/sbin/nginx -p /usr/local/nginx8094/ 停止:/usr/local/nginx8094/sbin/nginx -p /usr/local/nginx8094/ -s stop 如启动报错,注意检查nginx.conf配置,不要

Nginx FastCGI的运行原理

一.FastCGI 1.介绍 CGI全称通用网关接口 Commmon Gateway Interface 用于HTTP服务上的程序服务通信交流的一种工具,CGI程序须运行在网络服务器上. 传统CGI接口方式性能较差,由于每次HTTP服务器遇到动态程序需要重启解析器来执行解析,然后结果被返回给HTTP服务器.这在处理高并发时,几乎是不可能的,因此诞生了FastCGI.另外传统的CGI接口方式安全性也很差 一个可伸缩地.高速地在HTTP服务器和动态脚本语言间通信的接口 接口在linux下是socke

十五大原理之一--Nginx FastCGI的运行原理

一.FastCGI 1.介绍 CGI全称通用网关接口 Commmon Gateway Interface 用于HTTP服务上的程序服务通信交流的一种工具,CGI程序须运行在网络服务器上. 传统CGI接口方式性能较差,由于每次HTTP服务器遇到动态程序需要重启解析器来执行解析,然后结果被返回给HTTP服务器.这在处理高并发时,几乎是不可能的,因此诞生了FastCGI.另外传统的CGI接口方式安全性也很差 一个可伸缩地.高速地在HTTP服务器和动态脚本语言间通信的接口 接口在linux下是socke

PHP+FastCGI+Nginx配置PHP运行环境方法

PHP+FastCGI+Nginx配置PHP运行环境 Nginx不支持对外部程序的调用,所以必须通过FastCGI接口实现对外部程序的调用从而实现对客户端动态页面请求的处理. CGI的英文全称为Common Gateway Interface(公共网关接口),是Nginx和动态脚本程序的桥梁,Nginx通过FastCGI接口将动态请求发送给FastCGI,FastCGI中的Wrapper进程生成一个线程,将请求交给脚本解释程序执行,然后通过原来的socket将解释执行后的结果原路返回给Nginx

Nginx安装及运行服务

Nginx服务优点:稳定性和低系统资源消耗,以及对HTTP并发连接的高处理能力(单台物理服务器可支持30000~50000个并发请求)一.编译安装Nginx1.安装支持软件Nginx的配置运行需要pcre-devel.zlib-devel.gcc.gcc-c++等软件包的支持,因此应预先安装这些软件的开发包.若服务器中已装有httpd等其他Web服务软件,应采取措施如修改端口.停用或卸载等以避免冲突.2.创建运行用户.组并且解压Nginx安装包到/opt/目录下Nginx服务默认以nobody身

Nginx安装与运行

目录 Nginx安装与运行 安装Nginx 运行 注意事项 Nginx安装与运行 安装Nginx 在Nginx官网下载对应的nginx包(推荐使用稳定版[Stable version]) 上传nginx包到Linux系统 安装依赖环境 安装gcc环境 yum install gcc-c++ 安装PCRE库,用于解析正则表达式 yum install - y pcre pcre-devel 安装zlib压缩和解压缩依赖 yum install -y zlib zlib-devel 安装SSL安全的