Lunarpages Web Hosting Forum

Author Topic: CGI/SSI frustrations and woes  (Read 19331 times)

Offline Mclea8

  • Newbie
  • *
  • Posts: 4
CGI/SSI frustrations and woes
« on: August 08, 2012, 03:51:45 PM »
Hi all,
I'm having various frustrations trying to get a perl cgi script to work. I'm trying to implement a very simple "hello world" script with no success. Here are the details.

Contents of my index.shtml file:

<html>
<!--#exec cgi="/cgi-bin/test.pl"  -->
<!--#include virtual="/cgi-bin/test.pl" -->

</html>

And the contents of the test.pl file:

#!/usr/bin/perl
print "Content-type: text/html\r\n\r\n";
print "Hello, World.";

1;

As you can see, I'm trying two different ways simultaneously. The SSI "include virtual" directive does throw and error but the "exec cgi" does not. Server error is:

unable to include "/cgi-bin/test.pl" in parsed file /home/<username>/public_html/index.shtml

When I access the index.shtml page, I get the "[an error occurred while processing this directive]" generated from the SSI call but nothing from the CGI call. I expect nothing from the CGI call because when I call the file directly (www.mywebsite.com/cgi-bin/test.pl) I get nothing. The script works perfectly when initiated from shell on my local system. I feel like the problem is in my htaccess file which I've tried numerous variations but the following is what is currently in place:

AddType text/html .shtml
AddHandler server-parsed .shtml
Options Indexes FollowSymLinks Includes
Options +ExecCGI
AddHandler cgi-script .cgi .pl

Also worth noting, all files have 755 permissions, and the .htaccess file is located in the public_html directory. Does anyone have an idea of what may be causing my issues? I feel like its something simple but I'm at wits end!

Thanks in advance for any input or advice.


Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6423
Re: CGI/SSI frustrations and woes
« Reply #1 on: August 08, 2012, 05:01:15 PM »
Let me preface my remarks by saying that I've only lightly touched on Perl CGI scripts (and SHTML, for that matter), so take this with as many grains of salt as you wish!

First of all, I wouldn't try running both methods simultaneously. Try doing one or the other at a time -- they may be stepping on each other. When you've proven that both work separately, then try combining them.

Try \n\n instead of \r\n\r\n. Keep in mind that your server's configuration could be quite different than your sandbox on a PC.

I'm a bit confused about which method is working correctly and which is throwing errors ('unable to include'?). Running one at a time would be helpful to confirm that the Perl script itself is running OK, but it's the SHTML #include that is not working.

I have successfully run before with a pure Perl script with a .cgi file extension and 755 permissions, in a regular server directory. Is it necessary for you to mix SHTML and Perl? Is the plan to have .shtml as your main driver?

I don't think I've ever had to put in any .htaccess entries to run SHTML, so I'm concerned about the first two .htaccess lines, especially the AddType. Does it appear that the .shtml file itself is running correctly, or is that where the failure is?
Visit My Site

E-mail Me
-= From the ashes shall rise a sooty tern =-

Offline Mclea8

  • Newbie
  • *
  • Posts: 4
Re: CGI/SSI frustrations and woes
« Reply #2 on: August 08, 2012, 07:20:48 PM »
Thanks for the quick reply and tips!

Ok, I pulled out the SSI directive and am onlying trying the exec cgi directive. I also removed the extra '\r' and left the line to read "print "Content-type: text/html\n\n";" I have also renamed my .htaccess file so that it is no longer being used.

The output is still confusing me though. I believe the shtml file is infact doing what it should but the cgi script is not being executed. The reason I think this is that when I call the script directly ( www.mywebsite.com/cgi-bin/test.pl ) I get no output. Even when I "view source" in my browser, there is nothing! Also no errors are being thrown in my log.

So all that to say, do you have any suggestions on troubleshooting steps for the perl script execution? Once again, my gut instinct is telling me that there must be something to let the server know that the script is to be executed, hence the reason I initially included the htaccess file.

Just for completeness, I tried the SSI option and I get the the "[an error occurred while processing this directive]" from my shtml and the "unable to include "/cgi-bin/test.pl" in parsed file /home/<username>/public_html/index.shtml" in my log file like before. So neither way is working.

Thanks again!

Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6423
Re: CGI/SSI frustrations and woes
« Reply #3 on: August 09, 2012, 06:00:46 AM »
The following Perl works for me:
Code: [Select]
#!/usr/bin/perl
#
#  printenv -- demo CGI program which just prints its environment
#

print "Content-type: text/html\n\n";

print qq|
<html>
<head>
<title>dump environment variables</title>
</head>
<body bgcolor="#000000" link="#00FFFF" vlink="#00FFFF" alink="#00FFFF" text="#FFFFFF">
<font size="-1" face="arial" color="#FFFFFF">
|;

foreach $var (sort(keys(%ENV))) {
    $val = $ENV{$var};
    $val =~ s|\n|\\n|g;
    $val =~ s|"|\\"|g;
    print "${var}=\"${val}\"<br>";
}

$date=`date`;
$time=time();

print qq|
Date=$date<br>
Time=$time<br>
|;

print qq|
</body>
</html>
|;

exit;

The file is named printenv.cgi, is 755, and is in a "regular" site directory. I do not have any entries in my .htaccess for special treatment of .cgi or .pl, and the file is invoked directly from the browser (no .shtml). Perhaps you can build your page from that framework, avoiding using an .shtml. If you must use SHMTL for other reasons, I would see if I could get the "bare" Perl code working by itself first, just to confirm that the problem is in your .shtml and not in the .pl.

I'm not going to claim expertise in .shtml, but I understand that "include" and "exec" can be quite tricky to use properly, especially if you want the .pl file to be processed through Perl rather than simply thrown over the wall as-is.
Visit My Site

E-mail Me
-= From the ashes shall rise a sooty tern =-

Offline Mclea8

  • Newbie
  • *
  • Posts: 4
Re: CGI/SSI frustrations and woes
« Reply #4 on: August 09, 2012, 03:08:25 PM »
First, thank you for bearing with me though this...

Here's another tid-bit of info, something is keeping my scripts from being executed. I created the exact file you posted and placed in the public_html directory, with 755 permissions. When I call the file directly from my browser, the contents of the file are displayed. There must be something that I'm missing that causes that file to be executed. I have no .htaccess and as far as I can tell, it should be the same configuration as what you describe.

For grins and giggles, I also copied the file to the cgi-bin and there, it does nothing just like my hello world script. I also doubled checked all my directories, including my public_html and all have 755 permissions.

So you you have a .htaccess file at all? I still feel like there's something simple I'm missing.

Thanks again for all your advice and help!

Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6423
Re: CGI/SSI frustrations and woes
« Reply #5 on: August 09, 2012, 04:27:29 PM »
That's very strange. Yes, I have an .htaccess, but it has nothing in it regarding Perl or CGI. Are you on a Linux shared server (like me) or on VPS or dedicated, or on Windows? Any of those could require a different setup than my server. I don't routinely use Perl on my site, so I can't speak much from experience.
Visit My Site

E-mail Me
-= From the ashes shall rise a sooty tern =-

Offline Mclea8

  • Newbie
  • *
  • Posts: 4
Re: CGI/SSI frustrations and woes
« Reply #6 on: August 10, 2012, 06:02:27 PM »
It has finally been resolved! As I was about to answer your post last night (I am on a Linux shared server by the way...) when I decided to bite the bullet and submit a support ticket. Turns out they were very helpful, although it is an easy solution.

It was in fact a .htaccess issue. When I added the line:

AddHandler cgi-script .cgi .pl

The cgi call went from displaying the contents of the file to nothing. Which to my understanding, makes sense because you have told the server to treat those files as cgis and not normal files. The part that I had a hunch I was missing was the line that actually tells the server it can execute them as script. So adding the following line does exactly that:

AddType application/x-httpd-cgi .cgi .pl

Problem Solved! Thank you for bearing though this and giving good troubleshooting advice!