++ed by:

59 PAUSE user(s)
70 non-PAUSE user(s).

Sawyer X


Dancer::Deployment - common ways to put your Dancer app into use


Dancer has been designed to be flexible, and this flexibility extends to your choices when deploying your Dancer app.

Running stand-alone

At the simplest, your Dancer app can run standalone, operating as its own webserver using HTTP::Simple::PSGI.

Simply fire up your app:

    $ perl ./mysuperwebapp.pl
    >> Listening on
    == Entering the dance floor ...

Point your browser at it, and away you go!

This option can be useful for small personal web apps or internal apps, but if you want to make your app available to the world, it probably won't suit you.

Running stand-alone behind a proxy / load balancer

Another option would be to run your app stand-alone as described above, but then use a proxy or load balancer to accept incoming requests (on the standard port 80, say) and feed them to your Dancer app.

This could be achieved using various software; examples would include:

Using Apache's mod_proxy

You could set up a VirtualHost for your web app, and proxy all requests through to it:

    <VirtualHost mywebapp.example.com:80>
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/

Or, if you want your webapp to share an existing VirtualHost, you could have it under a specified dir:

    ProxyPass /mywebapp/ http://localhost:3000/
    ProxyPassReverse /mywebapp/ http://localhost:3000/

Using perlbal

perlbal is a single-threaded event-based server written in Perl supporting HTTP load balancing, web serving, and a mix of the two, available from http://www.danga.com/perlbal/

It processes hundreds of millions of requests a day just for LiveJournal, Vox and TypePad and dozens of other "Web 2.0" applications.

It can also provide a management interface to let you see various information on requests handled etc.

It could easily be used to handle requests for your Dancer apps, too.

It can be easily installed from CPAN:

    perl -MCPAN -e 'install Perlbal'

Once installed, you'll need to write a configuration file. See the examples provided with perlbal, but you'll probably want something like:

    CREATE POOL my_dancers
    POOL my_dancers ADD
    POOL my_dancers ADD
    POOL my_dancers ADD
    POOL my_dancers ADD

    CREATE SERVICE my_webapp
    SET listen          =
    SET role            = reverse_proxy
    SET pool            = my_dancers
    SET persist_client  = on
    SET persist_backend = on
    SET verify_backend  = on
    ENABLE balancer

Using balance

balance is a simple load-balancer from Inlab Software, available from http://www.inlab.de/balance.html.

It could be used simply to hand requests to a standalone Dancer app. You could even run several instances of your Dancer app, on the same machine or on several machines, and use a machine running balance to distribute the requests between them, for some serious heavy traffic handling!

To listen on port 80, and send requests to a Dancer app on port 3000:

    balance http localhost:3000

To listen on a specified IP only on port 80, and distribute requests between multiple Dancer apps on multiple other machines:

    balance -b 80

Running from Apache

You can run your Dancer app from Apache using the following examples:

Running from Apache with Plack

You can run your app from Apache using PSGI (Plack), with a config like the following:

    <VirtualHost myapp.example.com>
        ServerName www.myapp.example.com
        ServerAlias myapp.example.com
        DocumentRoot /websites/myapp.example.com

        <Location />
            SetHandler perl-script
            PerlHandler Plack::Handler::Apache2
            PerlSetVar psgi_app /websites/myapp.example.com/app.psgi

        ErrorLog  /websites/myapp.example.com/logs/error_log
        CustomLog /websites/myapp.example.com/logs/access_log common

Running from Apache via FastCGI

You can run your Dancer app from Apache via FastCGI using the dispatch.fcgi script written by the dancer helper script when you create your application scaffolding:

    <VirtualHost *:80>
        ServerName localhost
        DocumentRoot "/tmp/TestApp/public"

        <Directory "/tmp/TestApp/public">
            AllowOverride None
            Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
            Order allow,deny
            Allow from all
            AddHandler fastcgi-script .fcgi

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ /dispatch.fcgi [QSA,L]

Running from Apache under appdir

If you want to deploy multiple applications under the same VirtualHost, using one application per directory for example, you can do the following.

This example uses the FastCGI dispatcher that comes with Dancer, but you should be able to adapt this to use any other way of deployment described in this guide. The only purpose of this example is to show how to deploy multiple applications under the same base directory/virtualhost.

        <VirtualHost *:80>
                ServerName localhost
                DocumentRoot "/path/to/rootdir"
                RewriteEngine On
                RewriteCond %{REQUEST_FILENAME} !-f

                <Directory "/path/to/rootdir">
                        AllowOverride None
                        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                        Order allow,deny
                        Allow from all
                        AddHandler fastcgi-script .fcgi

                RewriteRule /App1(.*)$ /App1/public/dispatch.fcgi$1 [QSA,L]
                RewriteRule /App2(.*)$ /App2/public/dispatch.fcgi$1 [QSA,L]
                RewriteRule /AppN(.*)$ /AppN/public/dispatch.fcgi$1 [QSA,L]

Of course, if your Apache configuration allows that, you can put the RewriteRules in a .htaccess file directly within the application's directory, which lets you add a new application without changing the Apache configuration.