December 2012 Archives

Moving my Catalyst Apps from Apache/FCGI to Nginx/Starman

Recently I had to move all my projects to a new server and decided to give Nginx and Starman a chance.

The Nginx config is rather simple.

my Catalyst Application is located at /var/www/MyApp

server {
    listen 80;
    server_name myapp.at *.myapp.at;

    location / {
      include /etc/nginx/proxy_params;
      proxy_pass http://unix:/var/www/MyApp/myapp.socket:/;
    }

    location /static {
      root /var/www/MyApp/root;
      expires 30d;
    }
}

Nginx expects the Catalyst Application to listen on the socket /var/www/MyApp/myapp.socket.

Here is my initd script that takes care of starting and stopping the starman processes. (/var/www/MyApp/myapp.starman.initd)

#!/usr/bin/env perl
use warnings;
use strict;
use Daemon::Control;

my $app_home = '/var/www/MyApp';
my $program  = '/var/www/perl5/perlbrew/perls/perl-5.16.2/bin/starman';
my $name     = 'MyApp';
my $workers  = 1;
my $pid_file = $app_home . '/myapp.pid';
my $socket   = $app_home . '/myapp.socket';

Daemon::Control->new({
    name        => $name,
    lsb_start   => '$nginx',
    lsb_stop    => '$nginx',
    lsb_sdesc   => $name,
    lsb_desc    => $name,
    path        => $app_home . '/myapp.starman.initd',

    user        => 'www-data',
    group       => 'www-data',
    directory   => $app_home,
    program     => "$program -Ilib myapp.psgi --workers $workers --listen $socket",

    pid_file    => $pid_file,
    stderr_file => $app_home . '/myapp.out',
    stdout_file => $app_home . '/myapp.out',

    fork        => 2,
})->run;

Check out Daemon::Control, it's a really helpful CPAN module that eases the creation of init scripts. Let's create the initd script.

./myapp.starman.initd get_init_file > /etc/init.d/cat-myapp

You want the starman processes to survive a reboot, so let's install to runlevels

update-rc.d cat-myapp defaults

Now reload Nginx and start your starman processes and you should be good to go.

I just started using this kind of config, so any problems wouldn't surprise me too much.

About davewood

user-pic I like Toast.