Perl
  Home arrow Perl arrow Page 4 - Introduction to mod_perl (part 2): mod...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Introduction to mod_perl (part 2): mod_perl Quickstart
By: Stas Bekman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2002-05-07

    Table of Contents:
  • Introduction to mod_perl (part 2): mod_perl Quickstart
  • Installing mod_perl Detailed
  • Configuring and Starting mod_perl Server
  • The "mod_perl rules" Apache::Registry Script
  • The "mod_perl rules" Apache Perl Module
  • Is This All I Need to Know About mod_perl?
  • Acknowledgements
  • References

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Introduction to mod_perl (part 2): mod_perl Quickstart - The "mod_perl rules" Apache::Registry Script
    (Page 4 of 8 )

    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 Articles
    More By Stas Bekman


     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway