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. No Comments
Tagged with , , , , , , . Written by: Mathias Stjernström

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. No Comments
Tagged with , , , , . Written by: Mathias Stjernström

A couple of days ago my Terminal.app (console) got a really annoying feature. When launching the console or when I created a new tab it got stuck for about 5-10 seconds before I got my prompt.

If you are like me and are spending most of your time in Terminal.app you understand how annoying this is/was.

I tried to rename .profile, .inputrc and evey other startup script I had in my home folder but the problem did not go away.

Then I found an OSX user which had his Terminal crash when started. He solved his problem by cleaning up some OSX systemlogs. So I thought I try the same trick.

The logfiles which he removed where the asl files under /private/var/log/asl/

I read about them here: http://macosx.com/tech-support/can-i-deleteasl-log/24223.html

But I figured I’d just remove them all :)

1
sudo rm -rf /private/var/log/asl/*

When all asl files where removed my Terminal went back to normal again and I am sooooooo relieved.

This worked for me BUT I do not recommend you to remove anything if you do not know what you are doing!!!

Cheers!

Posted in Operatingsystem, Utilities at September 11th, 2009. 2 Comments
Tagged with , , , , . Written by: Mathias Stjernström

When helping a friend last night with couchdb a got stuck when trying to start it as an unprivileged user. The error messages you get from couchdb is far from easy to understand.

This is my way of solving it.
Read More…

Posted in Databases, Hosting at April 10th, 2009. 2 Comments
Tagged with , , . Written by: Mathias Stjernström

The iPhone is a really great device. It lacks a few basic features that I misses from ericsson or nokia but overall it’s a really great device.

Where it really kicks ass is when you are out of town and need to find a location or restaurant. This have been great when I am still in the same country but earlier this week I when to Glasgow/scotland and the first thing you get is a welcome SMS from one of the local mobile network operators and here is when the problem arise.

Read More…

Posted in Life at April 10th, 2009. No Comments
Tagged with , , , , , . Written by: Mathias Stjernström

I do not usually blog about this tuff. Hell I do not blog much at all :) But I think this was pretty cool.

Internet is a virtual share-your-stuff-machine.
On YouTube you share you creativity.
On Facebook you share you life.
On Second Life you share your dreams.
On Wikipedia you share you knowledge.
On Ebay you share your belongings.

Translated from a swedish blogpost reacting to the new Swedish IPRED law (http://micco.se/2009/03/delning-blir-en-multimiljardindustri/)

A pretty weak google translation of the page

Posted in Life at April 2nd, 2009. No Comments
Tagged with , , , , , , . Written by: Mathias Stjernström

Today I had a really painful experience with rails InvalidAuthenticityToken. It turned out not to have anything to do with rails at all and there is where the painful part come in to play.

Read More…

Posted in Hosting, Programming, Rails at March 23rd, 2009. No Comments
Tagged with , , , . Written by: Mathias Stjernström

I generate a loooot of passwords. I try out a lot of webservices and all those needs passwords. I control about 70-100 servers and all those have different services and accounts. So I generate a lot of passwords… and this post is about how I do it do make it as easy as possible.
Read More…

Posted in Mouseless, Operatingsystem at January 13th, 2009. 2 Comments
Tagged with , , . Written by: Mathias Stjernström

Configure and compiling VIM under OS X did not work out-of-the box for me. I did not want to use MacPorts VIM as it’s dependent on ruby (if you like me want that) and I did not find a way of compiling MacPorts VIM against my local ruby.

This post is about how to get VIM compiling on OS X/intel without the use of MacPorts.

Read More…

Posted in Programming at January 11th, 2009. 4 Comments
Tagged with , , , . Written by: Mathias Stjernström

Just before christmas I worked with one of our customers new server cluster or cloud if you may.
This cluster has production nodes and staging nodes and this post is about how to get capistrano to play nice with both production and the staging environment.
Read More…

Posted in Hosting, Programming, Rails, Ruby at January 6th, 2009. 7 Comments
Tagged with , , , . Written by: Mathias Stjernström

pastbedti.me is using WP-Gravatar