Mounting a Dancer app into Apache + PSGI/Plack

EDIT : This is going to change as soon as CGI::PSGI is available, the “patched CGI” way won’t be needed anymore, see miyagawa’s post for details

Now that Dancer 0.9904 is released, I’ve took the time to upgrade my Dancer configuration for the official website from a proxy_balancer configuration to an Apache2 + PSGI/Plack architecture.

Here is how to proceed if you like to power your Dancer app up with Apache and PSGI/Plack.

First of all, you need Plack, at the time of this writing, it’s not yet in CPAN, so you should go to miyagawa’s place. Grab also his patched version of CGI.pm. Install both of them and you’re ready to continue.

Next step is to make sure your app is ready for a Plack run, the best thing to do is to run the helper that comes with 0.9904, it will write all the files needed in your application directory if they’re not already present.

Here’s all the files you should have:

$ dancer -a mywebapp
+ mywebapp
+ mywebapp/views
+ mywebapp/views/index.tt
+ mywebapp/views/layouts
+ mywebapp/views/layouts/main.tt
+ mywebapp/environments
+ mywebapp/environments/development.yml
+ mywebapp/environments/production.yml
+ mywebapp/config.yml
+ mywebapp/mywebapp.pm
+ mywebapp/mywebapp.pl
+ mywebapp/app.psgi
+ mywebapp/public
+ mywebapp/public/css
+ mywebapp/public/css/style.css
+ mywebapp/public/css/error.css
+ mywebapp/public/images
+ mywebapp/public/404.html
+ mywebapp/public/500.html

Note that your route handlers should be defined in mywebapp.pm, and not directly in the standalone server mywebapp.pl.

You should also have an app.psgi file in your application directory.

Make sure everything is allright by running the standalone webserver:

$ ./mywebapp.pl
>> Listening on 127.0.0.1:3000
== Entering the development dance floor ...

Ok, now run it with Plack’s standalone server, to make sure the PSGI file is ok:

$ /path/to/Plack/scripts/plackup &
$ curl -I http://localhost:8080/
HTTP/1.0 200 OK
Content-Length: 0
Content-Type: text/html

Great, you’re ready to go. The last step is to mount the application in your Apache configuration, for instance with a VirtualHost:

<VirtualHost webapp.domain.com>
    ServerName webapp.domain.com
    DocumentRoot /srv/webapp.domain.com/public

    <Location />
        SetHandler perl-script
        PerlHandler Plack::Impl::Apache2
        PerlSetVar psgi_app /srv/webapp.domain.com/app.psgi
    </Location>
</VirtualHost>

Restart Apache and enjoy your dances.

This entry was posted in Programming and tagged , , , . Bookmark the permalink. . 2457 views.

2 Responses to Mounting a Dancer app into Apache + PSGI/Plack

  1. David Moreno says:

    Awesome! :)

    Note that Plack::Impl::Apache2 was renamed to Plack::Server::Apache2 per:
    http://github.com/miyagawa/Plack/commit/4a6cc4a84ec181bf8b190e301e0bb54968213ca2

  2. sukria says:

    Noted, I’ll have to rewrite that post-entry in order to be sync with CGI::PSGI, so I’ll change that too.