Running .rb Ruby file - ruby

What is needed to run .rb file in a web browser?
(My first day in ruby)
I have Apache server (I'm coming from PHP) with passenger. Terminal proof:
gundars#linuxr528:~$ apache2ctl -t -D DUMP_MODULES
passenger_module (shared)
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
I also have installed rails etc, Tried to make a quick app with rails and it worked fine.
Now I would like to simply open .rb file in browser, and it is prompting me to save it.
This is how it looks in my browser, the window on right side pops-up after clicking, it asks where to save it.
Entries, concerning Ruby and this file, are:
apache2.conf:
LoadModule passenger_module /home/gundars/.rvm/gems/ruby-1.9.3-p0/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
PassengerRoot /home/gundars/.rvm/gems/ruby-1.9.3-p0/gems/passenger-3.0.11
PassengerRuby /home/gundars/.rvm/wrappers/ruby-1.9.3-p0/ruby
virtual-hosts.conf
<VirtualHost *:80>
ServerName tictactoe.ruby.dev
DocumentRoot "/localhost/sandbox/RUBY/tictactoe/"
ErrorLog /var/log/rubyonrails2.log
</VirtualHost>
/etc/hosts
127.0.0.1 tictactoe.ruby.dev
So the question - What is needed to run .rb file in a web browser?
I thought it's what passenger was for...

STEPS TO RUN .rb SCRIPTS FROM APACHE:
Edit your apache configuration file (e.g., /etc/httpd/conf/httpd.conf). Update or create an AddHandler setting so that it includes ".rb", e.g.:
AddHandler cgi-script .cgi .rb
Create your ruby script (.rb). Make sure you have a shebang (#!) pointing to your ruby interpreter. Use the "which" command if you're not sure where ruby is.
#!/bin/ruby
puts "Content-type: text/html"
puts
puts "<html>"
puts "<body>"
puts "<h3>Hello</h3>"
Set proper ownership and execute permissions (x) on your .rb file so apache can execute it, e.g.:
-rwxr-x--- 1 apache apache 163 Nov 26 10:55 index.rb
That might be all you have to do. If your script still doesn't execute, the first troubleshooting step I would try is temporarily disabling SELinux (using setenforce 0).

First day? Try a Sinatra tutorial. It's easy to get something up and running quickly and then you can concentrate on learning the language a bit.

Ruby is a server-side language. What you need is for a process to intercept your request, route it to the correct Ruby file, execute it, and send the output to your browser. Luckily, somebody has done this for you, with Rack and Passenger.
Why you would want to do this instead of working within the Rails framework is beyond me.

Related

Cannot get site loaded with Apache on Mac

I'm running El Capitan and trying to get my website hosted locally for development with Apache (that came with the OS). I followed the instructions here exactly. I made an anthony.conf file and put the Directory text in there, I replaced the actual directory with /Users/anthony/dev/web/unfinished-asteroids/ and then placed my web files in there. I started apache and navigated for localhost/~anthony but I get the
Not Found: The requested URL /~anthony was not found on this server.
standard 404 error. When I navigate to localhost it works fine and I get the "It works" from the index.html that is located in /Library/WebServer/Documents, I even changed that index.html file and it works fine.
Any ideas why I'm not able to see my website using apache? All I did was download this github repository and place it in my unfinished-asteroids folder, the index.html is at the root.
So it looks like there are a couple of extra steps that you need to do. There isn't really anything too special about apache that comes with OSX, so your standard apache configurations will work. but to get Userdir (~username) working on El Capitan,this is what I had to do
Edit /etc/apache2/httpd.conf
on (or near) line 166, uncomment
loadModule userdir_module libexec/apache2/mod_userdir.so
then on line 493 uncomment
Include /private/etc/apache2/extra/httpd-userdir.conf
then edit file /etc/apache2/extra/httpd-userdir.conf
uncomment line 16
Include /private/etc/apache2/users/*.conf
then in /etc/apache2/users/anthony.conf
<Directory "/Users/anthony/Sites/">
Options Indexes MultiViews
Require all granted
</Directory>
then sudo apachectl restart
That should make http://localhost/~anthony point to your sites directory.
Logs are stored in /var/log/apache2
Now if all you want is web access to say your dev directory you can do something like this.. (don't make any of the changes listed above)
in /etc/apache2/other/ create a file called mydev.conf (name doesn't really matter, save it needs to end in .conf')
and put this in that file
Alias /dev /Users/anthony/dev/
<Directory "/Users/anthony/dev">
Options Indexes MultiViews
Require all granted
</Directory>
and reload apache (sudo apachectl restart)
then you will be able to access your dev folder at http://localhost/dev and should be able to access your page at http://localhost/dev/web/unfinished-asteroids/
you can adjust the paths at will above, just need to make sure that the user or group _www has access to it, and that paths in the alias and the directory match.

Ruby Grape + Passenger + Apache

I am trying to setup by API server with
grape
passenger
apache2
I installed the gem "passenger" and ran
passenger-install-apache2-module
Placed the following in my "virtual host" configuration
ServerName api.website.com
LoadModule passenger_module /var/lib/gems/1.9.1/gems/passenger-5.0.6/buildout/apache2/mod_passenger.so
PassengerRoot /var/lib/gems/1.9.1/gems/passenger-5.0.6
PassengerDefaultRuby /usr/bin/ruby1.9.1
DocumentRoot /var/www/html/lib/api
But when I restarted the server and tried accessing it via the browser, I just see the folder listing.
What do I need to do different ?
I figured it out. I looked at how Sinatra was being deployed. I had to point the DocumentRoot to a folder "public" (or anything else). Although this folder had to exist in the same directory where my config.ru exists. And then all just worked.
My folder structure looks like this
/app
|- apis.rb
|- config.ru
\- public
virtualhost.conf
DocumentRoot ...../app/public
My solution seems to work. Although would love some explanation to it though :) Anyone has any insight into this ?
Thanks !
You have to point Passenger to a public dir because this allows it to first serve static files from that directory, and if no files were found request the app to process the request. This gives clear semantics for serving static files and performance gains.
request to /img.gif
public/img.gif exists => send it to user
----
request to /users.json
public/users.json doesnt exist =>
send request to app in public/..
So you have to point it to public dir even if you don't have anything in it

Simplest way to run ruby CGI app

What is the simplest way to run locally a Ruby CGI app
I am looking for a very simple e.g. 5 lines of code if possible way without using external servers such as NginX and Apache etc
EDIT:
To be more precise:
Given a simple Ruby script I would like to serve it as CGI, either by requiring a Gem within it or by using another tiny .rb script.
By serve as CGI I mean to be able to interact with it using my web browser
$ gem install cgiup
$ cgiup ruby_cgi_script.rb
If you're not married to a particular webserver and don't need a ton of volume, you can set up and run Sinatra with its standalone Webrick server in about five lines of code.
CGI proper will require more setup, however the Lighttpd web server is relatively simple to configure for Ruby CGI. The only things you have to add to lighttpd.conf are:
server.modules += ( "mod_cgi" )
cgi.assign = (
".rb" => "/usr/local/bin/ruby" # or whatever your path to Ruby is
)

"rackup config.ru" returns nothing?

I have my beginner Ruby application working perfectly on my local server using Rack and Sinatra. I'm in the process of getting it to work on my VPS webhost.
Here are my questions:
I tried running:
rackup config.ru
via SSH, and nothing happens, it just shows an empty line and I have to hit CNTRL+C to abort. Does anyone have ideas why?
If I run ruby main.rb, it successfully says Sinatra is taking the stage, but that's it, so I know you can't have it working with JUST that, but at least Sinatra is working properly.
I have a public_html folder. Is the main.rb and config.ru supposed to go in root "/" or is it supposed to go in public_html?
If it's the later, and I put a blank index.html inside public_html, will rackup know to 'replace' it with main.rb in / when it's running, and if it's not it just displays the index.html in public_html
When all is up and running, do I need to access the Ruby app via site.com:xxxx (xxxx=whatever port), or will I simply be able to go to site.com
You dont really need my code for main.rb, it just outputs "hello"
Here's config.ru:
require File.dirname(__FILE__) + "/main"
run Sinatra::Application
Q1, just rackup without config.ru
Q2, in general, the static folder name is public, not public_html unless you set it manually. other files, such as the main.rb, that is supposed to put into root directory, stay with the config.ru in the same folder if the code of main.rb as your given.
Q3, access by http://0.0.0.0:9292/ if you use the rackup by default setting to rack up the web server

Ruby app only works when Passenger is "disabled"

I have deployed a Sinatra application on an Ubuntu server using Apache and Passenger. Through some trial and error, I realize the app only works when the passenger module is disabled.
$ a2dismod passenger
After an Apache restart, the app runs as expected.
If I re-enable the module...
$ a2enmod passenger
...I see this warning upon Apache restart:
[warn] module passenger_module is already loaded, skipping
and the app stops working. Apache responds, serving the contents of the vhost's document root, but is not recognized by Passenger.
I'm glad my app works, but I'm not sure how to explain the reversed effect of enabling/disabling the passenger module.
I ran into the same problem: if you follow passenger installing instruction with this version of Apache you may actually tell Apache to load passenger twice.
Before adding the 3 famous lines to your Apache configuration file:
LoadModule passenger_module /usr/…/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.11
PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.2-p290/ruby
Check the configuration file apache2.conf for lines like these:
# Include module configuration:
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
If these lines do exists it means that on start your Apache server will include every *.conf and *.load file from /mods-available to /mods-enabled and than load them.
So if you run in the concerning warning message it means you are loading passenger module twice!
You can take advantage of this Apache feature going to /mods-available, looking for passenger.conf and passenger.load files and edit them instead of apache2.conf.
The ‘LoadModule passenger_module’ line goes into passenger.load, while the other 2 ‘PassengerRoot’ and ‘PassengerRuby’ lines go into passenger.conf.
Then restart your server and you’ll be fine.
More about this issue here: http://www.duccioarmenise.net/ruby-on-rails/warn-module-passenger_module-is-already-loaded/
This most likely means you've specified 'LoadModule passenger_module ...' twice. The first entry is somewhere not in passenger.conf, the second entry is in passenger.conf.

Resources