ServersGetting Started with mod_perl in 30 Minutes Page 5

Getting Started with mod_perl in 30 Minutes Page 5

ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.




This is very very insecure and you should not follow this
approach on the production machine. This is good enough when you just want to
try things out and want to have as few obstacles as possible. Once you
understand how things work, you should tighten the permissions of files served
by Apache. In future articles we will talk about setting proper file
permissions.

The “mod_perl rules”
Apache::Registry Script

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

  mod_perl_rules1.pl
  ------------------
  print "Content-type: text/plainrnrn";
  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:

  mod_perl_rules1.pl
  ------------------
  #!/usr/bin/perl
  print "Content-type: text/plainrnrn";
  print "mod_perl rules!n";

Of course you can write the same script using the Apache Perl API:

  mod_perl_rules2.pl
  ------------------
  my  = shift;
  ->send_http_header('text/plain');
  ->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. Apache is
running under a user-name specified by the User directive. If the
server is running under user-name nobody, the following will do:

  % chown nobody /home/httpd/perl/mod_perl_rules1.pl                  /home/httpd/perl/mod_perl_rules2.pl
  % chmod 0755   /home/httpd/perl/mod_perl_rules1.pl                  /home/httpd/perl/mod_perl_rules2.pl

The first command makes the files belong to user nobody, the
second sets the proper execution and read permissions.

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

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends & analysis

Latest Posts

Related Stories