nginxとunicornをOS Xで自動起動にする
このエントリーで書いたレシピ食材検索アプリですが、自宅ネットワーク内のMac miniで、nginx+unicornで稼働させています。
permissionや所有者ではまると面倒なので、nginxもunicornもユーザ権限で動かしてます。nginxはport 8080、unicornはport 5000にしてます。妻にはURLのhttp://x.x.x.x:8080/をブックマークしてもらいました。
で、mac miniの再起動時に手動でnginxとunicornを起動させるのが面倒で、自動起動するようにしたので、やり方を記録しておきます。nginxとunicornの設定ファイル等もついでに記録。
#app_path/config/unicorn.rb
worker_processes 2 listen File.expand_path('tmp/sockets/unicorn.sock', ENV['RAILS_ROOT']) stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT']) stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT']) preload_app true before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{ server.config[:pid] }.oldbin" unless old_pid == server.pid begin Process.kill :QUIT, File.read(old_pid).to_i rescue Errno::ENOENT, Errno::ESRCH end end end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end
#/usr/local/etc/nginx/nginx.conf
user user_account;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream unicorn.rails_app{
server unix:/path/to/kkfoodstuff/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 8080;
server_name localhost;
location / {
root /path/to/kkfoodstuff/public;
index index.html index.htm;
try_files $uri/index.html $uri.html $uri @unicorn_rails_app;
}
location @unicorn_rails_app {
if (-f $request_filename) { break; }
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Server $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://unicorn.rails_app;
}
}
}nginxの自動起動は、brewでnginxをインストールすると、やり方を表示してくれるのでその通りにするだけで自動起動できます。
Docroot is: /usr/local/var/www
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
To have launchd start nginx at login:
ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
Then to load nginx now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
Or, if you don't want/need launchctl, you can just run:
nginxunicornはlocal.unicorn_rails.plistというファイル名でlaunchd.plistを用意しました。
#~Library/LaunchAgents/local.unicorn_rails.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>local.unicorn_rails</string> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> <key>WorkingDirectory</key> <string>/path/to/kkfoodstuff</string> <key>ProgramArguments</key> <array> <string>/Users/user_account/.rbenv/shims/bundle</string> <string>exec</string> <string>unicorn_rails</string> <string>-c</string> <string>/path/to/kkfoodstuff/config/unicorn.rb</string> <string>-E</string> <string>production</string> <string>-p</string> <string>5000</string> </array> </dict> </plist>
launchctrl コマンドでシステムにloadしておく。
$ launchctl load -w ~/Library/LaunchAgents/local.unicorn_rails.plist
参考