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
項目了