Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Serving multiple Plack apps

by Your Mother (Bishop)
on Feb 09, 2015 at 23:32 UTC ( #1116113=note: print w/replies, xml ) Need Help??

in reply to Serving multiple Plack apps

Multiple executables and apps with one PSGI server

Proof of concept to help you and others (and me the next time I want to stub out an example). Tested to work on my OS X box. I recommend uWSGI as an app server and nginx as a webserver (it doesn’t like CGI so you might have to forgo it unless you get ALL your CGIs ironed out to PSGI).


Install or skip/edit the parts that use: strictures, Catalyst, Catalyst::Devel, Mojolicious, Plack, Plack::Builder, Plack::Middleware::Rewrite… maybe some I missed. Judicious middleware use can let you do all kinds of unified logging or even sessions (x-app you’d need to unify the handling and name but not as hard as from scratch).

Update: DERP indeed, left these out, Plack::App::CGIBin, Plack::App::WrapCGI.

Make a play dir and get in it (update, added the cgi “bin”)–

cd mkdir pm-1116108 cd pm-1116108 mkdir cgi

Create Cat app– MyCatalystApp

Create hello.php

<?php print "Hello, World!"; ?>

Create cgi/hello.cgi (need the “bin”)–

#!/usr/bin/env perl use strictures; use CGI qw(:standard); print header(), start_html("HAI"), h1("DERP!"), end_html();


use Mojolicious::Lite; get '/' => sub { my $c = shift; $c->render(text => "OHAI!"); }; app->start;

Create app.psgi

use strictures; use utf8; use Plack::Builder; use Encode; require Mojo::Server::PSGI; require Plack::App::CGIBin; require Plack::App::WrapCGI; use lib "./MyCatalystApp/lib"; require MyCatalystApp; my $root = sub { [ 200, [ "Content-Type" => "text/plain; charset=utf-8" ], [ encode_utf8("I \x{2763} Plack::Builder") ] ]; }; my $cat_app = MyCatalystApp->psgi_app; my $mojo_app = eval { my $server = Mojo::Server::PSGI->new; $server->load_app("./"); $server->to_psgi_app; }; my $cgi_app = Plack::App::CGIBin ->new( root => "./cgi" ) ->to_app; my $php = Plack::App::WrapCGI ->new( script => "/usr/bin/php ./hello.php", execute => 1 ) ->to_app; builder { enable "Rewrite", rules => sub { s,(?<=/cat)\z,/,; # Add trailing slash for Cat app’s root. }; mount "/cat" => $cat_app; mount "/mojo" => $mojo_app; mount "/cgi" => $cgi_app; mount "/php" => $php; mount "/" => $root; }; __END__

Start it up (with restart flag to watch for edits, Cat does lots of debug for a new app so we turn it off here)–

env CATALYST_DEBUG=0 plackup -r Watching ./lib pm-1116108.psgi for file updates. HTTP::Server::PSGI: Accepting connections at http://0:5000/

Now visit (might need to change “localhost” depending on your box’s setup)–

  • http://localhost:5000/
  • http://localhost:5000/cat
  • http://localhost:5000/mojo
  • http://localhost:5000/cgi/hello.cgi
  • http://localhost:5000/php

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1116113]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2020-02-22 22:35 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (102 votes). Check out past polls.