How to properly require any Ruby gem in an Apache CGI application?
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
How to use Ruby CGI scripts on Uberspace
First, note that Uberspace 7 runs on SELinux. This means that CGI script files in ~/html/
not only have to be executable but also need the correct SELinux context. In this case, the type must be httpd_sys_content_t
.
You can view the SELinux context with ls -lZ
:
$ ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
If some files have the wrong context, the context can be restored with the restorecon
command, e.g. restorecon -R ~/html/
.
The user installation directory for Ruby gems is ~/.gem/
. On Uberspace, gem install
installs into that directory by default:
$ cat /etc/gemrc
gem: --no-document --user-install
As the home directory cannot be accessed by the apache process, gems installed there cannot be executed from CGI scripts. You can install gems in /var/www/virtual/$USER/gem
instead, create the directory with
$ mkdir /var/www/virtual/$USER/gem
You cannot use the --install-dir
parameter for gem install
directly as this conflicts with the default parameters mentioned above:
$ gem install mygem --install-dir /var/www/virtual/$USER/gem
ERROR: Use --install-dir or --user-install but not both
Instead, create ~/.gemrc
with the following content to override the default parameters (replace <USERNAME>
with your actual user name):
gem: --install-dir /var/www/virtual/<USERNAME>/gem
Now the installation of gems should work:
$ gem install mygem
To use the gems in CGI scripts, set the Gem.paths
variable before requiring gems:
#!/usr/bin/ruby
Gem.paths = { 'GEM_PATH' => '/var/www/virtual/<USERNAME>/gem' }
require 'mygem'
(... rest of the script)
This is needed as we cannot modify the environment variables (i.e. set GEM_PATH
) for the apache process.
cgi script can't write to world writable file under apache
SELinux was blocking the file writes. "setenforce Permissive" allowed them to go through. Edited /etc/sysconfig/selinux and rebooted to make permanent.
Related Topics
Rails - Get the Time Difference in Hours, Minutes and Seconds
Rails Strip Non Numeric Values Before Save
Rails 4 + Devise: Invalid Route Name, Already in Use
How to Add a Primary Key to a Table in Rails
How to Deal with Not Knowing What Exceptions Can Be Raised by a Library Method in Ruby
How to Set/Get Session Vars in a Rack App
How to Validate a Non-Model Form in Rails 3
Private Class (Not Class Method) in a Ruby Module
How to Append a String to a Variable That Either Exists or Not
How to Resolve Ruby Error (Missing Psych)
How to Fix Hanging Popen3 in Ruby
Ruby Find String in File and Print Result
-%>' (Minus Sign) at the End of a Erb Sequence
Fail VS. Raise in Ruby:Should We Really Believe the Style Guide
What's the Difference Between the Build and Create Methods in Factorygirl
How to Include Variables in My Vagrantfile
How to Add MAC-Specific Gems to Bundle on MAC But Not on Linux