1

Before we begin, I want to say that I have read this: Perl Apache : Perl script displayed as plain text

I've read it, tried the things, didn't work out (Sadly)

I am on Ubuntu

I want to run cgi scripts, they compile fine, I have the appropriate rights to the folder (atleast I don't get an error that I don't when I try to enter it through the browser)and the files run as plain text.

First the file which I am trying to run:

 #!/usr/bin/perl

 use CGI qw/:standard/;
 print header,
 h1('CGI.pm is simple.'),
 end_html; 

And I get as output the source code.

The path to the folder is /var/www/a2/

and the configuration in the apache .config file is:

ScriptAlias /a2/ /var/www/a2/
<Directory "/var/www/a2">
AddHandler cgi-script .cgi .pl
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>

The file compiles fine and prints the html in the terminal.

My only guess is that I have not given proper rights to the file.

EDIT: I checked the error log. It doesn't give an error for this.

EDIT: After a very long discussion with Mark Setchell the error was discovered. I had posted this in the wrong .config file and I had been using the wrong (unconfigured folder) all along.

Community
  • 1
  • 1
Stanimirovv
  • 3,064
  • 8
  • 32
  • 56

3 Answers3

1

Here are some things to try:

  1. Ensure your script is named with the extension ".pl"

  2. Ensure your script is executable. Do this by typing:

    chmod +x yourscript.pl

  3. As Mark says, ensure you have the shebang as the very first line of your script:

    #!/usr/bin/perl

  4. Remove the "1;" at the end, you only need that for packages/modules.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 1) The script has extention .pl. 2) I executed the command as root. 3) I have the shebang. 4) I have updated the script with a new example I am using (shorter and more straightforward). It is still printed as plain text. – Stanimirovv Dec 13 '13 at 08:42
  • So, if the script is called "fred.pl", what happens when you type "./fred.pl" – Mark Setchell Dec 13 '13 at 08:43
  • Okay! We are making progress. I do not have permission to run the script. It says "permission denied". The script is tt1.pl, the command which I ran was sudo chmod -x tt1.pl and I got no error. – Stanimirovv Dec 13 '13 at 08:45
  • Just for a minute, try typing "chmod 777 tt1.pl", then try again. – Mark Setchell Dec 13 '13 at 08:46
  • It still doesn't work and now it gives an error when I try to run it ( ./tt1.pl) – Stanimirovv Dec 13 '13 at 08:49
  • Check your perl interpreter is really at /usr/bin/perl, by typing "which perl" or "whereis perl" and see if it matches the first line of your script - edit script if not. If that doesn't work, tell me what error you get when trying to run it. – Mark Setchell Dec 13 '13 at 08:50
  • Basically the first path is: "/usr/bin/perl/ so the path must be ok. Now it get's wierd. When just compiling I get that the syntax is ok, however when I try to run it I get line 3: use: command not found Warning: unknown mime-type for "header," -- using "application/octet-stream" Error: no such file "header," ./tt1.pl: line 5: syntax error near unexpected token `'CGI.pm is simple.'' ./tt1.pl: line 5: ` h1('CGI.pm is simple.'),' EDIT: Maybe a package is missing? – Stanimirovv Dec 13 '13 at 08:55
  • change the commas at the ends of the lines to semi-colons. – Mark Setchell Dec 13 '13 at 08:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43106/discussion-between-mark-setchell-and-bloodcount) – Mark Setchell Dec 13 '13 at 08:57
0

I don't see a

#!/usr/bin/perl

at the top of your script. Without it, the CGI handler won't know how to execute it.

Mark
  • 2,792
  • 2
  • 18
  • 31
  • A funny error came up! Now it is trying to download it :D Edit: Download as in the browser is asking me where to download the file. – Stanimirovv Dec 13 '13 at 08:25
0

Make sure apache cgi mode is enable

sudo a2enmod cgi   //will enable the cgi mode
sudo service apache2 restart

Make sure that the file permission to the .cgi file is okay

sudo chmod 755 yourFile.cgi

Try executing it through terminal

perl /Path_To_The_File/fileName.cgi

Ensure that your fileName.cgi contains bellow code at top of the file

#!/usr/bin/perl -w

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