passenger
是一个能快速搭建web环境的工具,它能快速的将nginx
和passenger
部署到你的服务器中,是部署ruby
环境就如同php环境那样简单快速,让人愉悦。下面我将使用这个工具将一个几乎空白的web服务器打形成一个高效的ruby服务器php
centos7
是最新的centos版本带来了一系列新特性,包括对Docker的支持和性能的提升,centos 6和 centos 7性能对比html
首先下载rvm
(ruby虚拟机)nginx
shellcurl -L get.rvm.io | bash -s stable
安装rvm
web
shellsource /etc/profile.d/rvm.sh
安装ruby
(请选择官网上最新的版本,使用ruby
就要一直坚决的使用其最新版本)shell
shellrvm install 2.2.1
安装完成后只要运行ruby -v
有显示版本号就证实已经安装成功了vim
首先使用gem
安装passenger
centos
shellgem install passenger
因为nginx
不支持动态的模块载入,因此要使用passenger
来进行编译安装由passenger
修改过的nginx
安全
接下来安装nginx
+passenger
ruby
shellpassenger-install-nginx-module
运行了这个命令后,按照提示一步步安装bash
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,1表明自动完整安装并配置nginx,2是表明根据本身需求定制nginx.
安装完成后系统会提示,nginx
安装的目录,在centos7
下默认是安装在/opt/nginx
下,配置文件是默认在/opt/nginx/conf/nginx.conf
打开nginx.conf
咱们能够看到,passenger
已经在nginx
的配置文件上作了一点小配置
passenger_root /usr/local/rvm/gems/ruby-2.2.1/gems/passenger-5.0.10; passenger_ruby /usr/local/rvm/gems/ruby-2.2.1/wrappers/ruby;
使用gem
安装rails
shellgem install rails
初始化一个rails
项目
shellrails new sample_app
第一次初始化rails
时通常会报出缺乏gem
的警告,此时只须要将rails
的镜像改成淘宝镜像,详见http://ruby.taobao.org,而后执行
shellbundle install
当执行完毕后,一个rails
项目的初始化就完成了
打开配置文件
vim /opt/nginx/conf/nginx.conf
这里给出一份最简单能运行的nginx.conf
(注意:rails项目的目录是/opt/www)
nginx{ worker_processes 1; events { worker_connections 1024; } http { passenger_root /usr/local/rvm/gems/ruby-2.2.1/gems/passenger-5.0.10; passenger_ruby /usr/local/rvm/gems/ruby-2.2.1/wrappers/ruby; include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { #监听的端口 listen 8080; server_name 127.0.0.1; #web根目录,必定是rails项目下的public root /var/www/sample_app/public/; #必定要记得将这个选项设置为on passenger_enabled on; } }
运行
shellsbin/nginx -t
若是没有报错,那说明配置成功了。那么已经万事大吉了吗?并无!!
Centos7
后已经废弃了原来的iptables
,改而使用firewall
,默认状况下centos7
系统不容许任何外来访问,就算你把firewall
关了也没用,因此必须配置firewall
shellfirewall-cmd --zone=public --add-port=8080/tcp --permanent
这个命令表示,容许外部访问8080端口,重载一下firewall
的配置,就外部就能访问服务器的8080端口了
配置完Centos7
的防火墙后,访问rails
程序时就会报出一个403的forbidden错误,仔细查看日志后,发现了问题了的缘由
App 6361 stderr: [ 2015-06-16 11:27:24.1412 6376/0x00000001d35760(Worker 1) utils.rb:85 ]: *** Exception RuntimeError in Rack application object (Missing `secret_token` and `secret_key_base` for 'production' environment, set these values in `config/secrets.yml`) (process 6376, thread 0x00000001d35760(Worker 1)):
这个错误表示Rails
生产环境下的密钥没有配置。在nginx
上跑rails
通常只有在生产环境下才会使用,于是passenger
默认下就是rails
环境设置为生产环境,而rails
初始化时默认没有对生产环境进行密钥配置。这时就须要咱们本身去配置rails
的密钥了
在rails
的Gemfile
中加入
rubygem 'dotenv-rails'
而后运行
shellbundle install
安装完这个gem
后就能够配置咱们的生产环境密钥了
首先在sample_app
目录下创建一个.env
文件
而后运行
shellrake secret
这个命令会随机生成一个安全密钥,将这个密钥复制下来,而后在.env
中添加
rubySECRET_KEY_BASE = 你的密钥
最后修改sample_app目录下的config/secrets.yml
yml
development: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> test: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
这样一来密钥配置就完成了,重启nginx
就能成功访问到rails
项目了