UseModWiki | AchievingPerformance | RecentChanges | Preferences

mod_perl Howto

OK. I just copied the stuff from the mother page here. This is at the moment more of a Howto? then a Howto!. But I'll clean the page up when (indeed if) I get things to work. -- PEZ

Table of contents

1. Setup
1.1. The files
2. Patches (or modifications to the wiki script)
2.1. Initializing global variables
2.2. GetRemoteHost
3. Results / Questions / Comments
3.1. Errors
3.2. Warnings

1. Setup

For the rest to make sence let me give some details. My operating environment happens to be a 386 Fedora Core 3 with SELinux disabled. And I have:

1.1. The files

z-vhosts.conf: (sourced from httpd.conf on my system)
NameVirtualHost *:80
<VirtualHost *:80>
    ServerName test.hostname.net
    ServerAdmin webmaster@hostname.net
    DocumentRoot /var/www/h2wiki
    ErrorLog /var/log/httpd/hostname.net-error_log
    CustomLog /var/log/httpd/hostname.net-access_log common

    PerlRequire  /home/wikilib/h2wiki/startup.pl
    PerlModule UseModWiki
    <Location /h2wiki>
        PerlSetupEnv On
        SetHandler perl-script
        PerlHandler UseModWiki::DoWikiRequest


use strict;
use lib qw(/home/wikilib/h2wiki);

2. Patches (or modifications to the wiki script)

I wish someone would say "Stop it! The wiki scripts works with mod_perl if you just set things up like this...". So far no such saviours so I have resorted to try patch the script to make this to work.

2.1. Initializing global variables

Some of the global variable access causes warnings. JAPH suggested I should initialize them. Here's the function I use so far:
sub InitGlobals {
  $MainPage = "";
  $ScriptName = "";
  $TableOfContents = "";
It's called first thing inside DoWikiRequest.

Question (that I have on my todo-list to test): Do the warnings related to these global variables also come in a regular non-persistent script setup? I haven't seen them before I started trying with mod_perl...

2.2. GetRemoteHost?

I have no REMOTE_HOST in my %ENV, which causes warnings from this function. The function now looks like so to get rid of the warnings:
sub GetRemoteHost {
  my ($doMask) = @_;
  my ($rhost, $iaddr);

  $rhost = $ENV{REMOTE_HOST};
  if ($UseLookup && (!defined($rhost) || $rhost eq "")) {
    # Catch errors (including bad input) without aborting the script
    eval {
      use Socket;
      $iaddr = inet_aton($ENV{REMOTE_ADDR});
      $rhost = gethostbyaddr($iaddr, AF_INET);
  if (!$UseLookup || !defined($rhost) || $rhost eq "") {
    $rhost = $ENV{REMOTE_ADDR};
  $rhost = &GetMaskedHost($rhost)  if ($doMask);
  return $rhost;

3. Results / Questions / Comments

This setup "works" in that the wiki script is only loaded once - when apache starts and then all browser requests are handled by this precompiled and loaded script. No perl startup time, no wiki script compile at runtime. Great! Though I the script still bails in certain situations and I also have many warnings in my error-log that i want to get rid off. (No, I don't like the idea to switch warnings off to get rid of warnings. It tends to hide "real" warnings for me.)

3.1. Errors

When opening a wiki page for edit I get a Server Confiuration error message at the bottom and the error-log says:
[Fri Apr 29 10:09:24 2005] [error] handler UseModWiki::DoWikiRequest didn't return a valid return value!
No other helpful messages that I can find anywhere. Any ideas how to nail this one anyone? I get similar errors from some other non-browsing wiki actions too. I'm hoping they have the same source of error.

3.2. Warnings

Even with the initialization of some global variables I have lots of warning messages in my error-log related to uninitialized variables. The function InitCookie is particularly active in generating warnings until I edit my wiki prefs again and then they dissapear. Note that even when my wiki prefs are edited (and it says my username is PEZ) I get warnings whenever the server is restarted and I haven't resubmitted the preferences form since.

Any ideas anyone? I also get server errors when accessing the "Edit text of this page" link. Like with the warnings above this also seems to be about global variables not being initialized...

-- PEZ

To get rid of these warnings you have to initialize the variables, or turn warnings off.--JAPH

Thanks. I've started this and it is listed under Patches above. -- PEZ


I'm completely stuck with this. I guess what this process tells me is that UseModWiki isn't mod_perl compliant. Or at least not the way I go about it. -- PEZ

I'm sorry for not replying sooner, but I did some experimenting with mod_perl a few years ago. As far as I recall, I settled for the Apache::RunPerl method rather than a pure mod_perl solution. This at least avoids creating a new process and initializing the Perl environment for each page. --CliffordAdams

Also, I think that one can avoid recompiling the wiki code by making the main script a stub which loads the wiki code like:


require "/some/path/here/umwiki.pl";
&UseModWiki::DoWikiRequest();  # Do the request

...where "umwiki.pl" is a copy of your current wiki script. Note: in the "umwiki.pl" copy, you should set the $RunCGI setting to 0 (so that the script is not run when initially loaded). I only vaguely remember playing with this possibility, however, so it is possible that you'll need to add something to the Apache config to pre-load the umwiki.pl Perl module. (If that doesn't work, you might try preloading the CGI.pm module somehow since that is also a big chunk of code.)

Finally, here are a couple links you might find useful: [PerlRun documentation] [CGI to mod_perl porting]. --CliffordAdams

Thanks for replying! Hopefully someone will pick this up and tell us if PerlRun? or the stub approach works (and how to make it work). Me, I got so stuck with my mod_perl attempts that I started to write a tool to ease a port of the wiki to MediaWiki. I'm quite close to getting something to work there so I guess MW will be it for the wiki I tried mod_perl on. I might return to PerlRun? for my other UseModWiki since it could need the boost when there's some load on it. If someone gets interested in my MW-porting approach contact me. --PEZ

The easiest way to get UseMod working under mod_perl's PerlRun? is to use a [wrapper] (stub) and setup perlrun for the stub in .htaccess with:
 <Files "stub.cgi">
 SetHandler perl-script
 PerlHandler Apache::PerlRun
That takes CPU usage down from 80-100% under mod_cgi to about 50%. Apache::Registry brings it down to around 5%, but requires an apache restart if you change anything.--JAPH

UseModWiki | AchievingPerformance | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited July 6, 2010 3:17 am by adsl-71-141-117-36.dsl.snfc21.pacbell.net (diff)