How to properly require any Ruby gem in an Apache CGI application?

540 Views Asked by At

My Ruby CGI scripts doesn't work when I require any gem. I would like to use the "peach" gem in my app, and it was installed by "gem install peach", however I installed it as root. My test app, called "gemtest":

#!/usr/bin/ruby

require "cgi"
require "peach"

cgi=CGI.new(:accept_charset => "UTF-8")

puts "Content type: text/plain; charset: 'UTF-8'"
puts
puts "It Works!"

It always trows an error in the apache error log:

/usr/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
cannot load such file -- peach
(
LoadError
)
\tfrom /usr/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
\tfrom /opt/lampp/cgi-bin/gemtest:6:in `<main>'
End of script output before headers: gemtest

I also tried to set up LOAD_PATH amd GEM_PATH environment variables in my Apache's httpd.conf file with directive "SetEnv", and I always restarted the Apache daemon:

SetEnv LOAD_PATH "/home/my_username/.gem/ruby/2.2.0/gems/"
SetEnv GEM_PATH "/home/my_username/.gem/ruby/2.2.0/gems/" 

But I had no success. How to require properly any gem when running simple CGI apps?

1

There are 1 best solutions below

6
On BEST ANSWER

The problem is that the user who is trying to load the gem, most probably doesn't have access to the gem installed for your username

Check the username that loads the apache service in your apache configuration, most probably its the nobody user, and install the gem for that user

sudo su - nobody
gem install peach

If you don't want to deal with different gems for different users, you can always use bundler