版本 9, 最后更新于

针对于新入门的开发者,如何在 Mac 下用 Nginx + Passenger 部署 Rails 的运行环境。

系统需求

  • Mac OSX Lion

步骤0 安装环境依赖

安装Xcode 4.1,Xcode4.2以及更高的版本在 Lion 仍然存在一些兼容性问题,强烈建议使用XCode 4.1,下载地址:

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

安装RVM

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

配置RVM自动加载,将下面这一行代码添加到~/.bash_profile中,然后退出iTerm并重新启动

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

安装 ruby-1.9.2-p290

$ rvm install 1.9.2

设置系统默认使用 ruby-1.9.2

$ rvm use 1.9.2 --default

步骤1 安装 Rails

安装Rails

$ gem install rails

Rails安装完成后,创建一个rails项目,假定你的项目叫做:awesome project

$ rails new awesome_project

启动Rails,并访问 http://localhost:3000

$ cd awesome_project
$ rails server

步骤2 安装 Passenger 和 Nginx

首先通过gem安装passenger

$ gem install passenger

因为Nginx不支持动态module载入,所以需要通过Passenger来自动下载,编译,安装由Passenger修改版的Nginx:

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

最后,希望你能够在Rails的开发中找到快乐!

本页贡献者:

Small_6c5369e0e1aa53683c00434d76019800 Small_f08750e605d58ff54872d955169ce0c3 B75f97b497b215815971e19fdbaafa40
Small_6c5369e0e1aa53683c00434d76019800 huacnlee 发表于与

@soloara 你错啦,谁给你说的 Nginx 小众啊

Dda6c4388751b5713ab51668c50ded1d soloara 发表于与

@huacnlee @lgn21st 两位不厚道,只给nginx配置,不给apache的配置,虽然差不多但新人在看到nginx会挺茫然的啊,毕竟nginx还算稍微小众一些。我再补个ubuntu下面apache+passenger的配发吧。。。

5756a5379678165d9b3512b4b60e0151 metal 发表于与

@soloara apache也简单。去看官方文档,不懂英文也应该知道怎么做。

7a23526e8de7f2fa57d2481b549b6d57 RobinWu 发表于与

支持 Nginx + Passenger

7a23526e8de7f2fa57d2481b549b6d57 RobinWu 发表于与
Bd115d162acfd342e2539376cf7f7dd0 lanvige 发表于与

我也贡献一篇,使用homebrew 安装,和新版本的更新:
http://lanvige.com/posts/deploy_rails_local_with_nginx_passenger_rvm_and_homebrew_on_osx/

66b29825a572fc6fe7665a764687c212 jiffies 发表于与

@huacnlee @metal 跪求ubuntu下的apache+passenger的配置。 还有这篇再ubuntu下同样可以吗?

00ced3a6cdfd0398fabe0717147a6a32 DrayChou 发表于与

@jiffies 同求啊

941056da1db23b2c0081e9c8d9416574 hlcfan 发表于与

我照着做的,有问题.请看:http://ruby-china.org/topics/2873 谢谢!

83cd9d30670582968eff9e108940f76a songlipeng2003 发表于与

好文章,就是和nginx配合时,不能在原有基础上安装很不爽

83cd9d30670582968eff9e108940f76a songlipeng2003 发表于与

网速卡了,我以为没有点上,就多点了几下