Sinatraのデブロイ設定(Nginx+Unicornで)
テストで作ったSinatraのアプリを、レンタルしているテスト用サーバのUbuntuで動かしてみました。
次回同じような事があった時に迷わないようにするためのに、やった事・覚えた事など書いておきます。
今回は動かすまでの記録です。
参考にさせていただいたサイトなど分かりやすくまとまっていて助かりました。
ありがとうございます。
参考にさせていただきました
Nginx
/etc/nginx辺りにあるconfファイルに設定します。
upstream unicorn {
server unix:[アプリまでのパス]/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
# Make site accessible from http://localhost/
server_name [運用するURL名];
access_log [アプリまでのパス]/logs/access.log;
error_log [アプリまでのパス]/logs/error.log;
root [アプリまでのパス]/public;
location / {
# ファイルが存在しなければunicornにproxyする
if (-f $request_filename) {
break;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://unicorn;
}
}
設定を反映させる
[shell]
$ sudo service nginx reload
[/shell]
Unicorn
Unicornで作ったアプリの直下にunicorn.rbという名前でファイルを作り編集します。
フォルダを作る
[shell]
$ mkdir -p tmp/{pids,sockets}
$ mkdir logs
[/shell]
unicorn.rb
@dir = "[アプリまでのパス]"
worker_processes 2
working_directory @dir
preload_app true
timeout 30
listen "#{@dir}/tmp/sockets/unicorn.sock", :backlog => 64
pid "#{@dir}/tmp/pids/unicorn.pid"
stderr_path "#{@dir}/logs/unicorn.stderr.log"
stdout_path "#{@dir}/logs/unicorn.stdout.log"
次にconfig.ruを作って編集します。
app.rbというファイルがメインのスクリプトになります。
config.ru
require './app.rb' run Sinatra::Application
起動
[shell]
$ bundle exec unicorn -c unicorn.rb -D
[/shell]
起動確認
[shell]
$ ps auxf | grep unicorn | grep -v grep
[/shell]
停止
[shell]
$ cat tmp/pids/unicorn.pid | xargs kill -QUIT
[/shell]
起動確認
Nginxのserver_nameで設定したURL名でアクセスしてみる。


