Perl
Practical Extraction and Report Language
- Problems and Solutions
- Testing Scripts
- Two Scripts Compared - w/ Potential Problems and 'Safer'
- Macintosh Notes
- Enable Execution Anywhere
- Enable index.pl - Handling
- References
Problems and Solutions
Problem: Seeing source code of script - no execution |
|||||
Solution: Make sure Perl is fully supported in the web server environment. | |||||
By default, Perl scripts are only allowed to execute in specific directory (server/cgi-bin/). To enable other locations, see Enable Execution Anywhere below. | |||||
Problem: Getting 'Internal Server Error' Message |
|||||
Solution: Problem can be caused by multiple different reasons - make sure script meets all requirements below. | |||||
Easy troubleshooting: try to execute script manually in a terminal window,
e.g. |
|||||
Problem: Getting 'Forbidden' Message |
|||||
Solution: This message may be a bit misleading (as at least we've seen it for all-together missing file... #1 below). | |||||
|
Testing Scripts
Click to see how the web server handles different potential issues, details that may differ on different servers. All opens in a separate window (or tab).
Script | Description | Expected result on Mac |
---|---|---|
printhello-dos.pl | DOS-style linebreaks | Ok |
printhello-mac.pl | Mac-style linebreaks | Failure (Internal Server Error) |
printhello-unix.pl | Unix-style linebreaks | Ok |
printhello-unix-w-diagnostic.pl | With use diagnostics; statement |
Failure (Internal Server Error) |
printhello-unix-local.pl | Assumes perl in /local/, #!/usr/ local /bin/perl
-wT . |
Failure (Internal Server Error) |
printhello-unix-rw-only.pl | No execution flags, only read and write | Failure (Internal Server Error) |
See all scripts above plus more executed with runtime results.
Two Scripts Compared - w/ Potential Problems and 'Safer'
Script w/ potential problems | Safer |
---|---|
#!/usr/local/bin/perl #1 use strict; use diagnostics; #2 print "Content-Type: text/html\n\n"; print "<html><head>\n"; print "<title>Print World Script</title>\n"; print "</head>\n"; print "<body>\n"; print "<p>Hello, World!</p>\n"; print "</body></html>\n"; |
#!/usr/bin/perl use strict;
# use diagnostics;
print "Content-Type: text/html\n\n"; print "<html><head>\n"; print "<title>Print World Script</title>\n"; print "</head>\n"; print "<body>\n"; print "<p>Hello, World!</p>\n"; print "</body></html>\n"; |
#1 Expects Perl interpreter in /usr/local/... |
Macintosh Notes
Perl-scripts, .pl, are by default only executable
in <webserver>/cgi-bin/
- directories and the actual scrips must be physically placed in /Library/WebServer/CGI-Executables/
on the Mac. I.e. when developing scripts this can be a bit confusing and a
cause for mishaps.
Location | Comments | |
---|---|---|
1.file | /Users/johan/Sites/comgt/inuse/cgi-bin/printhello.pl |
originals, where coded |
1.web | <none> | |
2.file | /Library/WebServer/CGI-Executables/printhello.pl |
where script from 1.file must be copied to be accessible via <stdApacheServer>/cgi-bin/ |
2.web | http://comgt.com.z/cgi-bin/printhello.pl |
access to files in 2.file, /Library/.... |
3.file | <hosting servers' customer path to web site>, e.g.:
|
where files are uploaded to when publishing to Internet |
3.web | http://comgt.com/cgi-bin/printhello.pl |
published and released, accessible on Internet |
x.file: where file is located in file system; x.web: how file is accessible via web server
Default /etc/apache2/httpd.conf (== /private/etc/apache2/httpd.conf) |
---|
<IfModule alias_module> # # Redirect: Allows you to tell clients about documents that used to # exist in your server's namespace, but do not anymore. The client # will make a new request for the document at its new location. # Example: # Redirect permanent /foo http://www.example.com/bar # # Alias: Maps web paths into filesystem paths and is used to # access content that does not live under the DocumentRoot. # Example: # Alias /webpath /full/filesystem/path # # If you include a trailing / on /webpath then the server will # require it to be present in the URL. You will also likely # need to provide a <Directory> section to allow access to # the filesystem path. # # ScriptAlias: This controls which directories contain server scripts. # ScriptAliases are essentially the same as Aliases, except that # documents in the target directory are treated as applications and # run by the server when requested rather than as documents sent to the # client. The same rules about trailing "/" apply to ScriptAlias # directives as to Alias. # ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1" </IfModule> # # "/Library/WebServer/CGI-Executables" should be changed to whatever your ScriptAliased # CGI directory exists, if you have that configured. # <Directory "/Library/WebServer/CGI-Executables"> AllowOverride None Options None Order allow,deny Allow from all </Directory> |
more on why like this...:
Re: Perl Testing Perl is enabled, but Perl is a little more difficult to use. There is no real reason for this, it is just "the way things were done". You will need to put your Perl scripts into your CGI-bin directory, wherever that happens to be. You could modify your Apache configuration to run Perl from any directory, but that is not commonly done. Basically, Perl suffers in this aspect just because it was first and that is the way CGIs were done then. PHP came along and the default setting allowed PHP scripts to run from any directory. That is the only difference - the default setting. Just find your CGI bin directory and put your perl scripts in there and they should work via Apache's cgi-bin alias, whatever that happens to be. from http://discussions.apple.com/thread.jspa?messageID=6818251 |
Enable Execution Anywhere
Two steps to configure in httpd.conf
or (recommended) users/<somefile>
,
e.g.
users/virtual.conf
:
- 'AddHandler cgi-script ...' in mod_mime.c section
- ExecCGI in Options statement(s)
1. AddHandler:
<IfModule mod_mime.c> ... # AddHandler allows you to map certain file extensions to "handlers", # actions unrelated to filetype. These can be either built into the server # or added with the Action command (see below) # # If you want to use server side includes, or CGI outside # ScriptAliased directories, uncomment the following lines. # # To use CGI scripts: # #AddHandler cgi-script .cgi # 2008-03-18 Tue 16:08 JAS adding the following line AddHandler cgi-script .cgi .pl ... </IfModule>
2. For every virtual host - make sure to have ExecCGI
,
e.g.:
#======================================================================= # comgt.com.z #======================================================================= <VirtualHost *> ServerName comgt.com.z ServerAlias comgt.com.z *.comgt.com.z DocumentRoot /Users/johan/Sites/comgt/inuse/ <Directory /Users/johan/Sites/comgt/inuse/> # Options Indexes FollowSymLinks MultiViews IncludesNOEXEC Options Indexes FollowSymLinks MultiViews IncludesNOEXEC ExecCGI </Directory> </VirtualHost>
Enable index.pl - Handling
If want to allow default directory index file named index.pl, include something similar to web server configuration:
<IfModule mod_dir.c> # DirectoryIndex index.html # DirectoryIndex index.html index.shtml DirectoryIndex index.html index.shtml index.pl </IfModule>
References
- Mac-specific:
- Topic : Perl Testing: http://discussions.apple.com/thread.jspa?messageID=6818251
- p5-cgi-speedycgi for Mac OS X 10.5 Leopard: http://p5-cgi-speedycgi.darwinports.com
- Apache Tutorial: Dynamic Content with CGI: http://httpd.apache.org/docs/2.2/howto/cgi.html, include Configuring Apache to permit CGI.
- http://httpd.apache.org/docs/2.2/ - Apache HTTP Server Version 2.2 Documentation
- http://httpd.apache.org/docs/ - Apache HTTP Server Documentation
- A bit less relevent, for perl, but stumbled upon during searching for info
and could be of interest for other reasons:
- 10.4: Install Apache2, PHP5, and MySQL5 http://www.macosxhints.com/article.php?story=20051214031718251
Updated 2008-08-25