ted.byers has asked for the wisdom of the Perl Monks concerning the following question:
I followed one set of instructions for writing a handler for use with mod_perl. Here is what I added to the end of apache2.conf (on Ubuntu 12.04):
PerlRequire /home/ted/modperlhandler/Hello.pm <Location /time> SetHandler perl-script PerlResponseHandler Hello </Location>
And here is the handler package code:
package Hello use strict; use Apache2::RequestRec (); use Apache2::RequestIO (); use Apache2::Request; use DBI; use Apache2::Const -compile => qw(OK); sub handler { my $db = 'nutrientsdb'; my $hostname = '192.168.2.24'; my $user = 'rejbyers'; my $dbpwd = 'xxxxxxxxxxx'; my $dbh = DBI->connect("DBI:mysql:database=$db;host=$hostname",$user +,$dbpwd,{RaiseError => 1}) or die "Failed to connect to the DB.\n"; my $r = shift; my $req = Apache2::Request->new($r); my $table = $req->param; my %p = %$table; my $name = $p{'name'}; if ((defined $name) && (length($name) > 0)) { print "Hello $p{'name'}, the time is " . localtime() . "\n\n\n"; foreach my $k (keys %p) { print "$k <=> $p{$k}\n"; } } else { print "Hello World, the time is " . localtime() . "\n"; } return Apache2::Const::OK; } 1;
Obviously, the example I followed did not involve the use of DBI. If the DBI code is commented out, the Hello handler works as the example implied (though I successfully extended the example to include getting the request parameters).
I know the above example does not (yet) do anything with $dbh. I first wanted to see if I could successfully connect to the DB before running any queries. Alas, I could not take this further because Apache gives me a 500 error. On examining the logs, I see the following entry (after a timestamp)): "DBI connect ('database==192.168.2.24','rejbyers',...) failed: Access denied for user 'rejbyers'@'localhost' (using password: YES) at /home/ted/modperlhandler/Hello.pm line 22."
Now, to verify that the database server is in fact reachable, I wrote the following CGI script and placed it in /usr/lib/cgi-bin (on Ubuntu):
#!perl use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use CGI::Carp::DebugScreen ( engine => 'TT', debug => 1, lines => 5, modules => 1, environment => 1, raw_error => 1, overload => 1, ); my $query = new CGI; print $query->header; my $db = 'nutrientsdb'; my $hostname = '192.168.2.24'; my $user = 'rejbyers'; my $dbpwd = 'xxxxxxxxxxx'; my $dbh = DBI->connect("DBI:mysql:database=$db;host=$hostname",$user,$ +dbpwd,{RaiseError => 1}) or die "Failed to connect to the DB.\n"; my $title = 'My CGI Script'; &print_html_header($title); &print_body; &print_end; exit(0); sub print_html_header { print $query->start_html(shift); } sub print_end { print $query->end_html; } sub print_body { my $sql = 'SELECT * FROM FD_GROUP'; my $sth = $dbh->prepare($sql); $sth->execute; print '<table border>'; while (my $aref = $sth->fetchrow_arrayref) { my ($code,$desc) = @$aref; print "<tr><td>$code</td><td>$desc</td></tr>"; } print '</table>'; $sth->finish; }
This CGI script worked as expected. Hence, the puzzle. On the same Ubuntu (virtual) machine, my mod_perl handler fails to connect to the database server, but my cgi script, using the same credentials, does connect to the database server. Why?
As an aside, I have, in addition to mod_perl, fastcgi enabled, and Apache::DBI is installed (and, unless I misunderstood the configuration instructions, apache's httpd server is configured to use it). My worry is that all the instructional materials for using Apache::DBI and fastcgi that I have found are quite old. While I am still studying effective use of mod-perl, do fastcgi and Apache::DBI remain good options? If so, how do they fit with what I have been doing with CGI and what I am trying to learn for using mod_perl?
Thanks
Ted
|
---|