Reputation: 897
I want to use Ruby in Apache through CGI. I have the following in my configuration file:
DocumentRoot /home/ceriak/ruby
<Directory /home/ceriak/ruby>
Options +ExecCGI
AddHandler cgi-script .rb
</Directory>
test.rb
is a testfile placed under /home/ceriak/ruby/
, #!/usr/bin/ruby
included on the first line and given executable permissions. Still, when I visit localhost/test.rb
I get a download window and can obtain the source code.
Interestingly, when I place the same script under /usr/lib/cgi-bin/
and call localhost/cgi-bin/test.rb
it works as supposed.
(Apache2 on Ubuntu 9.10.)
Any idea?
Upvotes: 18
Views: 9749
Reputation: 1273
To sum up all of the fine advice in these answers and your question itself (I had to do every single one of these things since I was starting from a blank slate):
Set up the CGI directory with:
+ExecCGI
optionRequire all granted
, for example)AddHandler
or SetHandler
(see note below)Example:
<Directory /home/ceriak/ruby>
Options +ExecCGI
AddHandler cgi-script .rb
Require all granted
</Directory>
Note: to use CGI without having to use a specific file extension like .rb
, you can use SetHandler
instead:
SetHandler cgi-script
Now everything in the directory will be treated as a CGI script, which is likely what you want anyway and you can leave the extensions off, which may look nicer and/or not inform visitors of the underlying technology: http://example.com/test
Finally, check that mod_cgi
is enabled (where ${modpath}
is correct for your system):
LoadModule cgi_module ${modpath}/mod_cgi.so
Don't forget to restart Apache after making your changes. On Slackware, for example, we do this:
$ sudo /etc/rc.d/rc.httpd restart
Don't forget the she-bang (#!
) to run the script with the Ruby interpreter.
Output a Content-type
, a newline, and then the body of your response:
#!/usr/bin/env ruby
puts "Content-type: text/html"
puts
puts "<html><body>Hello World!</body></html>"
Make sure the file is executable (by Apache!):
$ chmod +x /home/ceriak/ruby/test.rb
These two Apache documents are very helpful:
https://httpd.apache.org/docs/2.4/howto/cgi.html
https://httpd.apache.org/docs/current/mod/mod_cgi.html
Upvotes: 1
Reputation: 4850
Double check that mod_cgi is enabled; the default Yosemite http.conf has it disabled.
Upvotes: 1
Reputation: 2389
I ran in to the same situation and was able to fix it by adding the following line after AddHandler
:
Require all granted
Upvotes: 1
Reputation: 17960
Few things to check:
chmod +x /path/to/file
If you did all that, it should work fine. I have this as my test.rb file:
#!/usr/bin/env ruby
puts <<EOS
Content-type: text/html
<html><body>hi</body></html>
EOS
Upvotes: 7