moseley 02/04/25 00:01:02 Modified: src/start config.cfg Added: src/start/tips config.pod favicon.pod handler.pod logging.pod registry.pod Log: Revision Changes Path 1.3 +5 -4 modperl-docs/src/start/config.cfg Index: config.cfg =================================================================== RCS file: /home/cvs/modperl-docs/src/start/config.cfg,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- config.cfg 24 Apr 2002 04:15:27 -0000 1.2 +++ config.cfg 25 Apr 2002 07:01:01 -0000 1.3 @@ -12,10 +12,11 @@ hidden => [ chapters => [ qw( - tips/registry.html - tips/handler.html - tips/logging.html - tips/config.html + tips/registry.pod + tips/handler.pod + tips/logging.pod + tips/config.pod + tips/favicon.pod ) ], ], 1.1 modperl-docs/src/start/tips/config.pod Index: config.pod =================================================================== =head1 NAME Configure Apache with Perl Example =head1 Configure virtual hosts 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: NameVirtualHost 192.168.0.1:80 <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/; $config{Directory}{$config{DocumentRoot}} = { Allow => 'from all'}; push @{$VirtualHost{'192.168.0.1:80'}}, \%config; } close HOSTS; </Perl> See L<The Guide|guide::config/Apache_Configuration_in_Perl> for other examples of configuring Apache with mod_perl. =for html <a class="more" href="../index.html#config">« back</a> 1.1 modperl-docs/src/start/tips/favicon.pod Index: favicon.pod =================================================================== =head1 NAME Example PerlTransHandler =head1 Using mod_perl to rewrite URLs Anyone that's looked at web logs will quickly see the usefulness of this little mod_perl script. It catches all requests for favicon.ico and rewrites the request to point to a vaild location. No more logs full of 404 errors. This example is adapted from the L<mod_perl Devekioer's Cookbook|http://modperlcookbook.com>, chapter 12. package Cookbook::Favicon; use Apache::Constants qw(DECLINED); use strict; sub handler { my $r = shift; $r->uri('/images/favicon.ico') if $r->uri =~ m!/favicon\.ico$! return DECLINED; } 1; And configure in F<httpd.conf> with: PerlModule Cookbook::Favicon PerlTransHandler Cookbook::Favicon Although this example could easily be accomplished with Apache's mod_rewrite module, this example demonstrates how easy it is to rewrite URLs programatically, using the power of Perl. =for html <a class="more" href="../index.html#handler">« back</a> =cut 1.1 modperl-docs/src/start/tips/handler.pod Index: handler.pod =================================================================== =head1 NAME Content Handler Example =head1 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 </location> For a more in-depth explanation of creating mod_perl handlers see L<Documentation|"../../docs/index.html">. The L<mod_perl Guide|"../../docs/1.0/guide/index.html"> is also recommended reading. =for html <a class="more" href="../index.html#handler">« back</a> 1.1 modperl-docs/src/start/tips/logging.pod Index: logging.pod =================================================================== =head1 NAME Log Handler Example =head1 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> =for html <a class="more" href="../index.html#logging">« back</a> 1.1 modperl-docs/src/start/tips/registry.pod Index: registry.pod =================================================================== =head1 NAME Apache::Registry Example =head1 Running CGI scripts with mod_perl Existing CGI scripts will run much faster under mod_perl. And converting existing CGI scripts to run under mod_perl is easy. For example, here's an existing CGI script called F<hello.cgi>. #!/usr/local/bin/perl -w use strict; use CGI; my $q = CGI->new; print $q->header, $q->start_html, $q->h1('Hello World!'), $q->end_html; This script can now be run as-is under Apache::Registry by using the following configuration in httpd.conf: <files hello.cgi> SetHandler perl-script PerlHandler Apache::Registry Options ExecCGI </files> That's basically it. Your scripts do need to be well coded, but there's even the Apache::PerlRun module to help with those "less clean" programs. So how much faster do scripts run under Apache::Registry? Obviously, it depends on the script, but the "hello.cgi" script above ran at 7.3 requests per second as a CGI script and 243.0 requests per second with Apache::Registry. =for html <small>Tested with Apache Benchmark (ab -n 1000) on Linux PIII-550Mhz, Apache version 1.3.20</small> For more information on running CGI scripts under mod_perl please see L<mod_perl FAQs|"../../docs/1.0/faqs/index.html">. =for html <a class="more" href="../index.html#registry">« back</a>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]