dp518158 2015-06-17 16:40
浏览 182

由于gem_original_require错误,无法从php运行ruby脚本

I am trying to run a ruby script from php (browser) that requires some gems. VPS: CentOS, PHP5, ruby2.0.0, using RVM. I reach the server via PUTTY and I ran the php code from browser.

So when I run this:

<?php
$descriptorspec = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("file", "./error-output.txt", "a")
);
$process = proc_open('ruby /home/balint/test.rb', $descriptorspec, $pipes);

if (is_resource($process)) {
    fwrite($pipes[0], 'hello world');
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    $return_value = proc_close($process);

    echo "command returned $return_value
";
}
?>

And the test.rb script includes only puts "xxx" then I get the output on my screen as xxx.

However, when I include some gems like require 'nokogiri' I get this in the error log:

/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- nokogiri (LoadError)
    from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /home/balint/test.rb:5

This is working fine directly from shell.

ruby -v:

ruby 2.0.0p643 (2015-02-25 revision 49749) [x86_64-linux]

which ruby:

/usr/bin/ruby

gem env:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.4.8
  - RUBY VERSION: 2.0.0 (2015-02-25 patchlevel 643) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/balint/.rvm/gems/ruby-2.0.0-p643
  - RUBY EXECUTABLE: /home/balint/.rvm/rubies/ruby-2.0.0-p643/bin/ruby
  - EXECUTABLE DIRECTORY: /home/balint/.rvm/gems/ruby-2.0.0-p643/bin
  - SPEC CACHE DIRECTORY: /home/balint/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /home/balint/.rvm/rubies/ruby-2.0.0-p643/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/balint/.rvm/gems/ruby-2.0.0-p643
     - /home/balint/.rvm/gems/ruby-2.0.0-p643@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["https://rubygems.org/", "http://rubygems.org"]
  - REMOTE SOURCES:
     - https://rubygems.org/
     - http://rubygems.org
  - SHELL PATH:
     - /usr/local/jdk/bin
     - /home/balint/.rvm/gems/ruby-2.0.0-p643/bin
     - /home/balint/.rvm/gems/ruby-2.0.0-p643@global/bin
     - /home/balint/.rvm/rubies/ruby-2.0.0-p643/bin
     - /home/balint/perl5/bin
     - /usr/local/bin
     - /bin
     - /usr/bin
     - /usr/local/sbin
     - /usr/sbin
     - /sbin
     - /home/balint/.rvm/bin
     - /usr/local/bin
     - /usr/X11R6/bin
     - /home/balint/.rvm/bin
     - /home/balint/bin

echo $PATH

/usr/local/jdk/bin:/home/balint/.rvm/gems/ruby-2.0.0-p643/bin:/home/balint/.rvm/gems/ruby-2.0.0-p643@global/bin:/home/balint/.rvm/rubies/ruby-2.0.0-p643/bin:/home/balint/perl5/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/balint/.rvm/bin:/usr/local/bin:/usr/X11R6/bin:/home/balint/.rvm/bin:/home/balint/bin

According to the log it's looking in the 1.8 folder. But there is no other folder in usr/lib/ruby/site_ruby/ than 1.8. And I also don't understand why it's working in shell and why it's not from the browser.

Can someone put me in the right direction here?

  • 写回答

1条回答 默认 最新

  • donglu5041 2015-06-17 17:11
    关注

    When PHP tries to run a script, it's very likely running with very reduced privileges and a limited env and PATH. Its path is different than yours, which you see in your shell.

    Check to see what the PHP session runs as. It is most likely the server's env. Also check to see whether the server knows about RVM, and can see the proper gem env. That would explain why the server sees /usr/lib/ruby/site_ruby/1.8/ and you see the RVM hosted Ruby.

    Also, unless the path of the server is able to find the right Ruby, using

    ruby /home/balint/test.rb
    

    it is going to launch the first Ruby found in the PATH, which will probably be /usr/bin/ruby. Instead ALWAYS force the path to the Ruby you want. For the built-in Ruby that'd be:

    /usr/bin/ruby /home/balint/test.rb
    

    Throwing RVM into the mix complicates things a bit, but RVM provides a shim which will honor the --default Ruby you specified or Ruby with gem sets. Read more about working with automated/shelled-out tasks in the RVM cron documentation.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?