Running Ruby via Apache
- #Ruby
- #Tips
- #Know-how
- 2018/06/16
I wanted to run Ruby with Apache
I wondered if I could execute Ruby files on Apache the same way I run PHP scripts, so I tried it out. No Rails here—just a connectivity check.
Environment
- Ruby 2.5.1
- Apache 2.4
- Windows 10
Procedure
I assume Apache and Ruby are already installed.
- Edit
httpd.conf. OpenC:\\Apache24\\conf\\httpd.confin a text editor. - Update directory settings. Around line 380 you will find:
<Directory "c:/Apache24/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
Comment that block out and add the following instead:
<Directory "c:/Apache24/htdocs">
AllowOverride All
Options +ExecCGI
Order allow,deny
Allow from all
Require all granted
</Directory>
- Enable the CGI handler. Around line 430 you will see:
#AddHandler cgi-script .cgi
Add the next line below it:
AddHandler cgi-script .cgi .rb
Now .rb files will be treated as CGI scripts.
- Place the Ruby file. Put your file under
C:\\Apache24\\htdocsand write it like this:
#!C:/Ruby25-x64/bin/ruby.exe
# Specify the Ruby executable above.
puts "Content-type: text/html\n\n" # Append \n\n or Apache will throw an error (check the logs).
puts "<html><head></head><body>Hello</body></html>"
Make sure the shebang points to your Ruby executable and that you output the Content-type header.
- Verify. Start Apache and access
http://localhost/filename.rb. If “Hello” prints, you are done!
Share:
X (Twitter)