Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamiew/327516 to your computer and use it in GitHub Desktop.
Save jamiew/327516 to your computer and use it in GitHub Desktop.

Switching from apache2+passenger to nginx+unicorn

Much of this will be deprecated (manual init.d installation, compiling from source, etc) but documenting for good measure --jmw

Compiling nginx from source

./configure --with-http_ssl_module --sbin-path=/usr/local/sbin/ sudo make install

=> bins get put in in /usr/local/sbin => conf/logs/etc live in /usr/local/nginx/{conf,logs,etc}

init.d scripts

in RAILS_ROOT/config/etc/ I've stashed the (custom) init.d scripts for both servers, which live in /etc/init.d

To have them launch at boot: $ update-rc.d nginx defaults $ update-rc.d unicorn defaults

TODO: add logrotate config files TODO: use some custom .deb packages, yeesh! TODO: clean up logging locations
nginx is currently going to /usr/local/nginx/logs/...

An alternate unicorn init.d I liek: http://gist.github.com/308216

running nginx/unicorn

Launch unicorn manually: $ unicorn_rails -c config/unicorn.rb -E production -D

  • OR - $ sudo invoke-rc.d unicorn start

Test nginx config: $ nginx -t

Launch nginx: $ nginx

  • OR - $ sudo invoke-rc.d nginx start

restarting unicorn

It's referred to as "upgrading" if you are sidel-ing in a new version of Rails -- Unicorn does a running restart, one worker at a time

To upgrade: send the unicorn master process a USR2 signal. Cake. $ PID=#{RAILS_ROOT}/tmp/pids/unicorn.pid; test -s "$PID" && kill -USR2 cat $PID

To actually restart, which causes a lag on 1st request while Rails re-loads: $ PID=#{RAILS_ROOT}/tmp/pids/unicorn.pid; test -s "$PID" && kill -USR cat $PID

For deployment with capistrano:

  • replace the 'deploy:restart' task to send USR2 (or better: invoke the init.d script)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment