设为首页 收藏本站
查看: 1353|回复: 0

[经验分享] 如何在 Mac 下用 Nginx + Passenger 部署 Rails 的运行环境。

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2014-11-19 09:48:33 | 显示全部楼层 |阅读模式
系统需求

    Mac OSX Lion

步骤0 安装环境依赖

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

https://developer.apple.com/down ... de_4.1_for_lion.dmg

安装RVM

$ bash < <(curl -s https://raw.github.com/wayneeseg ... ripts/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

    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.

    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的官方网站

    http://wiki.nginx.org/Configuration
    http://www.modrails.com/documentation/Users%20guide%20Nginx.html

修改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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-31444-1-1.html 上篇帖子: Redmine接收邮件传入的问题 下篇帖子: ruby on rails 项目部署(Nginx + Passenger) 运行环境 如何
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表