Deploying a PSGI app in Apache without restarting (again)

241 views
Skip to first unread message

Mikolaj Kucharski

unread,
Oct 29, 2011, 8:25:49 AM10/29/11
to psgi-...@googlegroups.com
Hi,

I know this question was asked before here:

http://stackoverflow.com/questions/6124010/how-do-you-deploy-a-psgi-script-in-apache-without-restarting

but I would like to understand where exactly is the output cached? I
read Plack::Handler::Apache1 and Plack::Util, but I don't see any
caching mechanism there, maybe I'm missing something.

- Is it cached on Apache level, or in Plack/PSGI?

- Is the behaviour described at above StackOverflow link specific to
Apache and would behave differently with other HTTP server?

--
best regards
q#

Tatsuhiko Miyagawa

unread,
Oct 29, 2011, 3:16:49 PM10/29/11
to psgi-...@googlegroups.com
On Sat, Oct 29, 2011 at 5:25 AM, Mikolaj Kucharski
<mik...@kucharski.name> wrote:
> Hi,
>
> I know this question was asked before here:
>
> http://stackoverflow.com/questions/6124010/how-do-you-deploy-a-psgi-script-in-apache-without-restarting
>
> but I would like to understand where exactly is the output cached?

Output is not cached. The *application* is compiled and cached, for performance.

> I read Plack::Handler::Apache1 and Plack::Util, but I don't see any
> caching mechanism there, maybe I'm missing something.

https://metacpan.org/source/MIYAGAWA/Plack-0.9984/lib/Plack/Handler/Apache1.pm#L22
There's "||=" operator, caching the application.

> - Is it cached on Apache level, or in Plack/PSGI?
>
> - Is the behaviour described at above StackOverflow link specific to
>  Apache and would behave differently with other HTTP server?

Yes, but most Plack handlers and PSGI servers compile the PSGI code
and runs it in a persistent environment. The only exceptions are CGI
handler and Shotgun loader, which recompiles the application every
time a request comes in.


--
Tatsuhiko Miyagawa

Mikolaj Kucharski

unread,
Oct 29, 2011, 4:43:00 PM10/29/11
to psgi-...@googlegroups.com

Thanks for the anwsers. Much appreciated.

I'm learning, I'm a noob. In the past working with CGI, I could throw
bunch of testing scripts under a directory tree, modify them at any time
and see what happens after refreshing a page in a browser, without any
need to restart http server every time a change a character in a file.

So far no matter what I try to (re)create above scenario in PSGI
environment I hit a bug (some call it a ``feature'') which doesn't allow
me just work on the code and forget what server I'm running, and what
PSGI is all about.

Can anyone recommend me a solution for my need? I thought PSGIBin will
solve my problem, but so far it's not the case..

--
best regards
q#

Tatsuhiko Miyagawa

unread,
Oct 29, 2011, 8:21:10 PM10/29/11
to psgi-...@googlegroups.com

What you have is more about the issues with Mojolicious not allowing multi tenancy in one process like pointed out in the other thread.

For a development you can still use restarting handlers like CGI or Shotgun loader, but for a deployment you might have to use a non-lite mojolicious app that might not have that problem, or otherwise consult Mojolicious to fix that issue.

Mikolaj Kucharski

unread,
Oct 30, 2011, 2:08:49 PM10/30/11
to psgi-...@googlegroups.com
On Sat, Oct 29, 2011 at 05:21:10PM -0700, Tatsuhiko Miyagawa wrote:
> For a development you can still use restarting handlers like CGI or Shotgun
> loader, but for a deployment you might have to use a non-lite mojolicious
> app that might not have that problem, or otherwise consult Mojolicious to
> fix that issue.

Ok. Regards CGI handler and Shotgun loader. Do you mean this:

http://search.cpan.org/dist/Plack/lib/Plack/Handler/CGI.pm

http://search.cpan.org/dist/Plack/lib/Plack/Loader/Shotgun.pm


In documentation of CGI handler for Plack there is an example:

------------------------8<------------------------
Want to run PSGI application as a CGI script? Rename .psgi to .cgi and
change the shebang line like:

#!/usr/bin/env plackup
# rest of the file can be the same as other .psgi file
------------------------8<------------------------


Above it is working for me on Apache with mod_perl and
Mojolicious::Lite now, however I think I've tried this in the past and it
did not worked as I've put full path to plackup(1) on shebang line:

# head -n1 env-plackup-mojo.pl plackup-mojo.pl
==> env-plackup-mojo.pl <==
#!/usr/bin/env plackup

==> plackup-mojo.pl <==
#!/usr/local/bin/plackup


Surprisingly the second script doesn't work[#ref1]. Do you know maybe why?


Thanks again for above tips about CGI handler and Shotgun loader, I will
definately check them out.


References
1. http://www4.kucharski.name/test/script-alias/list.cgi
2. http://groups.google.com/group/mojolicious/browse_thread/thread/712fc3442f10390c

--
best regards
q#

Jakob Voss

unread,
Oct 30, 2011, 7:10:56 PM10/30/11
to psgi-...@googlegroups.com
Tatsuhiko Miyagawa wrote:

> For a development you can still use restarting handlers like CGI or
Shotgun loader, but for a deployment
> you might have to use a non-lite mojolicious app that might not have
that problem, or otherwise consult
> Mojolicious to fix that issue.

I asked the original question on Stackoverflow some time ago. Your
presentation

http://www.slideshare.net/miyagawa/deploying-plack-web-applications-oscon-2011-8706659

is very helpful, but some more-detailled best-practice guide wouldn't be
wrong. In particular I found that the following setup is very handy

1. use plackup (with -r) during development
2. use directory layout and settings as documented by DotCloud
3. push to DotCloud for smoke testing
4. run a gracefully restartable webserver (Starman) in user space (high
port) at production
5. don't forget to add a startup script to start Starman after reboot!
6. put a reverse proxy (Nginx, Apache...) in front (port 80)
7. host project in a git repository at production
8. add a git post-recieve hook at production to automatically restart
Starman

This way I never have to login at production and manually restart a
service. As HTML templates, CSS, images etc. are served static, you can
still edit them on production in case of emergency, but the Perl code
should never be edited on a live system. I suppose this lesson is new
for instance to developes coming from PHP ;-)

Hope this helps someone else too.

Cheers
Jakob


--
Verbundzentrale des GBV (VZG)
Digitale Bibliothek - Jakob Vo�
Platz der Goettinger Sieben 1
37073 Goettingen - Germany
+49 (0)551 39-10242
http://www.gbv.de
jakob...@gbv.de

Tatsuhiko Miyagawa

unread,
Oct 30, 2011, 7:13:51 PM10/30/11
to psgi-...@googlegroups.com
On Sun, Oct 30, 2011 at 4:10 PM, Jakob Voss <Jakob...@gbv.de> wrote:

> is very helpful, but some more-detailled best-practice guide wouldn't be
> wrong. In particular I found that the following setup is very handy
>
> 1. use plackup (with -r) during development
> 2. use directory layout and settings as documented by DotCloud
> 3. push to DotCloud for smoke testing
> 4. run a gracefully restartable webserver (Starman) in user space (high
> port) at production
> 5. don't forget to add a startup script to start Starman after reboot!
> 6. put a reverse proxy (Nginx, Apache...) in front (port 80)
> 7. host project in a git repository at production
> 8. add a git post-recieve hook at production to automatically restart
> Starman
>
> This way I never have to login at production and manually restart a
> service. As HTML templates, CSS, images etc. are served static, you can
> still edit them on production in case of emergency, but the Perl code
> should never be edited on a live system. I suppose this lesson is new
> for instance to developes coming from PHP ;-)
>
> Hope this helps someone else too.

I'm sure all of these are automatically taken care of if you use PaaS
like dotcloud.

Otherwise, well volunteered.

P.S. I'm giving a tutorial in London for this in about 2 weeks.

--
Tatsuhiko Miyagawa

Mikolaj Kucharski

unread,
Oct 31, 2011, 12:57:41 PM10/31/11
to psgi-...@googlegroups.com
Do you guys know why when putting full path to plackup in shebang
line doesn't work, but when using '/usr/bin/evn plackup' it does work?

http://www4.kucharski.name/test/script-alias/list.cgi

Tatsuhiko Miyagawa

unread,
Oct 31, 2011, 1:01:07 PM10/31/11
to psgi-...@googlegroups.com
Define "doesn't work". See the apache error log and find what is causing errors.

Sent from my iPad

Mikolaj Kucharski

unread,
Oct 31, 2011, 1:41:09 PM10/31/11
to psgi-...@googlegroups.com
Request returns HTTP 500 with following lines in error_log:

[Mon Oct 31 18:16:21 2011] [error] (8)Exec format error: exec of /var/www/test/plackup-mojo.pl failed
[Mon Oct 31 18:16:21 2011] [error] [client 195.99.160.66] Premature end of script headers: /var/www/test/plackup-mojo.pl


and script is readable and executable by all, but it looks unrelated to
Plack. I need to investigate this myself:

# /var/www/test/plackup-mojo.pl
/var/www/test/plackup-mojo.pl[3]: use: not found
/var/www/test/plackup-mojo.pl[5]: get: not found
/var/www/test/plackup-mojo.pl[6]: get: not found
/var/www/test/plackup-mojo.pl[8]: app-: not found
/var/www/test/plackup-mojo.pl[10]: __DATA__: not found
/var/www/test/plackup-mojo.pl[11]: @@: not found
/var/www/test/plackup-mojo.pl[12]: syntax error: `< ' unexpected

# head -n1 /var/www/test/plackup-mojo.pl
#!/usr/local/bin/plackup

# head -n1 /usr/local/bin/plackup
#!/usr/bin/perl


No joy with shebang line to a script? Anyway I'll try to figure it out..


On Mon, Oct 31, 2011 at 10:01:07AM -0700, Tatsuhiko Miyagawa wrote:
> Define "doesn't work". See the apache error log and find what is causing errors.
>
> Sent from my iPad
>
> On Oct 31, 2011, at 9:57 AM, Mikolaj Kucharski <mik...@kucharski.name> wrote:
>
> > Do you guys know why when putting full path to plackup in shebang
> > line doesn't work, but when using '/usr/bin/evn plackup' it does work?
> >
> > http://www4.kucharski.name/test/script-alias/list.cgi
> >
> >
> > On Sun, Oct 30, 2011 at 06:08:49PM +0000, Mikolaj Kucharski wrote:
> >> In documentation of CGI handler for Plack there is an example:
> >>
> >> ------------------------8<------------------------
> >> Want to run PSGI application as a CGI script? Rename .psgi to .cgi and
> >> change the shebang line like:
> >>
> >> #!/usr/bin/env plackup
> >> # rest of the file can be the same as other .psgi file
> >> ------------------------8<------------------------
> >>
> >>
> >> Above it is working for me on Apache with mod_perl and
> >> Mojolicious::Lite now, however I think I've tried this in the past and it
> >> did not worked as I've put full path to plackup(1) on shebang line:
> >>
> >> # head -n1 env-plackup-mojo.pl plackup-mojo.pl
> >> ==> env-plackup-mojo.pl <==
> >> #!/usr/bin/env plackup
> >>
> >> ==> plackup-mojo.pl <==
> >> #!/usr/local/bin/plackup
> >>
> >>
> >> Surprisingly the second script doesn't work[#ref1]. Do you know maybe why?

--
best regards
q#

Mikolaj Kucharski

unread,
Oct 31, 2011, 8:25:29 PM10/31/11
to psgi-...@googlegroups.com
On Mon, Oct 31, 2011 at 05:41:09PM +0000, Mikolaj Kucharski wrote:
> Request returns HTTP 500 with following lines in error_log:
>
> [Mon Oct 31 18:16:21 2011] [error] (8)Exec format error: exec of /var/www/test/plackup-mojo.pl failed
> [Mon Oct 31 18:16:21 2011] [error] [client 195.99.160.66] Premature end of script headers: /var/www/test/plackup-mojo.pl
>
>
> and script is readable and executable by all, but it looks unrelated to
> Plack. I need to investigate this myself:
>
> # /var/www/test/plackup-mojo.pl
> /var/www/test/plackup-mojo.pl[3]: use: not found
> /var/www/test/plackup-mojo.pl[5]: get: not found
> /var/www/test/plackup-mojo.pl[6]: get: not found
> /var/www/test/plackup-mojo.pl[8]: app-: not found
> /var/www/test/plackup-mojo.pl[10]: __DATA__: not found
> /var/www/test/plackup-mojo.pl[11]: @@: not found
> /var/www/test/plackup-mojo.pl[12]: syntax error: `< ' unexpected
>
> # head -n1 /var/www/test/plackup-mojo.pl
> #!/usr/local/bin/plackup
>
> # head -n1 /usr/local/bin/plackup
> #!/usr/bin/perl
>
>
> No joy with shebang line to a script? Anyway I'll try to figure it out..

FYI: This is not Plack issue, and is specific to OpenBSD. I've created
small testing scripts and compared their execution with Linux. On
OpenBSD you can see error (see above, see below) under Linux it works.

ktrace says: execve -1 errno 8 Exec format error


Just so you know if someone else runs into the same issue like me.

--
best regards
q#

Reply all
Reply to author
Forward
0 new messages