Dancer in Shared Hosting

Warning - Old Content

This post is quite old, and it might not apply anymore, or maybe there's a better way to do the same thing nowadays. Take with a big grain of salt.

The current(edit: not anymore) version of my website runs on a lightweight perl framework called Dancer. It’s easy to learn and avoids the massive weight of Catalyst, making it ideal for use in shared hosting (paying to get a VM just so you can install thousands of perl modules is not cool).

Dancer runs under fastCGI, mod perl, and (most importantly for shared hosting) plain old CGI. Unfortunately, the online docs assume you can edit your apache.conf file and don’t really cover situations where you only have an htaccess at your disposal. Here is the config that I have used to make it work with only mod rewrite:

Contents of top-level htaccess:

AddDefaultCharset utf-8`

RewriteEngine On
RewriteBase /

# explicit rule to make sure our root dir gets handled
RewriteRule ^$ /dispatch.cgi/ [L,QSA]

# allow actual files and paths to get through (passthrough)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L,QSA]

# rewrite everything remaining to our main CGI
RewriteRule ^(.*) /dispatch.cgi/$1 [L,QSA]

## END ##

The directory structure to go with this is almost the Dancer default layout:

  • htdocs/
    • dispatch.cgi
    • favicon.ico
    • css/
    • images/
  • IrDance/ (folder as built by Dancer)
    • app.psgi
    • config.yml
    • IrDance.pm
    • logs/
    • views/
    • environments/
    • IrDance/
  • lib/
    • perl5/ (module dependencies)

I’m using cpanminus to bring in dependencies, since the shared hosting doesn’t have most of them. I’m also using local::lib to get them installed and managed in the lib/perl5 directory above.