mac中搭建nginx + unicorn

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_PATH

stdeer_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這個沒有去動


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章