ruby - 使用 Apache 托管 Ruby gem

标签 ruby apache rubygems bundler

我无法弄清楚如何使用 Apache 托管我自己创建的 Ruby gem。我想知道是否有人可以帮我解决这个问题。

这是我到目前为止所做的事情。我首先创建自己的“hello world” gem(通过关注 these instructions ):

$ bundle gem helloearth

$ cat lib/helloearth.rb
require "helloearth/version"

module Helloearth
  def self.hi
    puts "hello world"
  end
end

$ cat helloearth.gemspec 
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'helloearth/version'

Gem::Specification.new do |spec|
  spec.name          = "helloearth"
  spec.version       = Helloearth::VERSION
  spec.authors       = ["Some Guy"]
  spec.email         = ["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="15667a787072606c55706d74786579703b767a78" rel="noreferrer noopener nofollow">[email protected]</a>"]
  spec.description   = %q{a gem description}
  spec.summary       = %q{a gem summary}
  spec.homepage      = "http://www.example.com"
  spec.license       = "MIT"

  spec.files         = `git ls-files`.split($/)
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
  spec.require_paths = ["lib"]

  spec.add_development_dependency "bundler", "~> 1.3"
  spec.add_development_dependency "rake"
end

然后我构建了 gem,安装并运行它以验证它是否有效:

$ gem build helloearth.gemspec
  Successfully built RubyGem
  Name: helloearth
  Version: 0.0.1
  File: helloearth-0.0.1.gem

$ gem install ./helloearth-0.0.1.gem 
Successfully installed helloearth-0.0.1
1 gem installed
Installing ri documentation for helloearth-0.0.1...
Installing RDoc documentation for helloearth-0.0.1...

$ irb
1.9.3-p362 :001 > require 'helloearth'
 => true 
1.9.3-p362 :002 > Helloearth.hi
hello world
 => nil

它有效!现在我要卸载我的“hello world”gem(通过清空整个gemset),然后从服务器重新安装它:

$ rvm gemset empty currentgemset
Are you SURE you wish to remove the installed gems for gemset 'ruby-1.9.3-p362@currentgemset' (/home/joe/.rvm/gems/ruby-1.9.3-p362@currentgemset)?
(anything other than 'yes' will cancel) > yes

我需要设置 Apache 来托管这个 gem。我按照 http://docs.rubygems.org/read/chapter/18#page81 中的说明执行此操作。 :

$ sudo mkdir /var/www/gems
$ sudo cp helloearth-0.0.1.gem /var/www/gems
$ rvmsudo gem generate_index -d /var/www/gems/
Warning: can not check `/etc/sudoers` for `secure_path`, falling back to call via `/usr/bin/env`, this breaks rules from `/etc/sudoers`. export rvmsudo_secure_path=1 to avoid the warning.Generating Marshal quick index gemspecs for 0 gems

Complete
Generated Marshal quick index gemspecs: 0.000s
Generating Marshal master index
Generated Marshal master index: 0.000s
Generating specs index
Generated specs index: 0.000s
Generating latest specs index
Generated latest specs index: 0.000s
Generating prerelease specs index
Generated prerelease specs index: 0.000s
Compressing indicies
Compressed indicies: 0.001s

$ ls /var/www/gems
helloearth-0.0.1.gem  Marshal.4.8.Z            specs.4.8
latest_specs.4.8      prerelease_specs.4.8     specs.4.8.gz
latest_specs.4.8.gz   prerelease_specs.4.8.gz
Marshal.4.8           quick

Apache 现在正在托管 gem,我可以使用我的 Web 浏览器在 localhost/gems 成功访问该目录。但是,我无法从 Apache 服务器安装我的 gem:

$ gem sources -a http://localhost/gems/
http://localhost/gems/ added to sources

$ gem sources
*** CURRENT SOURCES ***

http://rubygems.org/
http://localhost/gems/

cd ~ # leave the directory which holds the local copy of the gem to force use of the Apache server

$ gem install helloearth
ERROR:  Could not find a valid gem 'helloearth' (>= 0) in any repository
ERROR:  Possible alternatives: hello_bar, hello_ext, hellogr, helloh, hellolorem

我还尝试添加 127.0.0.1/gems/和 0.0.0.0/gems/作为 gem 源,但都没有解决问题。有人告诉我在对 $ gemgenerate_index 的调用中添加 --legacy 标志,但这也不起作用。

最佳答案

helloearth-0.0.1.gem 不在 gemgenerate_index 的正确位置。

如果您希望源为/gems,则 .gem 文件需要位于/var/www/gems/gems

gemgenerate_index -d 调用位于父目录 (BASEDIR) 上,.gem 文件位于/gems 子目录中。

http://docs.rubygems.org/read/chapter/18 "How do I run a Gem Server like RubyForge?"

关于ruby - 使用 Apache 托管 Ruby gem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15816725/

相关文章:

rubygems - 从 ruby​​ gems 渲染部分 View

rubygems - Cocoapods 错误:执行 gem 时...(Errno::ENOTSUP)

ruby-on-rails - nokogiri:生产环境中的重定位错误

ruby - 使用客户端证书和 TLS_RSA_WITH_AES_256_CBC_SHA256 密码套件

ruby-on-rails - 被 RHEL 6.4 上的 OpenShift 难住了

css - 在 html 类 =""属性中插入 ruby

apache - 如何使 yii2 模块在根级别加载

php - 用户 "apache"应该拥有我的 SQLite 数据库吗?

ruby - 使用 block 在父作用域中设置变量

python - 在 Windows 上使用 Mod_WSGI 的 Flask 应用程序将无法运行 - 现在 URLS 将无法工作