【问题标题】:How can I keep a Passenger Standalone up even after a restart?即使在重新启动后,我如何才能让乘客独立运行?
【发布时间】:2011-03-30 17:31:45
【问题描述】:

我有一些应用程序在 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,那就更好了。

【问题讨论】:

    标签: ruby-on-rails nginx passenger monit god


    【解决方案1】:

    这是我的工作。使用 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://serverfault.com/questions/114052/logging-a-daemons-output-with-upstart(将 &gt;&gt; /tmp/upstart.log 2&gt;&amp;1 之类的内容附加到脚本块的第二行)

    希望这会有所帮助。

    【讨论】:

    • 不错。这就是我一直在寻找的。​​span>
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      我用宝石眼-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
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 我正在使用 init.d 脚本来启动主 nginx。不过,Passenger Standalone 在另一个 nginx 实例上运行。要独立启动乘客,我需要将 ruby​​ 版本更改为 ree,运行 passenger start。我怎样才能编写一个 init.d 脚本来做到这一点?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-05
        • 1970-01-01
        • 2017-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-11
        相关资源
        最近更新 更多