Mac 下用 Nginx + Passenger 部署 Rails 的運行環境。

系統需求

  • Mac OSX Lion

步驟0 安裝環境依賴

安裝Xcode 4.1,Xcode4.2以及更高的版本在 Lion 仍然存在一些兼容性問題,強烈建議使用XCode 4.1,下載地址: html

https://developer.apple.com/downloads/download.action?path=Developer_Tools/xcode_4.1_for_lion/xcode_4.1_for_lion.dmg

安裝RVM nginx

$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

配置RVM自動加載,將下面這一行代碼添加到~/.bash_profile中,而後退出iTerm並從新啓動 git

[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm

安裝 ruby-1.9.2-p290 github

$ rvm install 1.9.2

設置系統默認使用 ruby-1.9.2 vim

$ rvm use 1.9.2 --default

步驟1 安裝 Rails

安裝Rails xcode

$ gem install rails

Rails安裝完成後,建立一個rails項目,假定你的項目叫作:awesome project 瀏覽器

$ rails new awesome_project

啓動Rails,並訪問 http://localhost:3000 ruby

$ cd awesome_project
$ rails server

步驟2 安裝 Passenger 和 Nginx

首先經過gem安裝passenger bash

$ gem install passenger

由於Nginx不支持動態module載入,因此須要經過Passenger來自動下載,編譯,安裝由Passenger修改版的Nginx: app

安裝Passenger + Nginx

$ passenger-install-nginx-module
  1. Yes: download, compile and install Nginx for me. (recommended)
    The easiest way to get started. A stock Nginx 1.0.10 with Passenger
    support, but with no other additional third party modules, will be
    installed for you to a directory of your choice.

  2. No: I want to customize my Nginx installation. (for advanced users)
    Choose this if you want to compile Nginx with more third party modules
    besides Passenger, or if you need to pass additional options to Nginx's
    'configure' script. This installer will 1) ask you for the location of
    the Nginx source code, 2) run the 'configure' script according to your
    instructions, and 3) run 'make install'.

Whichever you choose, if you already have an existing Nginx configuration file,
then it will be preserved.

Enter your choice (1 or 2) or press Ctrl-C to abort: 這裏建議選擇1

Please specify a prefix directory [/opt/nginx]: /usr/local/nginx

當詢問nginx的安裝路徑的時候,我的建議安裝到/usr/local/nginx下

當安裝完成後,會在console中提示如何配置Nginx

Passenger會自動幫你將下面兩行添加到Nginx的配置文件中/usr/local/nginx/conf/nginx.conf(很人性化)

http {
  ...
  passenger_root /Users/Daniel/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.10;
  passenger_ruby /Users/Daniel/.rvm/wrappers/ruby-1.9.2-p290/ruby;
  ...
}
server {
  listen 80;
  server_name www.yourhost.com;
  root /somewhere/public;   # <--- be sure to point to 'public'!
  passenger_enabled on;
}

請不要忘記將nginx命令行程序鏈接到/usr/local/sbin

$ sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

步驟3 配置Nginx + Passenger + Rails

關於Nginx的配置,請參考Nginx的官方網站以及Passenger的官方網站

修改hosts文件,給你的項目一個本地域名, 好比awesome_project.local

$ sudo vim /etc/hosts
127.0.0.1 awesome_project.local

測試hosts

$ ping awesome_project.local
PING awesome_project.local (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.054 ms

繼續配置Nginx, 這裏我給出一個最小可運行的Nginx配置文件

$ vim /usr/local/nginx/conf/nginx.conf

nginx.conf

worker_processes 1; events { worker_connections 1024; } http { passenger_root /Users/Daniel/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.10; passenger_ruby /Users/Daniel/.rvm/wrappers/ruby-1.9.2-p290/ruby; include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name awesome_project.local; root /Users/Daniel/awesome_project/public; passenger_enabled on; rails_env development; } }

測試Nginx的配置文件語法是否正確

$ sudo nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

啓動Nginx

$ sudo nginx

如何在修改Nginx的配置文件後,讓Nginx載入新配置

$ sudo nginx -s reload

如何中止Nginx

$ sudo nginx -s stop

如何在不停Nginx的狀況下,從新啓動Passenger

$ cd path/to/your/awesome/project $ touch tmp/restart.txt

好了,這個時候你能夠打開瀏覽器,訪問你的awesome_project網站了

http://awesome_project.local
相關文章
相關標籤/搜索