前回の続き
なにがしたいの
ディレクトリ構造
~~~/hogehoge |-- Gemfile |-- Gemfile.lock |-- app.rb |-- config.ru |-- unicorn.conf |-- unicorn.log |-- unicorn.pid |-- vendor `-- views
各ファイルの設定
unicorn.conf
worker_processes 2
listen '/tmp/unicorn_hogehoge.sock'
pid File.expand_path('unicorn.pid', File.dirname(__FILE__))
stderr_path File.expand_path('unicorn.log', File.dirname(__FILE__))
stdout_path File.expand_path('unicorn.log', File.dirname(__FILE__))
preload_app true
config.ru
require './app.rb'
if ENV['RAILS_RELATIVE_URL_ROOT']
map ENV['RAILS_RELATIVE_URL_ROOT'] do
run Sinatra::Application
end
else
run Sinatra::Application
end
nginx.conf
http{}の中に以下を追記
upstream unicorn_server_hogehoge {
server unix:/tmp/unicorn_hogehoge.sock;
}
sites-available/unicorn
server {
server_name app.nyaaz.info;
listen 80;
location /hogehoge/ {
proxy_pass http://unicorn_server_hogehoge;
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_set_header Host $host;
}
}
上で作った/etc/nginx/sites-available/unicornを/etc/nginx/sites-enabled/へシンボリックリンクを貼る
sudo ln -s /etc/nginx/sites-available/unicorn /etc/nginx/sites-enabled/
いざ起動
アプリのディレクトリに移動して
unicorn_rails -c unicorn.conf -E production --path /hogehoge -D
停止する場合
アプリのディレクトリに移動して
kill -QUIT `cat unicorn.pid`