1. 程式人生 > >Ruby on Rails 終極部署方案 nginx+mina+puma

Ruby on Rails 終極部署方案 nginx+mina+puma

搭建工具介紹

Ruby on Rails作為一款十分優秀的web開發框架,在當前web領域中慢慢佔據了越來越重要,秉承rails快速開發的特點,很多快速部署rails的方案也越來越多。這篇文章中所選的方案是我個人認為十分優秀的部署方案。這套部署方案的結構是,nginx作為反向代理伺服器負責負載均衡,mina作為自動化部署工具,puma作為rails的web伺服器

nginx

nginx是一款優秀的代理伺服器,其高效的效能已經得到了業界的廣泛認可,相信作為web開發人員不會沒聽說過他的大名

mina

mina是一款由ruby開發的自動化部署工具,其目的是為了簡化每次rails程式碼提交時的部署,一鍵完成部署,杜絕了提交到git伺服器後,又去伺服器上git pull的情況

puma

puma是一款專門針對rails的併發伺服器,相對於passengerpuma可配置面更廣,而且效能比passenger更高,是rails web伺服器的不二之選

部署前言

由於這篇文章需要很多鋪墊,包括rails的安裝下載,git的配置等等,需要讀者自己去查閱資料或者查閱之前我寫過的一些文章,如果期間有什麼問題,請留言。。

mina

首先在你的rails專案的Gemfile中加上

gem mina

執行bundle 安裝 mina,接著在你的rails專案根目錄初始化mina

mina init

這是在你專案的config目錄下會有一個deploy.rb,配置deploy.rb

,列出重點部分,每一行的解釋會附在程式碼的註釋裡

#伺服器地址,是使用ssh的方式登入伺服器
set :domain, '[email protected]'
#伺服器中專案部署位置
set :deploy_to, '/var/www/ruby_sample'
#git程式碼倉庫
set :repository, 'https://github.com/gameFu/ruby_sample.git'
#git分支
set :branch, 'master'

# 中括號裡的檔案 會出現在伺服器專案附錄的shared資料夾中,這裡加入了secrets.yml,環境金鑰無需跟開發計算機一樣
set :shared_paths
, ['config/database.yml', 'log', 'config/secrets.yml'] # 這個塊裡面的程式碼表示執行 mina setup時執行的命令 task :setup => :environment do # 在伺服器專案目錄的shared中建立log資料夾 queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"] queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"] # 在伺服器專案目錄的shared中建立config資料夾 下同 queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"] queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"] queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"] queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"] # puma.rb 配置puma必須得資料夾及檔案 queue! %[mkdir -p "#{deploy_to}/shared/tmp/pids"] queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/pids"] queue! %[mkdir -p "#{deploy_to}/shared/tmp/sockets"] queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/tmp/sockets"] queue! %[touch "#{deploy_to}/shared/config/puma.rb"] queue %[echo "-----> Be sure to edit 'shared/config/puma.rb'."] # tmp/sockets/puma.state queue! %[touch "#{deploy_to}/shared/tmp/sockets/puma.state"] queue %[echo "-----> Be sure to edit 'shared/tmp/sockets/puma.state'."] # log/puma.stdout.log queue! %[touch "#{deploy_to}/shared/log/puma.stdout.log"] queue %[echo "-----> Be sure to edit 'shared/log/puma.stdout.log'."] # log/puma.stdout.log queue! %[touch "#{deploy_to}/shared/log/puma.stderr.log"] queue %[echo "-----> Be sure to edit 'shared/log/puma.stderr.log'."] queue %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/database.yml'."] end #這個程式碼塊表示執行 mina deploy時執行的命令 desc "Deploys the current version to the server." task :deploy => :environment do to :before_hook do end deploy do #重新拉git伺服器上的最新版本,即使沒有改變 invoke :'git:clone' #重新設定shared_path位置 invoke :'deploy:link_shared_paths' invoke :'bundle:install' invoke :'rails:db_migrate' invoke :'rails:assets_precompile' invoke :'deploy:cleanup' to :launch do queue "mkdir -p #{deploy_to}/#{current_path}/tmp/" # queue "chown -R www-data #{deploy_to}" queue "touch #{deploy_to}/#{current_path}/tmp/restart.txt" end end end

這樣一來mina的基本配置就完成,接下來只要將你開發環境的專案上傳到git伺服器,然後執行下面的命令就完成了

mina deploy

完成部署後,你就可以在指定的伺服器目錄下看到你的專案,目錄結構如下

  • current -當前版本目錄也就是專案目錄
  • last_version -版本號
  • releases/ -過去的版本
  • scm/
  • shared/ 先前shared_path所設定另外拉出來的檔案都在這裡
  • tmp/

這裡需要注意的幾點
1.shared_path裡面的檔案不僅僅是表示這些檔案會在伺服器目錄中出現在另外的目錄裡,也表示這些檔案或者目錄不會受到git版本庫的控制,也就是說這些檔案的配置必須在你伺服器中手動去配置,這兩個檔案包括database.yml和secrets.yml,在shared/config目錄下
2.針對deploy最好在伺服器建立一個使用者,並針對他建立一個ssh authorized_keys,這裡直接使用了root身份,參考centos7 伺服器部署ssh證書授權登入,這樣做能避免每次部署的時候都需要輸入伺服器賬號密碼

可能會遇到的問題

由於生產環境一般會搭配類似於postgresql等成熟資料庫,這裡我就舉出一個搭建postgresql,首先是啟動資料庫時(centos 7下),如果遇到問題請使用下面的命令就能看到詳細的錯誤資訊

systemctl status postgresql-9.4.service -l 

然後在跑mina deploy時可能會報類似於這樣的一個錯誤

 Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).

從錯誤資訊上能很明顯的看出是因為沒有安裝pg這個包導致的,但是有一種情況是明明在專案的Gemfile上寫上了pg但還是跑不過,造成這個的原因,可能是由於你的伺服器環境缺少了pg的標頭檔案導致的,如果是在centos下,只需要執行下面命令就能解決

yum install postgresql-libs
yum install postgresql-devel

Puma

首先在你的Gemfile里加上

gem puma

然後在config目錄下手動建立一個puma.rb檔案,配置puma.rb檔案

#!/usr/bin/env puma

#rails的執行環境
environment 'production'
threads 2, 64
workers 4

#專案名
app_name = "ruby_sample"
#專案路徑
application_path = "/var/www/#{app_name}"
#這裡一定要配置為專案路徑下地current
directory "#{application_path}/current"

#下面都是 puma的配置項
pidfile "#{application_path}/shared/tmp/pids/puma.pid"
state_path "#{application_path}/shared/tmp/sockets/puma.state"
stdout_redirect "#{application_path}/shared/log/puma.stdout.log", "#{application_path}/shared/log/puma.stderr.log"
bind "unix://#{application_path}/shared/tmp/sockets/#{app_name}.sock"
activate_control_app "unix://#{application_path}/shared/tmp/sockets/pumactl.sock"

#後臺執行
daemonize true
on_restart do
  puts 'On restart...'
end
preload_app!

這裡需要注意的地方

  • threads - puma的執行緒數,第一個引數是最小的執行緒數,第二個引數是最大執行緒數
  • bind - 這個指定的是puma執行時產生的socket,後面nginx會用到
  • 這裡所有對應的目錄是在deploy配置中配置的,如果需要更改配置目錄,deploy.rb也需要相應的更改

Nginx

下載安裝nginx後,開啟nginx的配置檔案nginx.conf進行配置

  worker_processes  1;

      error_log  /var/log/nginx/error.log warn;
      pid        /var/run/nginx.pid;

      events {
          worker_connections  1024;
      }

      http {
          include       /etc/nginx/mime.types;
          default_type  application/octet-stream;

          log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for"';

          access_log  /var/log/nginx/access.log  main;

          sendfile        on;
          #tcp_nopush     on;

          keepalive_timeout  65;

          #gzip  on;

          #include /etc/nginx/conf.d/*.conf;
          upstream deploy {
                  server unix:///var/www/ruby_sample/shared/tmp/sockets/ruby_sample.sock;
          }

          server {
              listen 80;
              server_name your.server.domain.ip; # change to match your URL
              root /var/www/ruby_sample/current/public; # I assume your app is located at this location

              location / {
                  proxy_pass http://deploy; # match the name of upstream directive which is defined above
                  proxy_set_header Host $host;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
              }

              location ~* ^/assets/ {
                  # Per RFC2616 - 1 year maximum expiry
                  expires 1y;
                  add_header Cache-Control public;
                          # Some browsers still send conditional-GET requests if there's a
                  # Last-Modified header or an ETag header even if they haven't
                  # reached the expiry date sent in the Expires header.
                  add_header Last-Modified "";
                  add_header ETag "";
                  break;
              }
       }
      }

這裡只需要注意的是

  • upstream中 server 要配置成你在puma中bind的 socket就行了
  • root要設定成你伺服器專案的根目錄,也就是puma.rb中的 directory

接下里只需要重啟nginx伺服器,整個rails的環境就搭建完成了


nginx -s reload

如果完成了配置後訪問站點是504,那麼可能是兩種情況,一是伺服器防火牆問題,二是rails環境金鑰的問題,請在使用passenger在Centos7部署nginx+Ruby on Rails中尋找答案

相關推薦

Ruby on Rails 終極部署方案 nginx+mina+puma

搭建工具介紹 Ruby on Rails作為一款十分優秀的web開發框架,在當前web領域中慢慢佔據了越來越重要,秉承rails快速開發的特點,很多快速部署rails的方案也越來越多。這篇文章中所選的方案是我個人認為十分優秀的部署方案。這套部署方案的結構是,nginx作為反向代理伺服器負責負載均衡,min

ruby on rails 專案部署(Nginx + Passenger)

由於最近用rails在做一個專案,以前似乎接觸過一些資訊說rails部署起來非常麻煩於是自己嘗試了一下,花了半個小時終於搞定了(Nginx+passenger)。成功執行出第一個自己部署的rails專案。下面就把自己的安裝部署中出現的問題以及可能出現的問題總結一下。   一

第二天,Ruby on Rails,Git版本控制,更新到遠端倉庫,部署

2018年11月30日 用Git做版本控制,我們暫時不用smartgit,就用本地的git。 在使用之前要做一些系統設定,設定你的使用者名稱和郵箱,這個設定只需一次 $ git config --global user.name "dongfangyier" $ git config -

記錄從零開始在Ubuntu 16.04 上部署Ruby on rails 專案

之前學習Java Web專案時,在亞馬遜雲(aws)上部署了一臺 Windows Server,最近在學習Ruby,跟著官網把小部落格寫完了,感覺挺好玩的,也想部署到這個上面,怎奈都說不建議或者直接讓放棄Windows,然後我又弄了一臺Ubuntu的伺服器,從零開始,也記錄

Mac上配置 Ruby on Rails和Git

code class gist pass ack nss exit ech https Ruby on Rails on Mac ====================================================================

ruby on rails模擬HTTP請求錯誤發生:end of file reached

ats ace post result tcs 後來 nec scu microsoft 在文章 Ruby On Rails中REST API使用演示樣例——基於雲平臺+雲服務打造自己的在線翻譯工具 中,利用ruby的Net::HTTP發起http請求訪問IBM Blu

打造適合Ruby on Rails的Sublime Text 3開發環境[原創]

ebr ins oda lan new alt mac 管理 nbsp 強大的 Package Control Package Control 是一個包管理工具,類似於 Homebrew, NPM, 用它來管理所有 ST 插件非常方便 安裝:https://package

Ruby on Rails,一對多關聯(One-to-Many)

用例 存在 BE details 一對一 擁有 class room 方法 在上一篇文章中,我們知道通過has_one和belongs_to方法定義一對一關聯關系。接下來是更常見的情況,一對多關聯。比如老師與所教課程的關系,一個老師負責多個課程。換成對象之間的關系就是:一個

各種環境下搭建ruby on rails開發環境

load pro entos RR 解壓 ins url 當前 bin win10上搭建raby on rails環境: 步驟如下 1.安裝ruby (我選擇的版本是ruby 2.2.3p173) 2.安裝rails gem 在這之前建議先把gem的源換成淘寶的源,速度快點

Ruby on Rails Installation(Learn Rails5.2)

brew rail div home xcod 使用 VM 想要 版本控制器 使用版本控制器的原因:你沒有系統根權限,所以你沒有別的選擇你想要分開運行幾個rails 系統 ,並且這幾個rails有不同的Ruby版本。使用RVM就可以輕松做到。沒有什麽新鮮的先安裝xcode,

ruby on rails environment setup

HR scrip rail CA UC node make install rst firstly ,we can install the latest version of ruby through ruby`s official site, $./configure

Ruby on Rails module 筆記

namespace ber for mes 使用 png nbsp class a esp Module Module的兩個功能 1. as namespace: 同c++。如下 module A   class C     ...   end end module B  

Ruby on Rails Scope 筆記

vat ces bsp ESS pri 補充 method scope 外部變量 Scope 首先補充一下ruby 中的import(和java對比), require_relative variables 對於method來說,outside variables的作

Ruby on rails class筆記

welcome fun 使用 .org def 例子 需要 www. functions class class method 有別於 object method, 類似於java中的static method. 使用class method 不需要通過object。 3種

ruby on rails 的 I18n問題

借用一本叫《web開發敏捷之道-應用Rails進行敏捷web開發》的入門書籍在學習ruby on rails的時候,在國際化那個章節我碰到一個問題,總是報關於I18n的錯誤,與書上的程式碼對照了半天發現並沒有什麼不同,於是折騰了半天,終於在網上找到了答案:Locale data should b

windows下安裝ruby on rails出現問題的解決辦法

windows平臺下用gem install rails會出現以下錯誤: 'websocket-driver' native gem requires installed build tools Please update your PATH to include build tool

Vulhub - Ruby On Rails 路徑穿越漏洞(CVE-2018-3760)復現

Ruby On Rails 路徑穿越漏洞(CVE-2018-3760) Ruby On Rails在開發環境下使用Sprockets作為靜態檔案伺服器,Ruby On Rails是著名Ruby Web開發框架,Sprockets是編譯及分發靜態資原始檔的Ruby庫。 Sprockets

mac os x 10.5.8上安裝ruby on rails

http://rubyforge.org/frs/download.php/56871/rake-0.8.7.gem   http://rubyforge.org/frs/download.php/64425/activerecord-2.2.3.gem   h

Vagrant和VirtualBox搭建基於windows的Ruby on Rails基本開發環境(共享資料夾配置)

搭建基本開發環境的主要步驟如下(結合實踐和眾多網上眾多教程): 1、搭建平臺:WIN10平臺+Vagrant+VirtualBox+Xshell 2、安裝步驟: (1)安裝VirtualBox虛擬機器模擬器(輕量級) 下載地址:https://www.virtualbox.org

Ruby On Rails視訊教程

Ruby On Rails視訊教程下載課程分享連結:https://pan.baidu.com/s/1odv5jJRf6Xc8Wh0-vCIXQg 密碼:x3me 什麼是Ruby On Rails?Ruby on Rails 是一個可以使你開發,部署,維護 web 應用程式變得簡單的框架。 講課內容簡介: