I have a module that handles database connections. It is used by standard CGI scripts as well as mod_perl2 modules.

The server hosts multiple sites, set up using Apache's VirtualHost configuration.

Using SetEnv (or PerlSetEnv), polutes across to other Apache requests, as %ENV is treated much like a mass global variable for all Apache's requests under MP2

Here is an example sample of code that does not work:

package MYDBCONFIG; use Apache2::RequestUtil; my %db = ( 'production' => { 'db1' => { 'host' => 'host1', 'name' => 'dbname1', 'user' => 'user1', 'pass' => 'pass1' }, 'db2' => { 'host' => 'host2', 'name' => 'dbname2', 'user' => 'user2', 'pass' => 'pass2' }, }, 'qa' => { 'db1' => { 'host' => 'host3', 'name' => 'dbname3', 'user' => 'user3', 'pass' => 'pass3' }, }, ); our ( $MYDBCONFIG ); # Used elsewhere for other 'magical' backwards co +de compatiblity sub new() { my $class = shift; my $dbname = shift; # Sanity checks. die( "No database name supplied") unless ($dbname); if( $ENV{'MOD_PERL'} ){ my $r = Apache2::RequestUtil->request(); $MYDBCONFIG = $r->subprocess_env('MYDB'); $r->log_error(__PACKAGE__.":[MP]: MYDB: $MYDBCONFIG"); } else { $MYDBCONFIG = $ENV{'MYDB'}; print STDERR __PACKAGE__.":[notMP]: MYDB: $MYDBCONFIG"; } my $dbhash = $db{$MYDBCONFIG}{$dbname}; die( "DB '$dbname' not defined / permitted") unless $dbhash; bless $dbhash, $class; }

This then allows MP2 and scripts alike to connect via ORMs (Class::DBI & DBIx::Class) to which ever DB is appropriate

The aim being to then configure apache for www.mysite.com and qa.mysite.com to use exactly the same code, but restrict access to db2, and use a different set of data for QA (allowing them to CRUD as much as they like).

-=( Graq )=-


In reply to mod_perl2 Apache VirtualHost ENV settings [OT?] by graq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.