mac中搭建nginx + unicorn

来源:互联网 发布:锻造模拟软件forge 编辑:程序博客网 时间:2024/06/05 20:27

mac10.9中安装passenger始终都不成功,从网上找了很多资料,有很多人建议nginx + unicorn去运行rails的项目

理由:

nginx 是一个非常高效的 http server, 而 unicorn 是一个非常高效的 app server。高效开发的 web 框架 rails 搭载上 这两个利器,可谓是如虎添翼。

摘至 http://blog.zlxstar.me/blog/2013/02/01/zai-rails-xiang-mu-zhong-ji-cheng-nginx-he-unicorn/

开始搭建:

1 首先参照下面的网址

http://wiki.summercode.com/setting_up_unicorn_with_nginx

Rails on Unicorns

Ready power your app by rainbows?

We’re going to set up Nginx in front of Unicorn.

Nginx

Start by installing Nginx via your favorite package manager. Afterwards we need to configure it for Unicorn, we’re gonna grab the nginx.conf example configuration shipped with Unicorn, the Nginx configuration file is usually located at/etc/nginx/nginx.conf, so place it there, and tweak it to your likings, read the comments — they’re quite good.

In nginx.conf you may have stumbled upon this line:

user nobody nogroup; # for systems with a "nogroup"

While this works, it’s generally adviced to run as a seperate user for security reasons and increased control. Let’s create an Nginx user, and a web group:

$ sudo useradd -s /sbin/nologin -r nginx$ sudo usermod -a -G nginx web

Configure your static path in nginx.conf to /var/www, and give permissions to that folder to the web group:

$ sudo mkdir /var/www $ sudo chgrp -R web /var/www # set /var/www owner group to "web"$ sudo chmod -R 755 /var/www # group write permission

Then add yourself to the web group so you can modify the contents of /var/www:

$ sudo usermod -a -G web USERNAME

Unicorn

Time for flying rainbow horses. Start by installing the Unicorn gem:

$ gem install unicorn

You should now have Unicorn installed: unicorn (for non-Rails rack applications) and unicorn_rails (for Rails applications version >= 1.2) should be in your path.

Time to take it for a spin! (You may wish to re-login with su - USERNAME if you haven’t already, this ensures your permission tokens are set, otherwise you will not have write permission to /var/www)

$ cd /var/www$ rails new unicorn

There we go, we now have our Unicorn Rails test app in /var/www! Let’s fetch some Unicorn config and start the madness. We’re going for the unicorn.conf example that comes with the Unicorn source:

$ curl -o config/unicorn.rb https://raw.github.com/defunkt/unicorn/master/examples/unicorn.conf.rb

You might want to tweak a few things:

APP_PATH = "/var/www/unicorn/"working_directory APP_PATHstdeer_path APP_PATH + "/log/unicorn.stderr.log"stdout_path APP_PATH + "/log/unicorn.stderr.log"pid APP_PATH + "/tmp/pid/unicorn.pid"

Our Unicorn is ready!

Rainbow magic

Start the Nginx deamon, how this is done depends on your OS. And then start Unicorn:

$ unicorn_rails -c /var/www/unicorn/config/unicorn.rb -D

-D deamonizes it. -c should be pretty obvious; it specifies the configuration. In production you will probably want to pass -E production as well to run the app in the production environment.

eg.  
$ unicorn_rails -c /var/www/unicorn/config/unicorn.rb -E production

2 正常的话,不会出什么问题

但是我在执行最后一步的时候出现的下面的问题

master failed to start, check stderr log for details

解决办法:http://stackoverflow.com/questions/17249385/cant-start-unicorn-master-failed-to-start-check-stderr-log-for-details


The socket is the "file" that nginx and unicorn use as a channel for all communication between them. Where have you defined it? In our unicorn configs, we usually have a line like this:

listen APP_PATH + "/tmp/pid/.unicorn.sock

Then, in your nginx.conf, you need to tell nginx about this socket, e.g.:

upstream unicorn {  server unix:/var/www/demo/tmp/pid/.unicorn.sock fail_timeout=0;}location / {  root /var/www/demo/current/public ;  try_files $uri @unicorns;}location @unicorns {  proxy_pass http://unicorn;}

In this config file, the first section defines how nginx can reach unicorn. The second one actually routes requests to an abstract location "@unicorns" which, in turn, is defined in the last section. This way you can reuse the @unicorns shorthand if your have more complex nginx routing going on.

3  我的nginx.conf的配置跟第一步的内容有点不一样



4  config/unicorn.rb 文件中也需要有一点要注意的就是 port要修改为跟nginx里面的配置一致

我的是8081,每个项目都会不一样,但是我的nginx是8080这个没有去动


0 0
原创粉丝点击