9

我有一些应用程序在 ruby​​ 1.9.2 上运行 rails 3,并使用 nginx + 乘客部署在 Ubuntu 10.04 LTS 机器上。现在,我需要添加一个在 ruby​​ 1.8.7 (REE) 和 Rails 2 上运行的新应用程序。我使用 RVM、Passenger Standalone 和反向代理完成了这项工作。

问题是,每次我必须重新启动服务器(例如安装安全更新)时,我都必须手动启动Passenger Standalone。

有没有办法自动启动它?我被告知要使用 Monit 或 God,但我无法编写适用于 Passenger Standalone 的正确配方。我在 God 和 RVM 方面也遇到了一些问题,所以如果你有一个不使用 God 的解决方案,或者如果你知道如何正确配置 God/Rvm,那就更好了。

4

4 回答 4

15

这是我的工作。使用 Upstart (Ubuntu 10.04) 启动乘客守护进程

我的环境使用 rvm 和 ruby​​ 1.9.2 和 apache,我的 rails 应用程序是通过 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

和 apache 配置:

<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 没有设置乘客依赖的 ENV['HOME'],所以我们必须在执行乘客命令时传递它。除此之外,它非常简单。

调试说明:https>> /tmp/upstart.log 2>&1 ://serverfault.com/questions/114052/logging-a-daemons-output-with-upstart(在脚本块的第二行附加类似的内容)

希望这可以帮助。

于 2011-04-20T14:08:08.777 回答
0

我使用宝石眼 - 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
于 2015-04-22T08:19:19.027 回答
0

我建议编写一个可以成功启动它的 shell 脚本,然后在 Monit 或 God 配方中使用它,如果你仍然愿意使用它们,或者如果不是,则使用 init 脚本。

你没有提到你的服务器运行什么操作系统,但如果它是最近的 Ubuntu,你可以很容易地编写一个 Upstart 脚本。它是内置的,并且具有 Monit/God 的重要特性 - 保持你的守护进程运行,即使重新启动也是如此。

于 2011-04-19T16:44:31.327 回答
-1

根据您的平台,您几乎肯定会有一些可用的 init 变体。在 debian 上,这是 init.d。在 ubuntu、init.d 或 upstart 上。在类似 RHEL 的服务上。这些将允许您在启动时管理服务。我建议阅读适合您平台的手册页。

于 2011-04-19T16:45:48.887 回答