My Sinatra + Apache + Passenger setup did not just run :)

Deploying a simple Sinatra rack application under Apache and Phusion Passenger turned out not work out of the box for me.

I already had a virtual host so I decided to deploy with passengers sub URI (sub URI documentation).

To make this work you’ll need Apache installed with Phusion Passenger setup (Passenger installation)

This is what I started with

This is my folders for the already existing website.

1
2
3
4
# Existing homedir
/home/my_web/
# Current webroot
/home/my_web/htdocs

Creating folders and files for Sinatra

The first thing you want to do is to create folders and files for Sinatra.

1
mkdir /home/my_web/sinatra
1
2
3
4
5
6
cd /home/my_web/sinatra
mkdir tmp
mkdir logs
mkdir public
touch app.rb
touch config.ru

Create symlink

Create a symbolic link from our current webroot to the new Sinatra app so that apache/passenger can find it.

1
ln -s /home/my_web/sinatra/public /home/my_web/htdocs/app

Creating the application

We also need a Sinatra application for this to work so lets fill those files.

File: config.ru

1
2
3
4
5
6
7
8
9
10
11
require 'app'

set :environment, ENV['RACK_ENV'].to_sym
set :app_file,     'app.rb'
disable :run

log = File.new("logs/sinatra.log", "a")
STDOUT.reopen(log)
STDERR.reopen(log)

run Sinatra::Application

File: app.rb

1
2
3
4
5
6
7
8
9
10
11
require 'rubygems'
require 'sinatra'

before do
  # Strip the last / from the path
  request.env['PATH_INFO'].gsub!(/\/$/, '')
end

get '' do
   "Hello world"
end

Apache configuration

Now we need to setup apache so that it can find our Sinatra app and load it.

The only thing you need to do is add the following line to your existing VirtualHost block:

1
RackBaseURI /app

This line will tell passenger to look into our existing webroot for a symlink thats named app and which points to our rack app. (RackBaseURI documentation)

Virtual host entry

1
2
3
4
5
6
7
8
9
10
11
12
<VirtualHost *:80>
  ServerName www.example.com
  ServerAlias example.com
  DocumentRoot /home/my_web/htdocs/
  RackBaseURI /app

  <Directory "/home/my_web/htdocs/">
    Order allow,deny
    Allow from all
    AllowOverride All
  </Directory>
</VirtualHost>

The best part is that you are allowed to specify this option multiple times. So you can easily setup multiple apps under the same virtual host.

Cheers!

Posted in Hosting, Programming, Sinatra at November 30th, 2009. 3 Comments
Tagged with , , , , , , . Written by:

Recently I’ve been working a lot with dates and tonight I had a hard time finding information about how to group a model by date when the model only have a timestamp column.

Lets pretend we have a user model with a created_at attribute which is stored as a timestamp.

If we want to plot a simple graph showing signups per date this is the way to extract them:

1
User.count(:group => "DATE(created_at)")

Thats all there is to it. From this you will get an OrderedHash containing the date and count of users for each date.

One small thing to remember if you are using PostgreSQL. You will need the DATE() function in any ORDER/SELECT statements because PostgreSQL will only select/order by fields thats in the GROUP BY statement and this is a pretty healthy behavior that many databases (no names here) don’t care about. Think about it. If you are grouping all users by date and use * as selector how would the database know which username or email address to display?

This is what happens if you try to group by created_at without DATE() ->

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>> User.count(:order => 'created_at DESC', :group => ["DATE(created_at)"])                                                
ActiveRecord::StatementInvalid: PGError: ERROR:  column "users.created_at" must appear in the GROUP BY clause or be used in an aggregate function
: SELECT count(*) AS count_all, DATE(created_at) AS date_created_at FROM "users"  GROUP BY DATE(created_at)  ORDER BY created_at DESC
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:507:in `
execute'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:985:in `select_raw'

        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:972:in `select'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `
select_all_without_query_cache'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'

        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:255:in `execute_grouped_calculation'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:132:in `
calculate'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:130:in `catch'

        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:130:in `calculate'
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:48:in `
count'
        from (irb):18

And this is the right way to do it:

1
2
>> User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"])
=> #<OrderedHash {"2009-10-14"=>22, "2009-10-25"=>4, "2009-11-04"=>8, "2009-10-15"=>9, "2009-10-26"=>16, "2009-11-05"=>9, "2009-10-16"=>193, "2009-10-27"=>14, "2009-11-06"=>9, "2009-10-17"=>49, "2009-10-28"=>15, "2009-11-07"=>6, "2009-10-18"=>36, "2009-10-29"=>8, "2009-10-19"=>116, "2009-10-30"=>15>

As you may notice the Hash does not look ordered. But if you loop over it you will get them in order:

1
2
3
4
5
6
7
8
9
10
11
12
>> User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"]).each {|u| puts "#{u[0]} -> #{u[1]}" }
2009-11-07 -> 6
2009-11-06 -> 9
2009-11-05 -> 9
2009-11-04 -> 8
2009-11-03 -> 14
2009-11-02 -> 20
2009-11-01 -> 10
2009-10-31 -> 6
2009-10-30 -> 15
2009-10-29 -> 8
..

And thats how you group a timestamp field with just its date part.

Cheers!

Posted in Databases, PostgreSQL, Rails, Ruby at November 8th, 2009. 3 Comments
Tagged with , , , , . Written by:

It's past my bedtime is using WP-Gravatar