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.
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:
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:
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:
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:
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:
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.