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):
httpd.conf
Set up the CGI directory with:
- The
+ExecCGI
option
- Access control that allows the visitors you want (
Require all granted
, for example)
- Set a handler for CGI scripts with
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
The script
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