How can I keep a Passenger Standalone up even after a restart?
I have a few apps running rails 3 on ruby 1.9.2 and deployed on a Ubuntu 10.04 LTS machine using nginx + passenger. Now, I need to add a new app that runs on ruby 1.8.7 (REE) and Rails 2. I accomplished to do that with RVM, Passenger Standalone and a reverse proxy.
The problem is that, every time I have to restart the server (to install security updates for example), I have to s开发者_如何学JAVAtart Passenger Standalone manually.
Is there a way to start it automatically? I was told to use Monit or God, but I couldn't be able to write a proper recipe that works with Passenger Standalone. I also had a few problems with God and RVM, so if you have a solution that doesn't use God, or if you know how to configure God/Rvm properly, it's even better.
Here is what I got working. Using Upstart (Ubuntu 10.04) to start the passenger daemon
My environment uses rvm with ruby 1.9.2 and apache and my rails app is deployed via capistrano
# Upstart: /etc/init/service_name.conf
description "start passenger stand-alone"
author "Me <me@myself.am>"
# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn
# When to start the service
start on started mysql
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
# Essentially lets upstart know the process will detach itself to the background
expect fork
# Run before process
pre-start script
end script
# Start the process
script
cd /var/path/to/app/staging/current
sh HOME=/home/deploy /usr/local/rvm/gems/ruby-1.9.2-p136@appname/gems/passenger-3.0.7/bin/passenger start --user 'deploy' -p '5000' -a '127.0.0.1' -e 'production'
end script
and the apache config:
<VirtualHost *:80>
ServerName myapp.com
PassengerEnabled off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
</VirtualHost>
Upstart doesn't set ENV['HOME'] which passenger relies on, so we have to pass that when executing the passenger command. Other than that its pretty straight forward.
A note for debugging: https://serverfault.com/questions/114052/logging-a-daemons-output-with-upstart (append something like >> /tmp/upstart.log 2>&1
to the second line in the script block)
Hope this helps.
I would suggest writing a shell script that can start this successfully, then use that in either a Monit or God recipe, if you're still willing to use them, or an init script if not.
You don't mention what OS your server runs, but if it's a recent Ubuntu, you can write an Upstart script pretty easily. It's built-in, and has the important feature of Monit/God - keeping your daemon running, even through restarts.
I use gem eye - https://github.com/kostya/eye
BUNDLE = 'bundle'
RAILS_ENV = 'production'
ROOT = File.expand_path(File.dirname(__FILE__))
Eye.config do
logger "#{ROOT}/log/eye.log"
end
Eye.application :app do
env 'RAILS_ENV' => RAILS_ENV
working_dir ROOT
trigger :flapping, :times => 10, :within => 1.minute
process :passenger do
daemonize true
pid_file "tmp/pids/passenger.pid"
start_command "#{BUNDLE} exec passenger start --port 3000 --environment #{RAILS_ENV}"
stop_signals [:TERM, 5.seconds, :KILL]
restart_command "kill -USR2 {PID}"
restart_grace 10.seconds
check :cpu, :every => 30, :below => 80, :times => 3
check :memory, :every => 30, :below => 70.megabytes, :times => [3,5]
end
process :delayed_job do
pid_file "tmp/pids/delayed_job.pid"
stdall "log/delayed_job.log"
daemonize true
stop_signals [:INT, 30.seconds, :TERM, 10.seconds, :KILL]
restart_grace 10.seconds
start_command "#{BUNDLE} exec rake jobs:work"
end
end
Depending on your platform, you will almost certainly have some variant of init available. On debian, this is init.d. On ubuntu, init.d or upstart. on a RHEL-like, service. These will allow you to manage services on startup. I suggest reading the appropriate man page for your platform.
精彩评论