Question

Ruby only shows script (doesn't run)

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

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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.