This file is indexed.

/usr/share/perl5/Dancer/Deployment.pod is in libdancer-perl 1.3095+dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
=head1 NAME

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

=head1 DESCRIPTION

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


=head2 Running as a cgi-script (or fast-cgi)

In providing ultimate flexibility in terms of deployment, your Dancer app can
be run as a simple cgi-script out-of-the-box. No additional web-server
configuration needed.  Your web server should recognize .cgi files and be able
to serve Perl scripts.  The Perl module Plack::Runner is required.

Start by adding the following to your apache configuration (httpd.conf or
sites-available/*site*):

    <VirtualHost *:80>
        ServerName www.example.com
        DocumentRoot /srv/www.example.com/public
        ServerAdmin you@example.com

        <Directory "/srv/www.example.com/public">
           AllowOverride None
           Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
           Order allow,deny
           Allow from all
           AddHandler cgi-script .cgi
        </Directory>

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

        ErrorLog  /var/log/apache2/www.example.com-error.log
        CustomLog /var/log/apache2/www.example.com-access_log common
    </VirtualHost>

Note that when using fast-cgi your rewrite rule should be:

        RewriteRule ^(.*)$ /dispatch.fcgi$1 [QSA,L]

Here, the mod_rewrite magic for Pretty-URLs is directly put in Apache's
configuration.  But if your web server supports .htaccess files, you can drop
those lines in a .htaccess file.

To check if your server supports mod_rewrite type C<apache2 -l> to list modules.
To enable mod_rewrite (Debian), run C<a2enmod rewrite>. Place following code in
a file called .htaccess in your application's root folder:

    # BEGIN dancer application htaccess
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule (.*) /dispatch.cgi$1 [L]
    # END dancer application htaccess

Now you can access your dancer application URLs as if you were using the
embedded web server.

    http://localhost/

This option is a no-brainer, easy to setup, low maintenance but serves requests
slower than all other options.

You can use the same technique to deploy with FastCGI, by just changing the
line:

	AddHandler cgi-script .cgi

By:

	AddHandler fastcgi-script .fcgi

Of course remember to update your rewrite rules, if you have set any:

    RewriteRule (.*) /dispatch.fcgi$1 [L]


=head2 Running stand-alone

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

Simply fire up your app:

    $ perl bin/app.pl
    >> Listening on 0.0.0.0:3000
    == 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.

=head3 Running on Perl webservers with plackup

A number of Perl web servers supporting PSGI are available on cpan:

=over 4

=item Starman

C<Starman> is a high performance web server, with support for preforking,
signals and more.

=item Twiggy

C<Twiggy> is an C<AnyEvent> web server, it's light and fast.

=item Corona

C<Corona> is a C<Coro> based web server.

=back

To start your application, just run plackup (see L<Plack> and specific servers
above for all available options):

   $ plackup bin/app.pl
   $ plackup -E deployment -s Starman --workers=10 -p 5001 -a bin/app.pl

As you can see, the scaffolded Perl script for your app can be used as a PSGI
startup file.

=head4 Enabling content compression

Content compression (gzip, deflate) can be easily enabled via a Plack
middleware (see L<Plack#Plack::Middleware>): L<Plack::Middleware::Deflater>.
It's a middleware to encode the response body in gzip or deflate, based on
Accept-Encoding HTTP request header.

Enable it as you would enable any Plack middleware. First you need to install
L<Plack::Middleware::Deflater>, then in the configuration file (usually
F<environments/development.yml>), add these lines:

  plack_middlewares:
    -
      - Plack::Middleware::Deflater
      - ...

These lines tell Dancer to add L<Plack::Middleware::Deflater> to the list of
middlewares to pass to L<Plack::Builder>, when wrapping the Dancer app. The
syntax is :

=over

=item *

as key: the name of the Plack middleware to use

=item *

as value: the options to pass it as a list. In our case, there is no option to
specify to L<Plack::Middleware::Deflater>, so we use an empty YAML list.

=back

To test if content compression works, trace the HTTP request and response
before and after enabling this middleware. Among other things, you should
notice that the response is gzip or deflate encoded, and contains a header
C<Content-Encoding> set to C<gzip> or C<deflate>

=head3 Running multiple apps with Plack::Builder

You can use Plack::Builder to mount multiple Dancer applications on
a L<PSGI> webserver like L<Starman>.

Start by creating a simple app.psgi file:

    use Dancer ':syntax';
    use Plack::Builder;

    setting apphandler => 'PSGI';

    my $app1 = sub {
        my $env = shift;
        local $ENV{DANCER_APPDIR} = '/Users/franck/tmp/app1';
        setting appdir => '/Users/franck/tmp/app1';
        load_app "app1";
        Dancer::App->set_running_app('app1');
        Dancer::Config->load;
        my $request = Dancer::Request->new( env => $env );
        Dancer->dance($request);
    };

    my $app2 = sub {
        my $env = shift;
        local $ENV{DANCER_APPDIR} = '/Users/franck/tmp/app2';
        setting appdir => '/Users/franck/tmp/app2';
        load_app "app2";
        Dancer::App->set_running_app('app2');
        Dancer::Config->load;
        my $request = Dancer::Request->new( env => $env );
        Dancer->dance($request);
    };

    builder {
        mount "/app1" => builder {$app1};
        mount "/app2" => builder {$app2};
    };

and now use L<Starman>

    plackup -a app.psgi -s Starman

=head3 Hosting on DotCloud

The simplest way to achieve this is to push your main application directory
to dotcloud with your C<bin/app.pl> file copied to (or symlinked from)
C<app.psgi>.

Beware that the dotcloud service enforces one environment only, named
C<deployment>. So instead of having C<environments/development.yml> or
C<environments/production.yml> you I<must> have a file named
C<environments/deployment.yml>.

Also make sure that your C<Makefile.PL> (or other dependency mechanism) includes
both Dancer and L<Plack::Request>.

The default in-memory session handler won't work, and instead you should switch
to something persistent. Edit C<config.yml> to change C<session: 'Simple'> to
(for example) C<session: 'YAML'>.

=head4 In case you have issues with Template::Toolkit on Dotcloud

If you use the L<Template::Toolkit> and its C<INCLUDE> or C<PROCESS> directives,
you might need to add the search path of your view files to the config. This is
probably going to be something like
C<INCLUDE_PATH: '/home/dotcloud/current/views'> in C<config.yml>.

An alternative implementation is to use a variation of the above Plack::Builder
template:

 use Plack::Builder;
 use Dancer ':syntax';
 use Dancer::Handler;
 use lib 'lib';

 my $app1 = sub {
     setting appdir => '/home/dotcloud/current';
     load_app "My::App";
     Dancer::App->set_running_app("My::App");
     my $env = shift;
     Dancer::Handler->init_request_headers($env);
     my $req = Dancer::Request->new(env => $env);
     Dancer->dance($req);
 };

 builder {
     mount "/app1" => $app1;
 };

This also supports hosting multiple apps, but you probably also need to specify
the specific Environment configuration to use in your application.

When mounting under a path on dotcloud, as in the above example, always create
links using the C<uri_for()> method for Dancer routes, and a C<uri_base>
variable for static content as shown in L<Dancer::Cookbook>. This means
whatever base path your app is mounted under, links and form submissions will
continue to work.

=head3 Creating a service

You can turn your app into proper service running in background using one of
the following examples:

=head4 Using Ubic

L<Ubic> is an extensible perlish service manager. You can use it to start and
stop any services, automatically start them on reboots or daemon failures, and
implement custom status checks.

A basic PSGI service description (usually in /etc/ubic/service/application):

    use parent qw(Ubic::Service::Plack);

    # if your application is not installed in @INC path:
    sub start {
        my $self = shift;
        $ENV{PERL5LIB} = '/path/to/your/application/lib';
        $self->SUPER::start(@_);
    }

    __PACKAGE__->new(
        server => 'Starman',
        app => '/path/to/your/application/app.pl',
        port => 5000,
        user => 'www-data',
    );

Run C<ubic start application> to start the service.


=head4 Using daemontools

daemontools is a collection of tools for managing UNIX services. You can use it
to easily start/restart/stop services.

A basic script to start an application: (in /service/application/run)

    #!/bin/sh

    # if your application is not installed in @INC path:
    export PERL5LIB='/path/to/your/application/lib'

    exec 2>&1 \
    /usr/local/bin/plackup -s Starman -a /path/to/your/application/app.pl -p 5000


=head3 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:

=head4 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/
    </VirtualHost>

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/

It is important for you to note that the Apache2 modules mod_proxy and
mod_proxy_http must be enabled.

    a2enmod proxy
    a2enmod proxy_http

It is also important to set permissions for proxying for security purposes,
below is an example.

    <Proxy *>
      Order allow,deny
      Allow from all
    </Proxy>

=head4 Using perlbal

C<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
L<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 10.0.0.10:3030
    POOL my_dancers ADD 10.0.0.11:3030
    POOL my_dancers ADD 10.0.0.12:3030
    POOL my_dancers ADD 10.0.0.13:3030

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




=head4 Using balance

C<balance> is a simple load-balancer from Inlab Software, available from
L<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 10.0.0.1 80 10.0.0.2:3000 10.0.0.3:3000 10.0.0.4:3000


=head4 Using Lighttpd

You can use Lighttp's mod_proxy:

    $HTTP["url"] =~ "/application" {
        proxy.server = (
            "/" => (
                "application" => ( "host" => "127.0.0.1", "port" => 3000 )
            )
        )
    }

This configuration will proxy all request to the B</application> path to the
path B</> on localhost:3000.

=head4 Using Nginx

with Nginx:

    upstream backendurl {
        server unix:THE_PATH_OF_YOUR_PLACKUP_SOCKET_HERE.sock;
    }

    server {
      listen       80;
      server_name YOUR_HOST_HERE;

      access_log /var/log/YOUR_ACCESS_LOG_HERE.log;
      error_log  /var/log/YOUR_ERROR_LOG_HERE.log info;

      root YOUR_ROOT_PROJECT/public;
      location / {
        try_files $uri @proxy;
        access_log off;
        expires max;
      }

      location @proxy {
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass       http://backendurl;
      }

    }

You will need plackup to start a worker listening on a socket :

    cd YOUR_PROJECT_PATH
    sudo -u www plackup -E production -s Starman --workers=2 -l THE_PATH_OF_YOUR_PLACKUP_SOCKET_HERE.sock -a bin/app.pl

A good way to start this is to use C<daemontools> and place this line with
all environments variables in the "run" file.

=head2 Running from Apache

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


=head3 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

        <Directory /home/myapp/myapp>
            AllowOverride None
            Order allow,deny
            Allow from all
        </Directory>

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

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

To set the environment you want to use for your application (production or
development), you can set it this way:

    <VirtualHost>
        ...
        SetEnv DANCER_ENVIRONMENT "production"
        ...
    </VirtualHost>

B<NOTE:> Only a single Dancer application can be deployed using the
C<Plack::Handler::Apache2> method. Multiple Dancer applications
B<will not work properly> (The routes will be mixed-up between the
applications).

It's recommended to start each app with C<plackup> using your
favorite server (Starman, for example) and then put a web server (Apache,
Nginx, Perlbal, etc.) as a frontend server for both apps using reverse proxy
(HTTP based, no fastcgi).

=head3 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
        </Directory>

        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]
    </VirtualHost>

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.


=head2 Running on lighttpd (CGI)

To run as a CGI app on lighttpd, just create a soft link to the dispatch.cgi
script (created when you run dancer -a MyApp) inside your system's cgi-bin
folder. Make sure mod_cgi is enabled.

    ln -s /path/to/MyApp/public/dispatch.cgi /usr/lib/cgi-bin/mycoolapp.cgi

=head2 Running on lighttpd (FastCGI)

Make sure mod_fcgi is enabled. You also must have L<FCGI> installed.

This example configuration uses TCP/IP:

    $HTTP["url"] == "^/app" {
        fastcgi.server += (
            "/app" => (
                "" => (
                    "host" => "127.0.0.1",
                    "port" => "5000",
                    "check-local" => "disable",
                )
            )
        )
    }

Launch your application:

    plackup -s FCGI --port 5000 bin/app.pl

This example configuration uses a socket:

    $HTTP["url"] =~ "^/app" {
        fastcgi.server += (
            "/app" => (
                "" => (
                    "socket" => "/tmp/fcgi.sock",
                    "check-local" => "disable",
                )
            )
        )
    }

Launch your application:

    plackup -s FCGI --listen /tmp/fcgi.sock bin/app.pl