markhorrocks markhorrocks - 6 months ago 17
Ruby Question

How to deploy a Rack-based Ruby application with Apache 2 and Passenger?

I have written a very simple "hello world" ruby script which I want to run under apache2 (2.4) and passenger on Ubuntu 14.04. This is not a rails app and I don't want to use the rails framework, this is just a single page application.

So far, all I can get is a directory listing. Here are my files.

uptime.rb

class Uptime
def call(env)
[200, {"Content-Type" => "text/plain"}, ["Hello world!"]]
end
end


Gemfile

source 'https://rubygems.org'
gem 'rack', '~>1.5.1'


config.ru

require './uptime'
run Uptime.new


/etc/apache2/mods-available/passenger.load

LoadModule passenger_module /usr/lib/apache2/modules/mod_passenger.so


/etc/apache2/mods-available/passenger.conf

<IfModule mod_passenger.c>
PassengerRoot /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini
PassengerDefaultRuby /home/mark/.rbenv/shims/ruby
</IfModule>


/etc/apache2/sites-available/000-default.conf

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

PassengerRuby /home/mark/.rbenv/versions/2.3.1/bin/ruby

<Directory /var/www/html>
PassengerEnabled on
Allow from all
Options -MultiViews
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

Answer

Passenger expects that your app has a public and a tmp directory:

  • The public directory has to be your DocumentRoot. It will be served by Apache if Passenger is disabled / not working, so nobody can see your application's source code.

  • The tmp directory is used for Passenger's restart.txt.

Your directory tree should look like this:

/var/www/html/
├── config.ru
├── uptime.rb
├── Gemfile
├── public/
└── tmp/

In your config:

<VirtualHost *:80>
    DocumentRoot /var/www/html/public
    # ...

    <Directory /var/www/html/public>
        # ...
    </Directory>

    # ...
</VirtualHost>