Setting Up Ruby Cgi in Apache

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



Leave a reply



Submit