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

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:
  print "Content-type: text/plainrnrn";
  print "mod_perl rules!n";

Save this script in the /home/httpd/perl/
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:
  print "Content-type: text/plainrnrn";
  print "mod_perl rules!n";

Of course you can write the same script using the Apache Perl API:
  my  = shift;
  ->print("mod_perl rules!n");

Save this script in the /home/httpd/perl/ 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/                  /home/httpd/perl/
  % chmod 0755   /home/httpd/perl/                  /home/httpd/perl/

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

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

  % perl /home/httpd/perl/

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:


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


Get the Free Newsletter!

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

Latest Posts

Related Stories