-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Because using reverse proxy is not really efficient

I started using Sinatra because I wanted to replace the cgi scripts that I’m using to provide services on my other website dotnul.com as this is a real/easy way of implementing REST API and having a way of controlling eventual errors. Well that and also the fact that it’s faster, stronger and portable.

Installing prerequisites

To install the passenger, we need to have tools build it.

yum install -y httpd git gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel curl-devel httpd-devel

You could use the default ruby and rubygems of your system. But the default ruby version available on Mac OS Lion (I didn’t do the update to Mountain Lion now) is not the latest one and there’s no way of switching gem context easily when changing projects. Those two reasons are one of the valid reasons to use RVM.

The installation of RVM has to be done as a standard user with sudo rights, and SELinux has to be turned off.

# Install RVM for all users (sudo is mandatory, doing this as root won't work)
curl -L https://get.rvm.io | sudo bash -s stable
# Adding the current user to the RVM group
sudo usermod -a rvm user
# Install the latest Ruby
rvm install 1.9.3-p125
# Install sinatra gem
gem install sinatra
# Install passenger
gem install passenger
# Finally, put everything in place
passenger-install-apache2-module

Configuring Apache

Configure httpd to load the previously built passenger module (these outputed values hav been provided by the output of the last command):

cat > /etc/httpd/conf.d/passenger.conf << EOF
LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.14/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.14
PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.3-p194/ruby
EOF

This is the way of configuring your vhosts:

<VirtualHost *:80>
   ServerName www.yourhost.com
   # !!! Be sure to point DocumentRoot to 'public'!
   DocumentRoot /var/www/sinatraproject1/public
   <Directory /var/www/sinatraproject1/public>
      # This relaxes Apache security settings.
      AllowOverride all
      # MultiViews must be turned off.
      Options -MultiViews
   </Directory>
</VirtualHost>

Configuring your app

This is the way you should structure your app:

sinatraproject1/
├── app
│   └── main.rb
├── config.ru
├── logs
│   └── sinatra.log
└── public

And this is how you should create your rackup file:

require 'rubygems'
require 'sinatra'

log = File.new('logs/sinatra.log','a')
$stdout.reopen(log)
$stderr.reopen(log)

require File.expand_path('../app/main.rb', __FILE__)

run Sinatra::Application

If you want to try, here’s a basic Sinatra application, let the magic happen:

require 'sinatra'

get '/' do
	"hello world!"
end
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJW5eRJAAoJEBeKS2x6xuR73OkP+wYBSCvnD/3YvlC25cjt64R6
LzkMrIa7LFfJIbc9ShF88hmexgzEecsFbjv40egd12RTMwkbHD4zosSvt5F98bJF
T+LogkvVSOJGdSMWFicFCM3XS0dW5A7lllATLh6jdQrq0eDJQiSXfj+MaaitXI4S
P8PTsHUHidaKGm1U6jVba4WnwGdvpLUoMJq9GF8oEwhH/Y/ihspo3wyZCLP2uBV9
/PfV/IBZBhdXhaoobzK3nPDi1HC46nn5R7CcdPWvmViddYOH+6LCpciK8S6kAiaL
pm05NMrOV12za6uHR84Th3Pgbh4tlruod9SN8ZeBlwpOFTO5bsyTCqRgP6KBbA5m
SI5ft2QLkN7Dm2U22l8PPFAedORBT6cUHQk/bCuUtoaSDgGXRi7IW+f3Gx66ZL/Y
VBmBWivoqou9glFzf/LboxWXqZlxWX4x/WqB1HDizo28ZuqCXgPl2siJWTpSsZMU
oYh5zIVVRjRkvwRbNdYWS6Ue+KQpCJ1CImOIMJt24n2NOYA3gN1oQXbMr1akN9Rq
1fbPcqRawT3bvQWz6TLk5r1W+iURxHAWcl8eItbIjZvAxpf9wvGYlf65eqi9pdZr
dLvaSuE/673i2+iUrbfhcVPwCfCaONQN4hZwWzV3sg8FjHg6O4jcZqegr1ByKBuK
3OPBkeEBbeaXKowGxDzj
=QSvS
-----END PGP SIGNATURE-----

Hint: To validate signature, please view page source and copy html code between BEGIN PGP Signed message and END PGP Signature anchors.

Created the 2012-07-29

Share this


Article content

Resources

10 last blog posts

Related to this article

blog comments powered by Disqus