moseley 02/04/23 21:12:18
Added: src/images titlegrad.gif
src/start/tips config.html handler.html logging.html
Log:
Adding new files to CVS, so I can then tag.
Revision Changes Path
1.1 modperl-docs/src/images/titlegrad.gif
<
With mod_perl, Perl code can be embedded directly in the Apache configuration file.
Perl in httpd.conf is commonly used to dynamically configure Apache, but anything from
URL translation to content generation can be accomplished directly in the configuation file.
This example reads configuration settings from a text file and configures Apache's
virtual hosts.
The httpd.conf setup:
See The Guide
for other examples of configuring Apache with mod_perl.
For a more in-depth explanation of creating mod_perl handlers see
Documentation.
The mod_perl Guide is also recommended
reading.
Configureing Apache using <Perl> sections
NameVirutalHost 192.168.1.22
# Read in virtual host config from a tab-delimited file.
<Perl>
open(HOSTS,'/etc/apache/vhosts.txt')
or die "Failed to open vhosts.txt: $!";
while (<HOSTS>) {
my %config;
my @params = qw/ServerName DocumentRoot ErrorLog TransferLog ServerAdmin/;
@config{ @params } = split /\t/;
push @{$VirtualHost{'192.168.1.22'}}, \%config;
}
close HOSTS;
</Perl>
Creating a content handler with mod_perl
Handlers are simply perl subroutines called by the server at various stages of the HTTP request cycle.
A content handler is a subroutine that is called by the response phase. Handlers, are
typically created as a perl modules, separate files store in a library, and accessable via perl's @INC array.
For example, here's an example that returns a greeting and the current local time.
package My::Greeting;
use strict;
sub handler {
my $r = shift;
my $now = scalar localtime;
my $server_name = $r->server->server_hostname;
$r->send_http_header('text/plain');
print <<EOT;
Thanks for visiting $server_name.
The local time is $now
EOT
return Apache::Constants::OK;
}
1; # modules must return true
Save the above as a file file in your perl library (e.g. My/Greeting.pm).
Now, to return the above greeting when the URL /hello is visited on your server:
<location /hello>
SetHandler perl-script
PerlHandler My::Greeting
</files>
Creating a PerlLogHandler
Every request phase can be controlled using mod_perl. Here's an example
of a PerlLogHandler. The PerlLogHandler is one of the last phases of the request
cycle.
This example sends mail when a request is made to the /private section of your web space. A more common use of a PerlLogHandler might be to track hits on a specific set of URLs, or to write logging data to a relational database.
package My::Notify; use strict; use Apache::Constants(':common'); use Mail::Send; sub handler { my $r = shift; my $email = $r->server->server_admin || return DECLINED; my $mail = Mail::Send->new( To => $email, Subject => "mod_perl Notification", ); my $file = $r->filename; my $fh = $mail->open; $fh->print("File '$file' was accessed"); $fh->close; return DECLINED; # let apache write to the lot } 1; # modules must return true
The httpd.conf setup:
<location /private> SetHandler perl-script PerlLogHandler My::Notify </location>--------------------------------------------------------------------- To unsubscribe, e-mail: docs-cvs-unsubscribe@perl.apache.org For additional commands, e-mail: docs-cvs-help@perl.apache.org