Question

Ruby only shows script (doesn't run)

Posted June 25, 2015 3k views
Apache Ruby Sinatra

I am a complete noob and I’m sure I’ve done something wrong, but I’m at a loss in how to make it work. I’ve written a basic “Hello World” script and put it in my /var/www/html/sites directory on Centos. I’ve followed the DO tutorial to install apache, passenger, and sinatra. Tutorial. When I go to my webpage using the ip address I get the apache welcome screen as I should. When I go to http://ip/ruby_script.rb, I get the contents of the script. I can’t seem to get the script to actually run. The script requires sinatra, so I run “ruby script.rb” at the command line before I try and hit the web page so that sinatra is running. I’m sure I’m missing a fundamental step because everything I read seems to indicate that this is a simple process. If you need more specifics to help me, please let me know. I’m feeling a little stupid because I think this should be very simple. BTW, this is the simple script I’m trying to run.

#!/usr/bin/ruby

require 'twilio-ruby'
require 'sinatra'

get '/' do
    'Hello World! Currently running version ' + Twilio::VERSION + \
        ' of the twilio-ruby library.'
end
edited by asb

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

1 answer

The Sinatra script should not be located directly in the web directory. You’ll also need a config.ru file that describes how Passanger should run the programs. You file layout should look something like:

# tree /var/www/html/
/var/www/html/
├── app.rb
├── config.ru
├── public
└── tmp

A basic config.ru would look like:

require 'rubygems'
require 'sinatra'
require File.expand_path '../app.rb', __FILE__

run Sinatra::Application

And your Apache configuration should look similar to the following. It’s important to note that the path to your passenger_module might be different. When you ran the passenger-install-apache2-module it should have provided you with the correct value.

<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www/html/public

    LoadModule passenger_module /usr/local/rvm/gems/ruby-2.2.1/gems/passenger-5.0.28/buildout/apache2/mod_passenger.so
    <IfModule mod_passenger.c>
        PassengerRoot /usr/local/rvm/gems/ruby-2.2.1/gems/passenger-5.0.28
        PassengerDefaultRuby /usr/local/rvm/gems/ruby-2.2.1/wrappers/ruby
    </IfModule>

    <Directory /var/www/html/public>
        Require all granted
        Allow from all
        Options -MultiViews
    </Directory>
</VirtualHost>

Remember to restart Apache after making configuration changes:

  • sudo service apache2 restart

For more information on using Sinatra with Passenger and Apache, see Sinatra Recipes.

Submit an Answer