Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Ruby CGI in Apache

Tags:

ruby

apache

cgi

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?

like image 589
Joó Ádám Avatar asked Feb 11 '10 15:02

Joó Ádám


People also ask

Does Apache use CGI?

Apache will assume that every file in this directory is a CGI program, and will attempt to execute it, when that particular resource is requested by a client. The example shown is from your default httpd. conf configuration file, if you installed Apache in the default location.

How do I know if Apache is CGI enabled?

Enable CGI Module in Apache To enable CGI in your Apache server. you need to Load module file mod_cgi.so or mod_cgid.so in your Apache configuration file. The CentOS, Red Hat, Fedora and other rpm based distributions edit /etc/httpd/conf. modules.

How do I run a CGI script on a web server?

There are three ways to execute CGI programs: Specify a directory containing CGI programs in the ScriptAlias directive. Specify the cgi-script handler in the file extension by using the AddHandler directive. Specify the cgi-script handler in the SetHandler directive.


1 Answers

Few things to check:

  • is your file executable? You can make it executable by going chmod +x /path/to/file
  • did you output the correct Content-type?
  • is there a blank newline between your headers and your output?
  • did you restart Apache after setting the configuration?

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
like image 139
robbrit Avatar answered Sep 30 '22 13:09

robbrit