Ruby on Rails で作成したアプリケーションをNginxを使ってデプロイするための備忘録になります。文章にまとめるとスイスイやったように見えますが、実際にはエラーとの戦いでした。。
【参考にしたサイト】
https://github.com/heroku/heroku-buildpack-nginx
英語ですが、基本的にこのgithubに書いてある通りです。。が、英語だというのを差し引いてもやはり分かりにくい。他のサイトを何度も参照しながら読み進めていきました。git hub のどの部分に相当するのかわかるように、原文も引用しながら書きました。
【実際にやったこと】
① Heroku に新しいアプリケーションを作成
② buildpacks を追加
(github原文)
Existing App
Update Buildpacks to use the latest stable version of this buildpack:$ heroku buildpacks:add heroku-community/nginx
実際に記述したコードは下記。
$ heroku buildpacks:add heroku-community/nginx
③ Procfileを作成してfile内部に追記
(github原文)
Create Procfile
web: bin/start-nginx bundle exec unicorn -c config/unicorn.rb
Procfileをどこに作成するのか明記されていませんが、アプリケーション直下に作成するようです。
実際に記述したコードは下記。
web: bin/start-nginx bundle exec unicorn -c config/unicorn.rb
④ unicorn.rbファイルをを作成し中身を記載
(github原文)
Update Unicorn Configrequire 'fileutils'
listen '/tmp/nginx.socket'
before_fork do |server,worker|
FileUtils.touch('/tmp/app-initialized')
end
Configファイルがなかったので作成しようとしましたがどこに書くのか明記なし・・・原文のadd, commit の表記を読んでconfigディレクトリ内と判断。
(github原文)
$ git add config/unicorn.rb
$ git commit -m 'Update unicorn config to listen on NGINX socket.'
実際に記述したコードは下記。
require 'fileutils'
listen '/tmp/nginx.socket'
before_fork do |server,worker|
FileUtils.touch('/tmp/app-initialized')
end
⑤ gemファイルに下記を追加してunicornをインストール
gem 'unicorn'
gem 'unicorn-rails'
のあと $ bundle install を実行。
⑥ git add, git commit して git push heroku master
細かいコードの内容は私もまだまだ分からない部分がありますが、これでひとまずうまくいきました。。。ただ、冒頭に書いたように途中でエラーの嵐となりますので、次の記事でエラーの対処方法について記述しようと思います。
追記
今回、よく分かっていなかった用語がたくさん出てきたので公式ページでその意味を確認しておきました。
1 Heroku buildbacks
公式ページの記載はこちら
Herokuにアプリケーションをコンパイルするための拡張機能集と理解しています。
Heroku buildpack は、Heroku でアプリケーションをコンパイルするために使用するオープンソーススクリプトを集めたもので、Heroku の多言語プラットフォームのバックボーンを担っています。buildpack を使用すると、Heroku のビルドシステムを拡張して好みの開発言語やカスタム要素をサポートできるようにしたり、特殊なバイナリパッケージをランタイムで使えるようにしたりすることができます。Heroku buildpack があれば、アプリケーションや開発チームに最適な言語やフレームワークを自由に選んでコードを書くことができます。
2 Procfile
Heroku公式ページの記載はこちら
アプリが実行された時に実行されるコマンドなどを記述しているファイル。今回作成したファイルを見ると "satat-nginx" という記述から、サーバーにNginxを使うことなどが記載されているのが分かります。
(Heroku HPの記載)
Heroku apps include a Procfile that specifies the commands that are executed by the app on startup. You can use a Procfile to declare a variety of process types, including:
- Your app’s web server
- Multiple types of worker processes
- A singleton process, such as a clock
- Tasks to run before a new release is deployed
Each dyno in your app belongs to one of the declared process types, and it executes the startup command associated with that process type.
3 unicorn
githubに掲載されていたunicornの説明
httpサーバーですが、nginxと組み合わせて、Rubyのアプリケーションと組み合わせて使用することを想定しているようです。
(githubに記載されている原文の抜粋)
unicorn is an HTTP server for Rack applications designed to only serve
fast clients on low-latency, high-bandwidth connections and take
advantage of features in Unix/Unix-like kernels. Slow clients should
only be served by placing a reverse proxy capable of fully buffering
both the the request and response in between unicorn and slow clients.== Features
- Designed for Rack, Unix, fast clients, and ease-of-debugging. We cut out everything that is better supported by the operating system, {nginx}[https://nginx.org/] or {Rack}[https://rack.github.io/].