cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot get simple perl cgi script to work, after migration

silsden
Newbie
Posts: 3
Registered: 29-08-2011

Cannot get simple perl cgi script to work, after migration

I Cannot get simple perl cgi script to work - prior to trying to get anything else to work
The script is called test.pl
It consists of two lines:
#!/usr/bin/perl
print ("Hello test");
I've tried all sorts of combinations to get the file to execute - changing its location, renaming .htaccess, adding .htaccess to other folders.
It seems I should have had CGI-BIN in public.  I actually had cgi-bin (unless that was my error).  I've changed it to caps now.
On one occasion, I managed to get it to show the file contents, rather than execute it.  But now cannot repeat it.
/cgi-bin is empty
I believe this is what should happen:
I place a perl file in public/CGI-BIN
Ensure it has 700 flag settings
ensure .htaccess is in the root (ie below 'public')
------ .htaccess content: -------
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^cgi-bin(/.*|)$ CGI-BIN$1 [L,NC]
Options +ExecCGI
AddHandler cgi-script .cgi .pl
------------------
When test.pl is called from a web browser (http://ccgi.xxxxxxxx.force9.co.uk/cgi-bin/test.pl)  the file: public/CGI-BIN/test.pl  should execute. (where xxxxxxxx is my username)
Is this correct ?
As it is, I'm getting an Internal Server Error.  I've tried setting permissions for cgi-lib, CGI-LIB, .htaccess, public and test.pl to 755 to eliminate permissions as a cause.


2 REPLIES
silsden
Newbie
Posts: 3
Registered: 29-08-2011

Re: Cannot get simple perl cgi script to work, after migration

I just had another thought.  What if my script has an error ?
Indeed it does.  I tried this as a test file.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";
And all works OK.  My script needed to output an HTML header.  Undecided
I'll now work through and reduce the permission levels until it fails again.
Superuser
Superuser
Posts: 9,573
Thanks: 947
Fixes: 54
Registered: 06-04-2007

Re: Cannot get simple perl cgi script to work, after migration

Yes, for Perl scripts sending output to the browser it is essential to output the Content Type header first. Without that an Internal Server Error is generated
Quote
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, or webmaster and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log

When developing Perl scripts including use CGI::Carp qw(fatalsToBrowser); will ensure fatal errors are notified, eg the message quoted above. You can provide the Content Type header directly as you did or import the 'standard' CGI functions and use the header() function as below:

#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
print header();
print ("Hello test");

Concerning where your files go. If /public/CGI-BIN is self-contained and contains only CGI files (.cgi and .pl)  I suggest moving its contents to the root /cgi-bin and deleting /public/CGI-BIN. To activate /cgi-bin you need to disable the /.htacess file in / which rewrites references to /cgi-bin to /public/CGI-BIN. .cgi and .pl scripts need to be executable, chmod 700.
If you wish you can provide a /public/.htaccess containing the single line

AddHandler cgi-script .cgi .pl

if there is ever a need to include CGI scripts in /public.
David
David