以root身份运行rails时出错

时间:2011-03-30 05:05:10

标签: ruby-on-rails ruby

我的rails应用程序在我的用户下运行良好:

$ rails server
=> Booting WEBrick
=> Rails 3.0.5 application starting in development on htttp://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-03-30 01:56:29] INFO  WEBrick 1.3.1
[2011-03-30 01:56:29] INFO  ruby 1.9.2 (2011-02-18) [x86_64-darwin10.7.0]
[2011-03-30 01:56:29] INFO  WEBrick::HTTPServer#start: pid=3899 port=3000

但是我需要在端口80下运行它,所以运行它作为sudo给了我这个:

$ sudo rails server -p 80
/Users/jamez/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:900:in `report_activate_error': Could not find RubyGem rails (>= 0) (Gem::LoadError)
    from /Users/jamez/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:248:in `activate'
    from /Users/jamez/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:1276:in `gem'
    from /Users/jamez/.rvm/gems/ruby-1.9.2-p180/bin/rails:18:in `<main>'

更多信息:

$ ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]

$ sudo ruby -v
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]

$ rails -v
Rails 3.0.5

$ sudo rails -v
/Users/jamez/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:900:in `report_activate_error': Could not find RubyGem rails (>= 0) (Gem::LoadError)
    from /Users/jamez/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:248:in `activate'
    from /Users/jamez/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:1276:in `gem'
    from /Users/jamez/.rvm/gems/ruby-1.9.2-p180/bin/rails:18:in `<main>'

$ gem list --local rails

*** LOCAL GEMS ***

rails (3.0.5)

$ sudo gem list --local rails

*** LOCAL GEMS ***

rails (3.0.5)

$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 1.6.2
  - RUBY VERSION: 1.9.2 (2011-02-18 patchlevel 180) [x86_64-darwin10.7.0]
  - INSTALLATION DIRECTORY: /Users/jamez/.rvm/gems/ruby-1.9.2-p180
  - RUBY EXECUTABLE: /Users/jamez/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/jamez/.rvm/gems/ruby-1.9.2-p180/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-10
  - GEM PATHS:
     - /Users/jamez/.rvm/gems/ruby-1.9.2-p180
     - /Users/jamez/.rvm/gems/ruby-1.9.2-p180@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

$ sudo gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 1.6.2
  - RUBY VERSION: 1.9.2 (2011-02-18 patchlevel 180) [x86_64-darwin10.7.0]
  - INSTALLATION DIRECTORY: /Users/jamez/.rvm/gems/ruby-1.9.2-p180
  - RUBY EXECUTABLE: /Users/jamez/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/jamez/.rvm/gems/ruby-1.9.2-p180/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-10
  - GEM PATHS:
     - /Users/jamez/.rvm/gems/ruby-1.9.2-p180
     - /Users/jamez/.rvm/gems/ruby-1.9.2-p180@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

知道发生了什么事吗? 如何在没有sudo的情况下使其工作?

2 个答案:

答案 0 :(得分:7)

RVM阻止sudo看到Rails二进制文件。

要解决此问题,请使用rvmsudo

> rails -v
Rails 2.3.11

> sudo rails -v
/Users/dan/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/site_ruby/1.8/rubygems.rb:779:in `report_activate_error': Could not find RubyGem rails (>= 0) (Gem::LoadError)
        from /Users/dan/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/site_ruby/1.8/rubygems.rb:214:in `activate'
        from /Users/dan/.rvm/rubies/ree-1.8.7-2010.02/lib/ruby/site_ruby/1.8/rubygems.rb:1082:in `gem'
        from /Users/dan/.rvm/gems/ree-1.8.7-2010.02@crm/bin/rails:18

> rvmsudo rails -v
Rails 2.3.11

答案 1 :(得分:2)

您正在使用Rails 3,它使用Bundler,它基于每个用户安装gem,因此在您以给定用户身份运行应用程序之前,必须首先以该用户身份登录时运行bundle install 。我认为这会让它对你有用。

顺便说一下,我假设您知道以root身份运行任何东西都是有风险的。您可能希望配置Apache和Passenger(或类似的东西)来通过端口80提供Rails应用程序。