Home arrow Perl Programming arrow Page 4 - Introduction to mod_perl (part 2): mod_perl Quickstart

The "mod_perl rules" Apache::Registry Script - Perl

Last week you found out all sorts of good things about mod_perl, but you may be wondering how you get started using it. Let this article be your guide.

TABLE OF CONTENTS:
  1. Introduction to mod_perl (part 2): mod_perl Quickstart
  2. Installing mod_perl Detailed
  3. Configuring and Starting mod_perl Server
  4. The "mod_perl rules" Apache::Registry Script
  5. The "mod_perl rules" Apache Perl Module
  6. Is This All I Need to Know About mod_perl?
  7. Acknowledgements
  8. References
By: Stas Bekman
Rating: starstarstarstarstar / 2
May 07, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
As you probably know, mod_perl allows you to re-use CGI scripts written in Perl which were previously used under mod_cgi. Therefore our first test script can be as simple as:

print "Content-type: text/plain\r\n\r\n"; print "mod_perl rules!\n";
Save this script in the /home/httpd/perl/mod_perl_rules1.pl file. Notice that the shebang line is not needed with mod_perl, but you can keep it if you want. So the following script can be used as well:

#!/usr/bin/perl print "Content-type: text/plain\r\n\r\n"; print "mod_perl rules!\n";
Of course you can write the same script using the Apache Perl API:

my $r = shift; $r->send_http_header('text/plain');$r->print("mod_perl rules!\n");
Save this script in the /home/httpd/perl/mod_perl_rules2.pl file.

Now make both of the scripts executable and readable by the server. Remember that when you execute scripts from a shell, they are being executed by the user-name you are logged with. When instead you try to run the scripts by issuing requests, Apache needs to be able to read and execute them. So we make the script readable and executable by everybody:

% chmod 0755 /home/httpd/perl/mod_perl_rules1.pl \ /home/httpd/perl/mod_perl_rules2.pl
If you don't want other users to be able to read your script, you should add yourself into the groupname the webserver is running with (as defined by the Group directive) and then make the script owned by that group and then you can tighten the permissions. For example on my machine I run the server under the group httpd and I'm the only one who is in the same group, so I can do the following:

% chown stas.httpd /home/httpd/perl/mod_perl_rules1.pl \ /home/httpd/perl/mod_perl_rules2.pl % chmod 0750 /home/httpd/perl/mod_perl_rules1.pl \ /home/httpd/perl/mod_perl_rules2.pl
The first command makes the files belong to group httpd, the second sets the proper execution and read permissions.

That's secure, assuming that you have a dedicated groupname for your server, of course.

Also remember that all the directories that lead to the script should be readable and executable by the server.

You can test mod_perl_rules1.pl from the command line, since it is essentially a regular Perl script.

% perl /home/httpd/perl/mod_perl_rules1.pl
You should see the following output:

mod_perl rules!
You cannot test the second script by executing it from the command line since it uses the mod_perl API which is available only when run from within the mod_perl server.

Make sure the server is running and issue these requests using your favorite browser:

http://localhost/perl/mod_perl_rules1.pl
http://localhost/perl/mod_perl_rules2.pl

In both cases you will see on the following response:

mod_perl rules!
If you see it--congratulations! You have a working mod_perl server.

If you're using port 8080 instead of 80, you should use this number in the URL:

http://localhost:8080/perl/mod_perl_rules1.pl
http://localhost:8080/perl/mod_perl_rules2.pl

The localhost approach will work only if the browser is running on the same machine as the server. If not--use the real server name for this test, for example:

http://your.server.name/perl/mod_perl_rules1.pl

If there is any problem please refer to the error_log file for the error reports.

Now it's a time to move your CGI scripts from /somewhere/cgi-bin directory to /home/httpd/perl/ and see them running much much faster, when requested from the newly configured base URL (/perl/). If you were accessing the script as /cgi-bin/test.pl, it will now be accessed from /perl/test.pl.

Some of your scripts might not work immediately and will require some minor tweaking or even a partial rewrite to work properly with mod_perl. Chances are that if you are not practicing sloppy programming, the scripts will work without any modifications at all.

If you have a problem with your scripts, a good approach is to replace Apache::Registry with Apache::PerlRun in httpd.conf, as the latter can execute really badly written scripts. Put the following configuration directives instead in httpd.conf and restart the server:

PerlModule Apache::PerlRun <Location /perl> SetHandler perl-script PerlHandler Apache::PerlRun Options ExecCGI PerlSendHeader On allow from all </Location>
Now your scripts should work for sure, unless there is something in them mod_perl doesn't accept. We will discuss these nuances in future articles.

 
 
>>> More Perl Programming Articles          >>> More By Stas Bekman
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: