Search code examples
perlnginxfastcgicatalystperlbrew

How to create a init-script for an Perl catalyst application running on nginx with fastcgi and perlbrew


I'm looking for a initscript to make usage of perlbrew on a webserver running a nginx as proxy for an perl catalyst application. I'm currently trying to start the app via

  source $PERLBREW
  execute "perlbrew use perl-5.14.4@devel"
  execute "mkdir -p $PID_PATH && $START_ICOS_APP > /dev/null  2>&1 &"
  echo "$DESC started"

but it appers it cannot find the local perl installation. $PERLBREW is set to my perlbrew folder.


Solution

  • This is a good step by step guide how to do this, but it is French (but still understandable).

    http://www.catapulse.org/articles/view/124

    I copied here:

    Setup the user which is going to run the catalyst app (www-data in this example)

    su - www-data
    curl -kL http://install.perlbrew.pl | bash
    echo 'source ~/perl5/perlbrew/etc/bashrc' >> .profile
    . .profile
    
    perlbrew install perl-5.16.3 -Dusethreads --as perl-5.16.3_WITH_THREADS
    perlbrew switch perl-5.16.3_WITH_THREADS
    #perlbrew install-cpanm
    #cpanm Catalyst Catalyst::Devel
    #catalyst.pl myapp
    

    (I assume that your application name is myapp, replace it with yours.)

    create /etc/nginx/sites-enabled/myapp

    server {
        listen 80;
        server_name exemple.com *.exemple.com;
        client_max_body_size 50m;
    
        location / {
          include /etc/nginx/fastcgi_params;
          fastcgi_param SCRIPT_NAME '';
          fastcgi_param PATH_INFO $fastcgi_script_name;
          fastcgi_pass unix:/var/www/myapp/myapp.socket;
        }
    
        location /static {
          root /var/www/myapp/root;
          expires 30d;
        }
    }
    

    Create /var/www/myapp/myapp.fastcgi.initd

    #!/usr/bin/env perl
    use warnings;
    use strict;
    use Daemon::Control;
    
    # 1) create initd file
    # ./myapp.fastcgi.initd get_init_file >  /etc/init.d/cat-myapp
    #
    # 3) install to runlevels
    # update-rc.d cat-myapp defaults
    
    
    my $app_home = '/var/www/myapp';
    my $perl     = 'perl';
    my $program  = $app_home . '/script/myapp_fastcgi.pl';
    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.fastcgi.initd',
    
        user        => 'www-data',
        group       => 'www-data',
        directory   => $app_home,
        program     => "$perl $program --nproc $workers --listen $socket",
    
        pid_file    => $pid_file,
        stderr_file => $app_home . '/myapp.out',
        stdout_file => $app_home . '/myapp.out',
    
        fork        => 2,
    })->run;
    

    Set permission on files and create the proper init file:

    $ chmod +x myapp.fastcgi.initd
    $ ./myapp.fastcgi.initd get_init_file >  /etc/init.d/cat-myapp
    

    Start your application and bounce your webserver:

    $ /etc/init.d/cat-myapp start
    $ /etc/init.d/nginx restart