Apache + FastCGI

748 views
Skip to first unread message

Ulrich

unread,
Jul 14, 2010, 8:02:34 PM7/14/10
to Mojolicious
Hello

i recently required a light-weighted, easy to setup perl web framework
and stumbled upon Mojolicious - much kudos to you! Great piece of
software!

My app runs via apache + fastcgi and it took me some time to figure
out how to do this. I read the latest documentation on github (http://
github.com/kraih/mojo/blob/master/lib/Mojolicious/Guides/
Cookbook.pod), but the suggested config did not work out for me. It
complains that FastCgiIpcDir is not allowed within Locations nor
VirtualHost and FastCgiServer also not in Locations. Further more, i
would like the fastcgi handler to handle the requests the same way the
daemon would (http://server/path not http://server/app.pl/path)

So, this is my working config, tested with apache 2.2.9 and 2.2.14:

----

NameVirtualHost *:1234

# FastCgiIpcDir /tmp

<VirtualHost *:1234>
ServerName localhost
DocumentRoot /var/www/
FastCgiServer /var/www/app.pl -processes 3
AddHandler fastcgi-script .pl
ScriptAliasMatch ^(/.*)$ /var/www/app.pl$1
</VirtualHost>

----

Maybe this will save somebody some time.

Could anyone share a working config for lighttpd ?

Greets
Ulrich

Sebastian Riedel

unread,
Jul 14, 2010, 11:26:03 PM7/14/10
to mojol...@googlegroups.com
> So, this is my working config, tested with apache 2.2.9 and 2.2.14:


Thanks, updated cookbook.

http://github.com/kraih/mojo/commit/0652724e4ffc294541691121fe9d9dbaeddf7c0e

--
Sebastian Riedel
http://labs.kraih.com
http://mojolicious.org
http://twitter.com/kraih

Brian Duggan

unread,
Jul 15, 2010, 9:41:04 AM7/15/10
to mojol...@googlegroups.com
On Wednesday, July 14, Ulrich wrote:
> Could anyone share a working config for lighttpd ?

Well, with Plack/PSGI and FCGI :

$ mojolicious generate lite_app
$ plackup -s FCGI --listen /tmp/fcgi.sock --daemonize ./myapp.pl
$ cat >> lighttpd.conf
server.document-root = var.CWD
server.port = 1234
server.modules = ( "mod_fastcgi" )
fastcgi.server = ( "/" =>
((
"socket" => "/tmp/fcgi.sock",
"check-local" => "disable",
"fix-root-scriptname" => "enable"
))
)
$ lighttpd -f ./lighttpd.conf
$ GET http://localhost:1234
<!doctype html><html>
<head><title>Funky!</title></head>
<body>Yea baby!
</body>
</html>

-Brian

Ulrich

unread,
Jul 17, 2010, 8:45:33 AM7/17/10
to Mojolicious
Thanks Brian.

I've hoped there would be a more "direct" way (without Plack). I will
try this out.

Greets
Ulrich

Ulrich

unread,
Jul 17, 2010, 8:49:16 AM7/17/10
to Mojolicious
According to Apache + Fastcgi, i've discovered a bug or at least a
strange behavior in Mojo::Server::FastCGI. Cause this somehow relates
to the initial post of mine, i will write it in this thread.

Whether this bug does only occur with Apache + FastCGI or also Plac/
PSGI + FCGI, i have not tested, yet.

Assuming the Apache + FastCGI config from above and a minimal
Mojolicious::Lite script, the "render_static" method hangs randomly.
The larger the static content to be served, the higher the probability
that the bug occurs. Here is the mini script:

----

use strict;
use warnings;
use Mojolicious::Lite;

any '/download/:name' => sub {
my ( $self ) = @_;
$self->render_static( $self->param( 'name' ) );
};

app->start;

----

Then you can go on with the test like so:
#> dd if=/dev/zero of=public/test bs=1M count=5
#> wget http://server/download/test

You will see somthing like this, before it stops:
----

Resolving server... 127.0.0.1
Connecting to server|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5242880 (5.0M) [text/plain]
Saving to: `out'

13% [====> ] 712,704 --.-K/s

----

The amount of bytes downloaded before the error occurs are afaik
multiples of 8192, if this helps.
A dirty hack to solve this issue is to comment out the line "return
unless defined $written;" in the "write_records" method. I have no
idea what side effects this might have.

Greets
Ulrich

Sebastian Riedel

unread,
Jul 17, 2010, 8:55:16 AM7/17/10
to mojol...@googlegroups.com
> I've hoped there would be a more "direct" way (without Plack). I will
> try this out.

There is a more direct way, every Mojolicious application *is* a FastCGI script out of the box.
It just seems nobody has an example configuration for that, which is no big surprise considering how unpopular Lighttpd has become.

Sebastian Riedel

unread,
Jul 17, 2010, 8:59:49 AM7/17/10
to mojol...@googlegroups.com
> According to Apache + Fastcgi, i've discovered a bug or at least a
> strange behavior in Mojo::Server::FastCGI.
> Assuming the Apache + FastCGI config from above and a minimal
> Mojolicious::Lite script, the "render_static" method hangs randomly.


That would be a big surprise, it is most likely a server constraint.

Lyle

unread,
Jul 17, 2010, 9:00:07 AM7/17/10
to mojol...@googlegroups.com
On 17/07/2010 13:55, Sebastian Riedel wrote:
> how unpopular Lighttpd has become

Why has this happened?


Lyle

Sebastian Riedel

unread,
Jul 17, 2010, 9:01:45 AM7/17/10
to mojol...@googlegroups.com
>> how unpopular Lighttpd has become
>
> Why has this happened?

Memory leaks and other bugs that have not been fixed in years.

Ulrich

unread,
Jul 17, 2010, 9:39:31 AM7/17/10
to Mojolicious

On Jul 17, 2:59 pm, Sebastian Riedel <kra...@googlemail.com> wrote:
> > According to Apache + Fastcgi, i've discovered a bug or at least a
> > strange behavior in Mojo::Server::FastCGI.
> > Assuming the Apache + FastCGI config from above and a minimal
> > Mojolicious::Lite script, the "render_static" method hangs randomly.
>
> That would be a big surprise, it is most likely a server constraint.

Excuse my imprecise wording, it's not the "render_static" method which
hangs. This was just the method with which it could be re-produced the
easiest. It's more a problem with mod_fastcgi + Mojo::Server::FastCGI.

Because mod_fastcgi is known to have issues related to this (eg
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=504132), i support
the idea, that it is not a bug in Mojolicious. Maybe it should not be
"fixed" but mentioned - it can save others a lot of time and hassle (i
spend lots of time, cause i assumed this is a problem in my app logic
before being able to reproduce it with render_static).
In the end, it means that any request to Apache + FastCGI + Mojolicous
has a chance of being aborted (the larger the output, the more
likely), which is most certainly not a behavior you would like in a
live environment.

I've tested this on two separate Apache installations (2.2.14 with
mod_fastcgi 2.4.6, 2.2.9 with mod_fastcgi 2.4.7). I will look into it
with Apache + mod_fcgid to figure out whether Apache or mod_fastcgi is
the more likely source for this problem..

Greets
Ulrich

Brian Duggan

unread,
Jul 17, 2010, 9:41:12 AM7/17/10
to mojol...@googlegroups.com
On Saturday, July 17, Sebastian Riedel wrote:
> > I've hoped there would be a more "direct" way (without Plack). I will
> > try this out.
>
> There is a more direct way, every Mojolicious application *is* a FastCGI script out of the box.
> It just seems nobody has an example configuration for that, which is no big surprise considering how unpopular Lighttpd has become.

Here's one :

server.document-root = var.CWD
server.port = 1234
server.modules = ( "mod_fastcgi" )
fastcgi.server = ( "/" =>
((

"port" => "1235",
"bin-path" => "myapp.pl fastcgi",


"check-local" => "disable",
"fix-root-scriptname" => "enable"
))
)

But, this only seems to work with "lighttpd -D -f lighttpd.conf".
When I omit "-D' (and a server.errorlog =..) I get

2010-07-17 09:36:47: (mod_fastcgi.c.1108) child exited with status 2 myapp.pl fastcgi

Brian

Ulrich

unread,
Jul 17, 2010, 10:30:23 AM7/17/10
to Mojolicious

> I've tested this on two separate Apache installations (2.2.14 with
> mod_fastcgi 2.4.6, 2.2.9 with mod_fastcgi 2.4.7). I will look into it
> with Apache + mod_fcgid to figure out whether Apache or mod_fastcgi is
> the more likely source for this problem..

Ok, i have tested this with Apache + mod_fcgid as well. The apache
config looks like this (not used to fcgid, maybe there is a more
elegant way, but this works):

<VirtualHost *:80>
ServerName mojo-fastcgi
FcgidWrapper "/var/www/app/test.pl fastcgi" .pl
AddHandler fcgid-script .pl
ScriptAliasMatch ^(/.*)$ /var/www/app/test.pl$1
</VirtualHost>

Sadly, the result is the same. With the "return unless defined
$written;", it fails to download the file randomly. Thus, i conclude
the problem is Apache vs Mojolicous, not mod_fastcgi.

Furthermore, it is also not Apache MPM related. I tried Prefork, ITK,
Worker and Event - same result. Overall, it seems that mod_fcgid has
less aborts and in at a later time in the download progress than
mod_fastcgi.

Even if this is not a bug in Mojolicous, apache is still a largely
used HTTP server. If the decision is to insist on a correct FastCGI
implementation and therefore exclude usage of Apache + Mojolicous, it
should be mentioned. Being an apache advocate, of course, i would
rather like a work around, which solves this issue ;)

However, besides the fact that i have tried all the mentioned test on
two separated installations (Ubuntu 10.04, Debian 5.05, on separate
physical machines), it could still be a local or a Debian (/Ubuntu)
problem. Maybe a third party should confirm this behavior.

Greets
Ulrich

Sebastian Riedel

unread,
Jul 17, 2010, 10:57:36 AM7/17/10
to mojol...@googlegroups.com
Please try the latest version from GitHub, i've made a few changes that might help.

http://github.com/kraih/mojo

Ulrich

unread,
Jul 17, 2010, 11:15:03 AM7/17/10
to Mojolicious

On Jul 17, 4:57 pm, Sebastian Riedel <kra...@googlemail.com> wrote:
> Please try the latest version from GitHub, i've made a few changes that might help.
>
>    http://github.com/kraih/mojo
>

Great work!

I can confirm that your fix worked out as expected. I've retested all
MPMs with mod_fastcgi and mod_fcgid and not a single abort occurred.
I've upped the file size to 1GB to assure the problem is not simply
shifted. Thanks for your quick responses and Mojo in general :)

Greets
Ulrich

Sebastian Riedel

unread,
Jul 17, 2010, 11:16:13 AM7/17/10
to mojol...@googlegroups.com
> I can confirm that your fix worked out as expected. I've retested all
> MPMs with mod_fastcgi and mod_fcgid and not a single abort occurred.
> I've upped the file size to 1GB to assure the problem is not simply
> shifted. Thanks for your quick responses and Mojo in general :)


Yay! :)

Reply all
Reply to author
Forward
0 new messages