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

就可以看到:

nginx unicorn 来运行rails_第1张图片

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

你可能感兴趣的:(nginx unicorn 来运行rails)