Plack - A Fantastic Layer To Make Your App Deployment Easier

What is it?

From the documentation:

    Plack is a set of PSGI reference server implementations and helper
	utilities for Web application frameworks, exactly like Ruby's Rack.

What is PSGI?

PSGI is an interface between Perl web apps and web servers. This means you have a pretty slick and well-rounded platform that allows you to deploy your app as a persistent process while letting you point your server software at the running Plack process. You can *still* run your application under FastCGI and such, but all of the communication to the server is handled entirely by PSGI.

What does it look like?

Don't do any of this just yet, as you'll need to set up your script first. You can do the nginx config now if you'd like, but obviously pointing your browser to localhost/myapp/ isn't going to serve up your app unless it's actually running.

Starting the process:

    plackup -s Standalone::Prefork yourappname.psgi 

Your server configuration (let's use nginx):

    # inside a server {} block
	location /myapp {
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_pass  http://localhost:5000/;   # 5000 is the default plackup port

Note that if you don't specify a server option (-s), plackup defaults to a standalone server like Catalyst does. It's *that* simple. Now, let's do what we came here for and show you how to hookup Catalyst with Plack, running under the Standalone::Prefork engine.

The Dispatch Script

Plackup wants a dispatch script to fire up and delegate things to your app from. So, we need to create one of these for our simple Catalyst app. If you're lazy like me, you can also do perl scripts/ PSGI and have a helper generate your script for you.

After the usual Placky mumbo jumbo (for fun, I called this app Placky), we create a file called placky_app.psgi (You can call it whatever you want, really). Copy and paste these lines, and save your file:

    #!/usr/bin/env perl
    use strict;
    use Placky;

    my $app = sub { Placky->run(@_) };

Now, let's fire it up and test things:

    squishface:Placky dhoss$ plackup -s Standalone::Prefork script/placky_app.psgi

Since we didn't specify a port, you should (after seeing all the Catalyst server startup stuff) see something like this:

    [info] Placky powered by Catalyst 5.80016
	Plack::Server::Standalone::Prefork: Accepting connections at http://0:5000/

Make sure that after modifying your server configuration you restart nginx/whatever software you happen to be using so the changes take effect.

Open up your browser and navigate to http://localhost/myapp/ and you should see your friendly Catalyst welcome message. If so, great success! You now know how to deploy a Catalyst application under Plack. There are many server engines that Plack supports right off the bat to allow you to customize your deployment to suit you best.

That's all folks, the beauty of simplicity and being robust all wrapped up in one. Plack/PSGI will really help people ease into deployment and manage their application processes. Don't forget to check out the Plack Advent Calendar too for cool tips and tricks on making Plack even funner.

Feel free to check out the Placky code here: or do svn co .


Devin Austin

Thanks to miyagawa (Tatsuhiko Miyagawa) for putting together this fantastic interface and for reviewing this article to make sure I did his work justice :-)