Apache2 doesn't execute .rb files (mod_ruby) - ruby

I want to make apache2 run ruby scripts. I managed to do this by using CGI, but now I want to go one step further and do it with mod_ruby. I installed mod_ruby through apt-get so it's supposed to be enabled, but when I visit http://localhost/test.rb my browser downloads the file instead of displaying it's output.
If I open /etc/apache2/mods-enabled/ruby.load there is a line containing the path do this mod.
I also added the following lined to my apache2.conf:
<IfModule mod_ruby.c>
<Directory /var/www/>
Options +ExecCGI
</Directory>
RubyRequire apache/ruby-run
#RubySafeLevel 0
<Files *.rb>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
<Files *.rbx>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>
</IfModule>
Can you please help me?
PS. Please don't suggest using ROR. I'm familiar with that. Now I just want to use ruby for educational reasons. Also please keep things simple I'm a total newbie to apache.

See this article - it has some instructions. Basically you need to tell Apache to pass your Ruby files through the handler.
Also, consider trying Sinatra - it's a micro web framework that's as simple as plain ruby files. And it runs with Passenger which is the well-documented Ruby module for Apache.

The article on HowtoForge covers almost the whole story, but I needed to add one line inside the <Directory /var/www> directive to prevent browsers from downloading the file:
AddType text/html .rb .rbx
Here are a couple more sites that I found helpful:
https://defuse.ca/blog/2012/07/how-to-install-mod_ruby-on-debian-squeeze/
http://modruby.net/en/doc/

Debian Stretch 9.
I tried installing mod_ruby with apt-get install libapache2-mod-ruby but there is no package by that name. In the end, all I needed to do was add this line to the apache-config to get the server to run my scripts.
AddHandler cgi-script .rb

Related

Apache - changing document root (Mac)

I'm just getting started using Apache on my Mac (El Capitan 10.11.13), but I'd like to change the document root. However, I'm not sure I fully understand how to do this, or if it is even possible to set it up the way that I would like to...
For starters, here's the snippet from my httpd.conf file (I haven't made any changes, this is stock)...
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options FollowSymLinks Multiviews
MultiviewsMatch Any
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
I would like to change the DocumentRoot to the folder where I generally keep my web development work, which is:
/Users/myname/Desktop/Stuff/Development
I know that the first step is to replace the DocumentRoot and Directory in the .conf file, but this doesn't work - I get the 403 forbidden error - and I'm not sure what to do after that.
My questions...
Is it possible to set the document root to a folder that is associated with a specific user - me, in this case? I'm the only user of my machine, and it would be much easier for me to do what I need to if Apache was pointed at that folder.
If so, what do I need to do other than change the first two lines from the snippet of my .conf file? Please be specific, as I'm relatively new to working with the terminal.
Thanks very much.
Yes you can't just change DocumentRoot without giving the permission to new path. If you must use it you have to add chmod +x to each path, in your case those are
User
myname
...
Development
It is not safe to do so. I would user a folder doesn't involved Users.

modify apache httpd.conf using ruby

Hey guys I'm trying to change in httpd.conf using ruby code 1.8.5 could you help me with it please
Apache configuration file The Apache configuration file must be configured specifically
for the server you have installed it on, i.e. the ServerName configuration item must be set to the IP address of the VM, not to 127.0.0.1 or any other generic address. TheServerAdmin configuration item must be set to cit470-sp2014-teamYOUR_TEAM_NUMBER#gmail.com. Create a gmail account with that name if you
have not done so already.
SSL All content must also be accessible using encrypted https versions of your URLs.
User directories Configure the server so that individual users can create their own web
sites by putting files in the public_html subdirectory of their home directory. These files should be available by both URLs of the form http://your_ip/~username and of the form http://your_ip/users/username. This may require that you create rewrite rules with mod_rewrite.
]# ./apconfigure.rb
./apconfigure.rb:8: warning: parenthesize argument(s) for future version
./apconfigure.rb:18: syntax error
.gsub(/ServerAdmin (.+)/, "ServerAdmin cit470su2015team2#gmail.com")
^
./apconfigure.rb:19: syntax error
.gsub(/UserDir disable/, "UserDir public_html")
^
./apconfigure.rb:20: syntax error
.gsub(userdir_regex, replacement) + "\nServerName 10.2.7.84 \nRewriteEngine on\nRewriteRule ^/users/(.*)$ /~$1 [PT]"
^
this is my code
#!/usr/bin/ruby
# This script edits the Apache configuration files and enables UserDir,
# as well as a custom RewriteRule
# path to apache config file
config_file = "/etc/httpd/conf/httpd.conf"
file = File.read(config _file)
# regex to uncomment the sample UserDir directives
userdir_regex = /#<Directory \/home\/\*\/public_html>.+?<\/Directory>/m
replacement = file.match(userdir_regex).to_s.gsub('#','')
# make some substitutions: change ServerName and ServerAdmin, enable UserDir,
# add rewrite rules.
edited_file =
file
.gsub(/ServerAdmin (.+)/, "ServerAdmin cit470su2015team2#gmail.com")
.gsub(/UserDir disable/, "UserDir public_html")
.gsub(userdir_regex, replacement) + "\nServerName 10.2.7.84 \nRewriteEngine on\nRewriteRule ^/users/(.*)$ /~$1 [PT]"
# write the changes to the file
File.open(config_file, 'w') do |f|
f.puts edited_file
end

Rails 3.2 xsendfile not working

I have to download static files which need authentication.
I am following http://makandracards.com/makandra/990-speed-up-file-downloads-with-rails-apache-and-x-sendfile and https://tn123.org/mod_xsendfile/
I have set config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache in production.rb
Controller:
send_file("#{Rails.root}/private/myfile.mp3",
:filename => "myfile.mp3",
type: "audio/mpeg",
:disposition => 'inline')
Apache configuration:
<VirtualHost _default_:80>
ServerName myserver.com
RequestHeader Set X-Sendfile-Type X-Sendfile
XSendFile On
XSendFilePath /sites/releases
RailsEnv production
</VirtualHost>
Even though, I put this, it is not sending any x-sendfile header.
It is strange but if I remove X-Sendfile On and X-SendfilePath, then it sets X-Sendfile header but the file does not play in browser.
I am trying this for three days, I have read everything available on google.
I have a feeling I'm missing something simple. If anyone has any suggestions, please let me
know!
Thanks.

Problems getting Apache running under Mac 10.9.1 Mavericks

all.
I am having ongoing problems getting Apache running under Mac 10.9.1 Mavericks (newest version).
I have apache running ("org.apache.httpd: Already loaded")
When I look at /etc/apache2/users/myfile.conf I see:
<Directory "/Users/rickeisner/Sites/">
Options Indexes Multiviews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
But when I run "apachectl configtest" to check the installation, I get:
[Tue Jan 21 12:21:51 2014] [warn] module php5_module is already loaded, skipping
Syntax error on line 4 of /private/etc/apache2/other/phpMyAdmin.conf:
order not allowed here
and when I look at /private/etc/apache2/other/phpMyAdmin.conf I see:
Alias /phpMyAdmin /usr/local/phpMyAdmin
Options Indexes
Order allow,deny
Allow from all
I can't make hide nor hair of this. Which is "line 4" anyway? "Options Indexes" or "Order allow,deny"? And what is wrong with this file?
I should add that I was only trying to get Apache/PHP/MySQL working on my computer, and couldn't get anything to work. Then, I began looking and found TOO MUCH INFORMATION. Everyone has slightly different versions of /etc/apache2/users/myfile.conf .
Please help me out of this confusion!
Rick

Ruby on Apache with mod_ruby

I really want to run some ruby code on Apache server. I've got libapache2-mod-ruby and libapache-ruby1.8 installed (complete list of what is installed is here). What do I do now to make it run (here`s an example)?
Here's a quick howto to get mod_ruby up and running. A short summary of the steps I used on Ubuntu 12.04 is replicated here:
Install mod_ruby for apache:
sudo apt-get install libapache2-mod-ruby
To enable mod_ruby for all files under /var/www, edit your apache config file, i.e. /etc/apache2/sites-enabled/000-default
I had to add +ExecCGI to Options, and add the section pertaining to Ruby:
<Directory /var/www/>
Options Indexes +ExecCGI
# Ruby with mod_ruby
RubyRequire apache/ruby-run
<Files *.rb>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
AddType text/html rb
</Files>
</Directory>
The main drawback that I see is that I can't seem to find a way to set the mime/content type from within the script. You have to set it globally for all ruby scripts with the AddType text/html rb directive.
Finally, you need a test script in /var/www and it must be executable. For example, I have /var/www/test.rb:
#!/usr/bin/ruby
puts "Hello World!<br><pre>"
ENV.each { |k,v|
puts "#{k}=#{v}"
}
puts "</pre>"
Don't forget to make it executable:
chmod a+x /var/www/test.rb
And this outputs:
Hello World!
HTTP_HOST=10.0.1.3
HTTP_CONNECTION=keep-alive
HTTP_CACHE_CONTROL=max-age=0
HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_USER_AGENT=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
HTTP_ACCEPT_ENCODING=gzip,deflate,sdch
HTTP_ACCEPT_LANGUAGE=en-US,en;q=0.8
HTTP_COOKIE=__test=1;
PATH=/bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin
SERVER_SIGNATURE=
Apache/2.2.22 (Ubuntu) Server at 10.0.1.3 Port 80
SERVER_SOFTWARE=Apache/2.2.22 (Ubuntu)
SERVER_NAME=10.0.1.3
SERVER_ADDR=10.0.1.3
SERVER_PORT=80
REMOTE_ADDR=10.0.1.3
DOCUMENT_ROOT=/var/www
SERVER_ADMIN=webmaster#localhost
SCRIPT_FILENAME=/var/www/test.rb
REMOTE_PORT=38188
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=GET
REQUEST_URI=/test.rb
SCRIPT_NAME=/test.rb
MOD_RUBY=mod_ruby/1.2.6
GATEWAY_INTERFACE=CGI-Ruby/1.1
I've run an Apache benchmark on mod_ruby vs. simple Ruby as CGI (which spins up the Ruby interpreter every request) and mod_ruby was about 8X faster and on par with PHP performance.
mod-ruby is not really the preferred way to go in the ruby community.
The easiest way for you to start it to install passenger (trough gem) and configure apache to use it.
It is really easy to do,
you can follow the official tutorial here: http://www.modrails.com/install.html

Resources