[0001] CentOS7下部署nginx+mongrel+rails环境

来源:互联网 发布:ubuntu和kali 编辑:程序博客网 时间:2024/04/30 04:47

0.安装依赖

yum install zlib zlib-devel openssl openssl-devel

1.安装ruby (http://ruby-lang.org/)

ruby官网下载(https://www.ruby-lang.org/zh_cn/downloads/) Ruby 2.1.3源码,进入下载解压,编译安装:

./configure

make

make install

查看安装信息:

installing binary commands:   /usr/local/bininstalling base libraries:    /usr/local/libinstalling arch files:        /usr/local/lib/ruby/2.1.0/x86_64-linuxinstalling pkgconfig data:    /usr/local/lib/pkgconfiginstalling command scripts:   /usr/local/bininstalling library scripts:   /usr/local/lib/ruby/2.1.0installing common headers:    /usr/local/include/ruby-2.1.0installing manpages:          /usr/local/share/man/man1installing extension objects: /usr/local/lib/ruby/2.1.0/x86_64-linuxinstalling extension objects: /usr/local/lib/ruby/site_ruby/2.1.0/x86_64-linuxinstalling extension objects: /usr/local/lib/ruby/vendor_ruby/2.1.0/x86_64-linuxinstalling extension headers: /usr/local/include/ruby-2.1.0/x86_64-linuxinstalling extension scripts: /usr/local/lib/ruby/2.1.0installing extension scripts: /usr/local/lib/ruby/site_ruby/2.1.0installing extension scripts: /usr/local/lib/ruby/vendor_ruby/2.1.0installing extension headers: /usr/local/include/ruby-2.1.0/rubyinstalling default gems:      /usr/local/lib/ruby/gems/2.1.0 (build_info, cache, doc, extensions, gems, specifications)                              bigdecimal 1.2.4                              io-console 0.4.2                              json 1.8.1                              minitest 4.7.5                              psych 2.0.5                              rake 10.1.0                              rdoc 4.1.0                              test-unit 2.1.3.0installing rdoc:              /usr/local/share/ri/2.1.0/systeminstalling capi-docs:         /usr/local/share/doc/ruby
安装成功,查看Ruby版本:
ruby -v

ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

2.安装rubygems (http://rubygems.org/)

官网下载最新rubygems版本RubyGems 2.4.2,进入下载目录直接安装:

ruby setup.rb

安装信息如下:

RubyGems installed the following executables:/usr/local/bin/gemRuby Interactive (ri) documentation was installed. ri is kind of like man pages for ruby libraries. You may access it like this:  ri Classname  ri Classname.class_method  ri Classname#instance_methodIf you do not wish to install this documentation in the future, use the--no-document flag, or set it as the default in your ~/.gemrc file. See'gem help env' for details.

安装成功,查看rubygems版本:

gem -v
2.4.2

3.安装rails (http://rubygems.org/pages/download/)

注意:此处选择安装Rails 4.1.6在启动mongrel时会有问题,详见mongrel小节描述,请先跳过此处阅读( 4. 集成mongrel )

使用gem安装rails,

gem install -V rails -v 4.1.6

如果出现如下错误,

[root@localhost download]# gem install --helpERROR:  Loading command: install (LoadError)cannot load such file -- zlibERROR:  While executing gem ... (NoMethodError)    undefined method `invoke_with_build_args' for nil:NilClass
则进入ruby源码文件夹 ,安装ruby自身提供的zlib包:
#cd ext/zlib
#ruby ./extconf.rb
#make

#make install

安装完毕后再次运行安装rails:

gem install -V rails -v 4.1.6

(如遇到各种安装报错情况,请参考文章:点击打开链接 解决)

安装成功后,新建rails工程:

rails new blog

进入工程根目录,启动rails服务:

rails server

打开浏览器,输入localhost:3000回车后进入主页面,至此ROR环境搭建完成。

4. 集成mongrel (https://rubygems.org/gems/mongrel/versions/)

mongrel下载地址为:mongrel下载

使用gem安装mongrel:

gem install mongrel -v 1.2.0.pre2 --pre

安装信息如下:

Done installing documentation for gem_plugin, daemons, mongrel after 2 seconds3 gems installed

进入blog工程根目录,用mongrel启动rails服务:

mongrel_rails start

如果启动出现如下问题:

no such file to load -- dispatcher (LoadError)

则参考点击打开链接解决:
 down voteJust in case you didn't figure it out.I ran gem install 'dispatcher' and added gem 'dispatcher' in the Gemfile of my app.
Then everything was fine.

具体步骤为:

gem install dispatcher -V

进入你自己的rails工程目录,打开Gemfile文件,加入:gem 'dispatcher' 保存文件。

之后再次启动:

mongrel_rails start

如果出现如下错误:

/usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require': cannot load such file -- mongrel/rails (LoadError)

则打开Gemfile文件,加入:gem 'mongrel','1.2.0.pre2' 保存文件。(备注:此处的' 1.2.0.pre2' 参数为你安装的mongrel版本号,具体请根据自己安装自行调整!)

再次启动mongrel:

mongrel_rails start

启动成功,打印信息如下:

** Starting Mongrel listening at 0.0.0.0:3000** Starting Rails with development environment...** Rails loaded.** Loading any Rails specific GemPlugins

** Signals ready.  TERM => stop.  USR2 => restart.  INT => stop (no restart).** Rails signals registered.  HUP => reload (without restart).  It might not work well.** Mongrel 1.2.0.pre2 available at 0.0.0.0:3000** Use CTRL-C to stop.

mongrel启动成功!

尝试访问服务:

curl http://127.0.0.1:3000

返回如下信息:

curl: (52) Empty reply from server

(备注:如果用浏览器访问,则提示“无法连接”)

服务器对于请求返回了空,但mongrel已经正常启动了。

通过多方查找,终于找到解决办法,替换之前的启动命令:

mongrel_rails star 

为:

rails server mongrel -e development -b 0.0.0.0 -p 80 -P tmp/pids/server.pid

再次启动服务。

成功启动后打印如下信息:

=> Booting Mongrel=> Rails 4.1.6 application starting in development on http://0.0.0.0:80=> Run `rails server -h` for more startup options=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)=> Ctrl-C to shutdown server

再次键入地址进行访问:

curl http://127.0.0.1:3000

成功返回请求的html代码,让我们总结一下:

在每次新建rails工程后,需要手动修改工程根目录下的Gemfile文件:

1. 添加 gem 'mongrel','1.2.0.pre2'

2. 添加 gem 'dispatcher'

3. 启动: rails server mongrel -e development -b 0.0.0.0 -p 80 -P tmp/pids/server.pid

到此在CentOS7环境下,已成功搭建好了mongrel和rails,接下来还有mongrel_cluster和nginx需要完成。请继续往下看!

错误更正:

今天在安装集成mongrel_cluster后,启动服务,访问服务时发现问题:

尝试访问服务:

curl http://127.0.0.1:3000

返回如下信息:

curl: (52) Empty reply from server

(备注:如果用浏览器访问,则提示“无法连接”)

服务器对于请求返回了空,但mongrel已经正常启动了。

依然存在,故之前的使用命令:rails server mongrel -e development -b 0.0.0.0 -p 80 -P tmp/pids/server.pid

启动成功不是问题根因,不能解决该问题。继续查找资料,没有发现对于该问题的描述,后来推测可能是rails版本太新导致的,故卸载了已安装的Rails 4.1.6,安装Rails 3.2.0,重新生成工程,再次使用mongrel_rails start启动服务,发现可以正常启动,

再次键入地址进行访问:

curl http://127.0.0.1:3000

成功返回了html页面代码:

  <body>    <div id="page">      <div id="sidebar">        <ul id="sidebar-items">          <li>            <h3>Browse the documentation</h3>            <ul class="links">              <li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li>              <li><a href="http://api.rubyonrails.org/">Rails API</a></li>              <li><a href="http://www.ruby-doc.org/core/">Ruby core</a></li>              <li><a href="http://www.ruby-doc.org/stdlib/">Ruby standard library</a></li>            </ul>          </li>        </ul>      </div>      <div id="content">        <div id="header">          <h1>Welcome aboard</h1>          <h2>You’re riding Ruby on Rails!</h2>        </div>        <div id="about">          <h3><a href="rails/info/properties" onclick="about(); return false">About your application’s environment</a></h3>          <div id="about-content" style="display: none"></div>        </div>        <div id="getting-started">          <h1>Getting started</h1>          <h2>Here’s how to get rolling:</h2>          <ol>            <li>              <h2>Use <code>rails generate</code> to create your models and controllers</h2>              <p>To see all available options, run it without parameters.</p>            </li>            <li>              <h2>Set up a default route and remove <span class="filename">public/index.html</span></h2>              <p>Routes are set up in <span class="filename">config/routes.rb</span>.</p>            </li>            <li>              <h2>Create your database</h2>              <p>Run <code>rake db:create</code> to create your database. If you're not using SQLite (the default), edit <span class="filename">config/database.yml</span> with your username and password.</p>            </li>          </ol>        </div>      </div>      <div id="footer"> </div>    </div>  </body></html>
成功了!!
到此在CentOS7环境下,已成功搭建好了mongrel和rails!下节将介绍安装使用mongrel_cluster插件实现mongrel服务的集群。

4. 安装mongrel_cluster插件实现mongrel集群

使用mongrel_rails start

启动的应用服务是单进程的,既只能绑定且监听某一IP的一个端口,故当出现大量HTTP请求时,只能按请求的先后顺序串行处理来自客户端的HTTP请求,如此性能低下。而是用mongrel_cluster插件后,可以指定多个mongrel实例绑定到同一IP的不同端口上,实现并行处理HTTP请求,再配合nginx的负载均衡,可以大大提高服务器性能。

首先安装mongrel_cluster插件:

gem install mongrel_cluster -V

然后配置mongrel_cluster

在Rails项目的根目录下执行以下命令,生成config/mongrel_cluster.yml 文件,供启用mongrel集群使用

mongrel_rails cluster::configure -e production -p 3000 -a 127.0.0.1 -N 2

有关cluster::configure 更多参数使用可借助帮助命令查看

mongrel_rails cluster::configure -h

生成的文件内容如下:

---address: 127.0.0.1log_file: log/mongrel.logport: "3000"environment: productionpid_file: tmp/pids/mongrel.pidservers: 2

启用mongrel_cluster

mongrel_rails cluster::startstarting port 3000starting port 3001
键入地址进行访问:

curl http://127.0.0.1:3000

 <body>    <div id="page">      <div id="sidebar">        <ul id="sidebar-items">          <li>            <h3>Browse the documentation</h3>            <ul class="links">              <li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li>              <li><a href="http://api.rubyonrails.org/">Rails API</a></li>              <li><a href="http://www.ruby-doc.org/core/">Ruby core</a></li>              <li><a href="http://www.ruby-doc.org/stdlib/">Ruby standard library</a></li>            </ul>          </li>        </ul>      </div>      <div id="content">        <div id="header">          <h1>Welcome aboard</h1>          <h2>You’re riding Ruby on Rails!</h2>        </div>        <div id="about">          <h3><a href="rails/info/properties" onclick="about(); return false">About your application’s environment</a></h3>          <div id="about-content" style="display: none"></div>        </div>        <div id="getting-started">          <h1>Getting started</h1>          <h2>Here’s how to get rolling:</h2>          <ol>            <li>              <h2>Use <code>rails generate</code> to create your models and controllers</h2>              <p>To see all available options, run it without parameters.</p>            </li>            <li>              <h2>Set up a default route and remove <span class="filename">public/index.html</span></h2>              <p>Routes are set up in <span class="filename">config/routes.rb</span>.</p>            </li>            <li>              <h2>Create your database</h2>              <p>Run <code>rake db:create</code> to create your database. If you're not using SQLite (the default), edit <span class="filename">config/database.yml</span> with your username and password.</p>            </li>          </ol>        </div>      </div>      <div id="footer"> </div>    </div>  </body></html>

curl http://127.0.0.1:3001

  <body>    <div id="page">      <div id="sidebar">        <ul id="sidebar-items">          <li>            <h3>Browse the documentation</h3>            <ul class="links">              <li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li>              <li><a href="http://api.rubyonrails.org/">Rails API</a></li>              <li><a href="http://www.ruby-doc.org/core/">Ruby core</a></li>              <li><a href="http://www.ruby-doc.org/stdlib/">Ruby standard library</a></li>            </ul>          </li>        </ul>      </div>      <div id="content">        <div id="header">          <h1>Welcome aboard</h1>          <h2>You’re riding Ruby on Rails!</h2>        </div>        <div id="about">          <h3><a href="rails/info/properties" onclick="about(); return false">About your application’s environment</a></h3>          <div id="about-content" style="display: none"></div>        </div>        <div id="getting-started">          <h1>Getting started</h1>          <h2>Here’s how to get rolling:</h2>          <ol>            <li>              <h2>Use <code>rails generate</code> to create your models and controllers</h2>              <p>To see all available options, run it without parameters.</p>            </li>            <li>              <h2>Set up a default route and remove <span class="filename">public/index.html</span></h2>              <p>Routes are set up in <span class="filename">config/routes.rb</span>.</p>            </li>            <li>              <h2>Create your database</h2>              <p>Run <code>rake db:create</code> to create your database. If you're not using SQLite (the default), edit <span class="filename">config/database.yml</span> with your username and password.</p>            </li>          </ol>        </div>      </div>      <div id="footer"> </div>    </div>  </body></html>
成功,到此mongrel_cluster集群集成完毕!

关闭集群命令:

mongrel_rails cluster::stop

4. 安装配置Nginx (如果想系统学习Nginx请下载Nginx权威书籍阅读Nginx模块开发与架构解析)

4.1安装nginx

下载地址 http://nginx.org/en/download.html

我下载的版本是: nginx-1.6.2.tar.gz

解压安装

tar -zxvf nginx-1.6.2.tar.gzcd nginx-1.6.2.tar.gz/./configuremakemake install

默认情况下安装目录是 /usr/local/nginx

4.2 配置nginx (深入学习Nginx请阅读:)

编辑 emacs /usr/local/nginx/conf/nginx.conf,以下是添加的内容

upstream mongrel {  server 127.0.0.1:3000;  server 127.0.0.1:3001;}# rails serverserver {  listen 80;  server_name redmine.moon.ossxp.com;  root /home/zhangwei/workspace/blog/public; #注意这里一定要指向Rails应用的public目录  index index.html index.htm;  location / {    proxy_pass http://mongrel;    proxy_redirect off;    proxy_set_header Host $host;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  }}

启用mongrel_cluster

mongrel_rails cluster::startstarting port 3000starting port 3001

启用nginx

/usr/local/nginx/sbin/nginx

测试是否部署成功

键入 curl http://localhost按回车键,如果显示rails主页,则证明部署成功。


常用命令:

关闭mongrel_cluster

mongrel_rails cluster::stop

关闭nginx

/usr/local/nginx/sbin/nginx -s quit(“优雅地”停止)

/usr/local/nginx/sbin/nginx -s stop (快速停止)

到此,CentOS7 上部署Nginx+Mongrel+Rails彻底完成!(结束)

5.局域网其他电脑无法访问服务器rails应用问题

在安装部署成功后,我尝试通过局域网其他电脑访问服务器上的rails应用:(备注,我服务器ip:192.168.1.104

浏览器地址栏输入:http://192.168.1.104:3000 按回车后提示无法连接,原因为:防火墙设置未开放端口。故解决办法如下:

由于CentOS 7.0默认使用的是firewall作为防火墙,这里为方便操作改为iptables防火墙。

1、关闭firewall:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall开机启动

2、安装iptables防火墙

yum install iptables-services #安装

3、修改iptables防火墙配置,设置生效

emacs  /etc/sysconfig/iptables #编辑防火墙配置文件

原文件内容如下:

# Firewall configuration written by system-config-firewall# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT 
由于我的rails服务绑定起始端口为:3000,服务数量为2,故需要开放的端口为:3000,3001。则添加这两个端口的开放配置:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3001 -j ACCEPT
修改后的文件为:

# Firewall configuration written by system-config-firewall# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT<pre name="code" class="plain">-A INPUT -m state --state NEW -m tcp -p tcp --dport 3000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3001 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT

保存退出。

systemctl restart iptables.service #最后重启防火墙使配置生效

systemctl enable iptables.service #设置防火墙开机启动

重新启动服务,另一台电脑再次访问:http://192.168.1.104:3000

成功显示rails首页。




0 0
原创粉丝点击