<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 7.5 專業部署方案 現在注冊頁面可以使用了,該把應用部署到生產環境了。雖然我們從[第 3 章](chapter3.html#mostly-static-pages)就開始部署了,但現在應用才真正有點用,所以借此機會我們要把部署過程變得更專業一些。具體而言,我們要在生產環境的應用中添加一個重要功能,保障注冊過程的安全性,還要把默認的 Web 服務器換成一個更適合在真實環境中使用的服務器。 為了部署,現在你應該把改動合并到 `master` 分支中: ``` $ git add -A $ git commit -m "Finish user signup" $ git checkout master $ git merge sign-up ``` ## 7.5.1 在生產環境中使用 SSL 在本章開發的注冊表單中提交數據注冊用戶時,用戶的名字、電子郵件地址和密碼會在網絡中傳輸,因此可能在途中被攔截。這是應用的重大潛在安全隱患,解決的方法是使用“安全套接層”(Secure Sockets Layer,簡稱 SSL),[[12](#fn-12)]在數據離開瀏覽器之前加密相關信息。我們可以只在注冊頁面啟用 SSL,不過整站啟用也容易實現。整站都啟用 SSL 后,[第 8 章](chapter8.html#log-in-log-out)實現的用戶登錄功能也能從中受益,而且還能防范 [8.4 節](chapter8.html#remember-me)討論的會話劫持。 啟用 SSL 很簡單,只要在生產環境的配置文件 `production.rb` 中去掉一行代碼的注釋即可。如[代碼清單 7.27](#listing-ssl-in-production) 所示,我們只需設置 `config` 變量,強制在生產環境中使用 SSL。 ##### 代碼清單 7.27:配置應用,在生產環境中使用 SSL config/environments/production.rb ``` Rails.application.configure do . . . # Force all access to the app over SSL, use Strict-Transport-Security, # and use secure cookies. config.force_ssl = true . . . end ``` 然后,我們要在遠程服務器中設置 SSL。這個過程包括為自己的域名購買和設置 SSL 證書,有很多工作要做。不過幸運的是,我們并不需要處理這些事,因為在 Heroku 中運行的應用(例如我們的演示應用),可以直接使用 Heroku 的 SSL 證書。所以,[7.5.2 節](#production-webserver)部署應用后,會自動啟用 SSL。(如果你想在自己的域名上使用 SSL,例如 `www.example.com`,參照 [Heroku 對 SSL 的說明](http://devcenter.heroku.com/articles/ssl)。) ## 7.5.2 生產環境中的 Web 服務器 啟用 SSL 后,我們要配置應用,讓它使用一個適合在生產環境中使用的 Web 服務器。默認情況下,Heroku 使用純 Ruby 實現的 WEBrick,這個服務器易于搭建,但不能很好地處理巨大流量。因此,[WEBrick 不適合在生產環境中使用](https://devcenter.heroku.com/articles/ruby-default-web-server),我們要[換用能處理大量請求的 Puma](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server)。 我們按照 [Heroku 文檔中的說明](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server),換用 Puma。第一步,在 `Gemfile` 中添加 `puma` gem,如[代碼清單 7.28](#listing-puma-gemfile) 所示。因為在本地不需要使用 Puma,所以我們把 `puma` 放在 `:production` 組中。 ##### 代碼清單 7.28:在 `Gemfile` 中添加 Puma ``` source 'https://rubygems.org' . . . group :production do gem 'pg', '0.17.1' gem 'rails_12factor', '0.0.2' gem 'puma', '2.11.1' end ``` 因為我們配置過 Bundler,不讓它安裝生產環境的 gem([3.1 節](chapter3.html#sample-app-setup)),所以[代碼清單 7.28](#listing-puma-gemfile) 不會在開發環境中安裝額外的 gem,不過我們還是要運行 Bundler,更新 `Gemfile.lock`: ``` $ bundle install ``` 下一步是創建文件 `config/puma.rb`,然后寫入[代碼清單 7.29](#listing-production-webserver-config) 中的內容。這段代碼直接摘自 [Heroku 的文檔](https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server),[[13](#fn-13)]沒必要理解它的意思。 ##### 代碼清單 7.29:生產環境所用 Web 服務器的配置文件 config/puma.rb ``` workers Integer(ENV['WEB_CONCURRENCY'] || 2) threads_count = Integer(ENV['MAX_THREADS'] || 5) threads threads_count, threads_count preload_app! rackup DefaultRackup port ENV['PORT'] || 3000 environment ENV['RACK_ENV'] || 'development' on_worker_boot do # Rails 4.1+ 專用的職程設置 # 詳情參見:https://devcenter.heroku.com/articles/ # deploying-rails-applications-with-the-puma-web-server#on-worker-boot ActiveRecord::Base.establish_connection end ``` 最后,我們要新建一個 `Procfile` 文件,告訴 Heroku 在生產環境運行一個 Puma 進程。這個文件的內容如[代碼清單 7.30](#listing-procfile) 所示。`Procfile` 文件和 `Gemfile` 文件一樣,應該放在應用的根目錄中。 ##### 代碼清單 7.30:創建 Puma 需要的 `Procfile` 文件 ./Procfile ``` web: bundle exec puma -C config/puma.rb ``` 生產環境的 Web 服務器配置好之后,我們可以提交并部署了:[[14](#fn-14)] ``` $ bundle exec rake test $ git add -A $ git commit -m "Use SSL and the Puma webserver in production" $ git push $ git push heroku $ heroku run rake db:migrate ``` 現在,注冊頁面可以在生產環境中使用了,注冊成功后顯示的頁面如[圖 7.24](#fig-signup-in-production)。注意圖中的地址欄,使用的是 `https://`,而且還有一個鎖狀圖標——表明啟用了 SSL。 ![signup in production 3rd edition](https://box.kancloud.cn/2016-05-11_5733304c83abb.png)圖 7.24:在生產環境中注冊 ## 7.5.3 Ruby 的版本 部署到 Heroku 時,可能會看到類似下面的提醒消息: ``` ###### WARNING: You have not declared a Ruby version in your Gemfile. To set your Ruby version add this line to your Gemfile: ruby '2.1.5' ``` 經驗表明,對本書面向的讀者來說,明確指定 Ruby 的版本號要做很多額外工作,得不償失,[[15](#fn-15)]所以現在你應該忽略這個提醒。為了讓演示應用和系統中的 Ruby 版本保持最新,會遇到很多問題,而且不同的版本之間沒有太大的差異。不過要記住,如果想在 Heroku 中運行重要的應用,建議在 `Gemfile` 中明確指定 Ruby 版本號,盡量減少開發環境和生產環境之間的差異。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看