前提
已经存在的环境:
Mac OS X Yosemite
ruby-2.2.1
rails (4.2.4)
安装Passenger
用gem安装passenger
$ gem install passenger
安装Nginx
如果之前安装了Nginx需要先卸载掉
$ sudo rm -f -R /usr/local/nginx && rm -f /usr/local/sbin/nginx
用Passenger Nginx module installer安装Nginx
$ passenger-install-nginx-module
验证是否成功安装,例如
$ rvmsudo passenger-config validate-install
* Checking whether this Phusion Passenger install is in PATH... ✓
* Checking whether there are no other Phusion Passenger installations... ✓
使用Nginx
启动Nginx
$ sudo /opt/nginx/sbin/nginx
停止Nginx
$ sudo kill $(cat /opt/nginx/logs/nginx.pid)
查看Nginx和Passenger进程
$ rvmsudo passenger-memory-stats
Version: 5.0.8
Date : 2015-05-28 08:46:20 +0200
...
---------- Nginx processes ----------
PID PPID VMSize Private Name
-------------------------------------
12443 4814 60.8 MB 0.2 MB nginx: master process /usr/sbin/nginx
12538 12443 64.9 MB 5.0 MB nginx: worker process
### Processes: 3
### Total private dirty RSS: 5.56 MB
----- Passenger processes ------
PID VMSize Private Name
--------------------------------
12517 83.2 MB 0.6 MB PassengerAgent watchdog
12520 266.0 MB 3.4 MB PassengerAgent server
12531 149.5 MB 1.4 MB PassengerAgent logger
...
如果Nginx已经启动,应该可以看见这些基本进程,如果没有,那么可能在安装配置过程存在问题。
在Nginx上部署Rails
编辑nginx.conf
$ sudo vim /opt/nginx/conf/nginx.conf
做如下配置
server {
listen 80;
server_name localhost;
root /Users/wanglei/Documents/myrails/blog/public;
passenger_enabled on;
}
listen端口,server_name域名或ip地址, root Rails项目的public目录。
在浏览器中输入localhost即可打开项目。
如无法打开index,可以在rails routes.rb中设置路由
root 'welcome#index'
get 'index' => "welcome#index"
如上两种方法可试。
more:
https://www.phusionpassenger.com/library/install/nginx/install/oss/rubygems_rvm/
https://www.phusionpassenger.com/library/deploy/nginx/deploy/ruby/