Friday, October 12, 2012

Catalyst on JustHost with fastcgi

Update- Blog moved to: http://saleemabdulhamid.com/blog/2012/10/catalyst-on-justhost-with-fastcgi

I'm working on upgrading my personal site (and will also be consolidating this blog there) and have decided to go with the Catalyst web framework for Perl. I have the site hosted on JustHost, with shared hosting (no dedicated ip) so my options were limited to php, perl and ruby on rails.

I decided to go with Perl for a number of reasons, one of them being that I've been personally interested in the language for a while. Another is that I read a lot of comparisons that pointed towards rails being easier to get started but harder to extend and modify outside of the mold of how things are normally done. I'm not sure about the truth of that.

Anyway, it seems that JustHost's Perl support is more geared towards simple cgi scripts than modern Perl and Catalyst, so it was a bit difficult to figure out the appropriate setup to run Catalyst on there. So here is what you have to do, and I imagine this would be representative of a lot of other cheap shared hosting providers. The biggest issue is that you can't run the Catalyst server because you can't open a firewall port, so if you run it, it won't be visible to the outside world. You also can't use cgi, because it has serious performance issues when running an application like Catalyst.

The .htaccess file

JustHost uses an apache server, running behind the scenes and serving all the sites on the shared server. You need to create a new (or modify the existing) .htaccess file to tell the apache server what to do. The .htaccess file will be at the root of whatever directory your domain points to. If you only host a single site on the server, that will probably be in the public_html folder. If you host multiple sites, check what folder the subdomain points to (usually you register multiple domains with JustHost and then alias them to subdomains on your main domain; those subdomains point to different folders in your server).

In your .htaccess file, first you need this line:
AddHandler fcgid-script .pl
This tells apache to use fastcgi to handle files that have the .pl extension.

The second thing you need to do is redirect requests to your domain to the script, which is located somewhere inside your Catalyst project directory:
RewriteEngine On
RewriteRule ^.*$ myapp/script/myapp_fastcgi.pl [NC,L]
This redirects all  requests to the Catalyst automatically generated fastcgi script inside the script folder of your project.

Including your CPAN modules

There's one additional thing needed, in order to be able to access the installed cpan modules from your application. You use JustHost's cPanel to install Perl modules. Then you need to add this to the myapp_fastcgi.pl script so the script can find those installed modules (if you need to use any of the other automatically generated catalyst scripts, such as _create.pl, you'll have to add this there, too):
BEGIN {
    my $b__dir = (-d '/home3/detaile7/perl'?'/home3/detaile7/perl':( getpwuid($>) )[7].'/perl');
    unshift @INC,$b__dir.'5/lib/perl5',$b__dir.'5/lib/perl5/x86_64-linux-thread-multi',map { $b__dir . $_ } @INC;
}
This is actually taken from JustHost's documentation and just adds the path to the installed modules. This should be right up at the top of the file, just below the shebang.