Skip to content

PSGI/Plack

Official Website: plackperl.org

Blurb:

PSGI is an interface between Perl web applications and web servers, and Plack is a Perl module and toolkit that contains PSGI middleware, helpers and adapters to web servers.

PSGI and Plack are inspired by Python’s WSGI and Ruby’s Rack.

Why:

If your web app supports the PSGI interface, then you can run it on any shared hosting provider that supports PSGI.

Install:

First install local::lib. Then all you have to do is:

cpanm Plack

app.psgi:

No matter what fancy web framework you’re using to create your web app, if it supports PSGI/Plack, you’ll probably end up with an app.psgi that you want to run. Let’s create one by hand:

# app.psgi
my $handler = sub {
    return [ 200, [ "Content-Type" => "text/plain", "Content-Length" => 11 ], [ "Hello World" ] ];
};

Now let’s test it from the command line with plackup:


# Hello Plack
plackup -a app.psgi -p 5000 &

# If you have access to your server via port 5000 you can view it at: http://server:5000
# But you probably don't, so use curl locally instead:
curl http://0:5000/ # Hello World
pkill plackup # stop the server

How would you like to run your app?

The beauty of PSGI/Plack is that now that you’ve got your app.psgi file, you can choose what web server technology you want to use to run your app: CGI, FastCGI, … (see the plackperl.org site for an exhaustive list).

Most of the web frameworks listed on the Perl Shared Hosting site link here, because once we’ve told you how to install the relevant dependencies and produce your app.psgi, the steps for running app in CGI mode, or FastCGI more, (or anything else) is the same regardless of whether you’re running a Dancer app, a Web::Simple app, etc..

CGI Mode

Create a file called app.cgi. At the top goes the local::lib bootstrap lines (so that Perl can find your local lib). Next, Plack::Runner is used to run your app.psgi (customise the path to app.psgi to suit your directory structure)

#!/usr/bin/env perl

# local::lib bootstrap
use lib "/home/patrick/perl5/lib/perl5";
use local::lib;

use Plack::Runner;
Plack::Runner->run('app.psgi');

Make app.cgi an executable file:

chmod +x app.cgi

You can test your CGI script by running it from the command line (Plack will auto-detect your environment and not actually run your app in CGI mode, but this is a handy way to see errors on the command line):

./app.cgi # any errors will be displayed
HTTP::Server::PSGI: Accepting connections at http://0:5000/

If you don’t see any errors, you should now be able to visit: http://yourdomain.com/app.cgi/

CGI Mode with .htaccess

If your host allows .htaccess files, you can make your URLs a bit prettier by creating a .htaccess file in the same directory as your app.cgi:
Following the Dancer Dispatch guide, you can then create a .htaccess to make the URL nicer (as long as your provider supports .htaccess files):

DirectoryIndex app.cgi/
AddHandler cgi-script .cgi
# Note that some shared hosts (like HostMonster) dp not allow +FollowSymLinks
Options +ExecCGI +SymLinksIfOwnerMatch -Indexes

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) /app.cgi/$1 [L]

You can then view your app at: http://yourdomain.com/app instead of http://yourdomain.com/app/app.cgi/.

FastCGI Mode

Create a file called app.fcgi. At the top goes the local::lib bootstrap lines (so that Perl can find your local lib). Next, Plack::Handler::FCGI is used to run your app.psgi (customise the path to app.psgi to suit your directory structure).

#!/usr/bin/env perl

# local::lib bootstrap
use lib "/home/patrick/perl5/lib/perl5";
use local::lib;

use Plack::Handler::FCGI;
my $app = do('app.psgi');
my $server = Plack::Handler::FCGI->new(nproc  => 5, detach => 1);
$server->run($app);

Make app.fcgi an executable file:

chmod +x app.fcgi

Assuming your hosting provider supports .htaccess files, you can then configure your app to run in FastCGI mode by creating the following .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?app.fcgi
RewriteRule ^(.*)$ app.fcgi/$1 [PT,L]
One Comment
  1. That’s if you have shell access, right? Most shared hosting companies don’t provide shell access.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS