Q:

HTTP 504 Gateway Time-out when running Dancer2 app as cgi-script

I am trying to deploy a Dancer2 application as a cgi-script with Apache. I followed the instructions in the Dancer2 deployment guide for running as a cgi-script but I am getting HTTP 504 Gateway Time-out when I try to access my application in a browser.
I am brand new to the world of PSGI/Plack so I could be way off in my diagnosis, but it looks like when I request
http://<hostname>/
from a remote host, dispatch.cgi starts up a server listening on port 3000 and then just sits there waiting for input until the request times out. Here's what I see in my Apache error log:
[Mon Nov 04 09:44:32 2013] [error] [client 128.117.20.57] [ip2map:30142]  core @2013-11-04 09:44:32> Registered Dancer2::Core::DSL__WITH__Dancer2::Plugin::Ajax=HASH(0x3414560) in /var/www/ip2map/public/../lib/ip2map.pm l. 3
[Mon Nov 04 09:44:32 2013] [error] [client 128.117.20.57] [ip2map:30142]  core @2013-11-04 09:44:32> Registered Dancer2::Core::DSL__WITH__Dancer2::Plugin::Ajax__WITH__Dancer2::Plugin::Database=HASH(0x3414560) in /var/www/ip2map/public/../lib/ip2map.pm l. 4
[Mon Nov 04 09:44:32 2013] [error] [client 128.117.20.57] >> Dancer2 v0.10 server 30142 listening on http://0.0.0.0:3000
[Mon Nov 04 09:44:32 2013] [error] [client 128.117.20.57] >> Dancer2::Plugin::Ajax (0.10)
[Mon Nov 04 09:44:32 2013] [error] [client 128.117.20.57] >> Dancer2::Plugin::Database (2.10)
[Mon Nov 04 09:45:32 2013] [warn] [client 128.117.20.57] Timeout waiting for output from CGI script /var/www/ip2map/public/dispatch.cgi
[Mon Nov 04 09:45:32 2013] [error] [client 128.117.20.57] Script timed out before returning headers: dispatch.cgi

Here is my Apache configuration (Apache v2.2.15):
NameVirtualHost *:80

<VirtualHost *:80>
    ServerName mcmes21.cgd.ucar.edu
    ServerAlias mcmes21
    DocumentRoot /var/www/ip2map/public
    ServerAdmin mcarey@ucar.edu

    <Directory "/var/www/ip2map/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/httpd/ip2map-error_log
    CustomLog /var/log/httpd/ip2map-access_log common
</VirtualHost>

Note that my application works fine when served with Starman via mod_proxy with the following Apache configuration:
NameVirtualHost *:80

<VirtualHost *:80>
    ServerName mcmes21.cgd.ucar.edu
    ServerAlias mcmes21

    DocumentRoot /var/www/ip2map

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

    ProxyPass        / http://localhost:5000/
    ProxyPassReverse / http://localhost:5000/
</VirtualHost>

Does anyone know how I can get this working as CGI?
Note: I also posted this question to the dancer-users mailing list yesterday.

A:

Use the following to configure Apache to run a Dancer app using mod_perl. This is much faster than CGI.

Install mod_perl, if not already installed:
 sudo apt-get install libapache2-mod-perl2

Install Plack and Dancer, if not already installed:
 sudo apt-get install libplack-perl
 sudo apt-get install libdancer-perl

Add the following to your site configuration within apache (usually within ../sites-available). In the snippet below, I use /home/user/dancerapp as the absolute path to your dancer app, and 'dancerapp.com' as your server name. Be sure to change to your app:
 <VirtualHost *:80>

 ServerName dancerapp.com

 DocumentRoot /home/user/dancerapp/public

 <Location />
   SetHandler perl-script
   PerlResponseHandler Plack::Handler::Apache2
   PerlSetVar psgi_app /home/user/dancerapp/bin/app.pl
   SetEnv DANCER_ENVIRONMENT "production"
 </Location>

 <Perl>
   use Plack::Handler::Apache2;
   Plack::Handler::Apache2->preload("/home/user/dancerapp/bin/app.pl");
 </Perl>

 </VirtualHost>

Remember to restart apache:
  sudo /etc/init.d/apache2 restart

