2

My Perl program works correctly from the command line but not as a web page.

Here is my script:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<html>\n";
print "<title> PERL CGI</title>\n";
print "<body>";
print "hello perl-cgi!!!!!!!";
print "</body>";
print "</html>\n";

Error on wepbage is: Internal Server Error

Error from error log:

[Tue Oct 04 10:04:41 2016] [error] [client 172.23.40.113] (8)Exec format error: exec of '/var/www/html/test2.pl' failed
[Tue Oct 04 10:04:41 2016] [error] [client 172.23.40.113] Premature end of script headers: test2.pl

Here is a part of /etc/httpd/conf/httpd.conf

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

These are the contents of the www/html directory

786875 -rwxr-xr-x. 1 root root 16     3. ĹĂ­j     10.54 incex.html 
786669 -rwxr-xr-x. 1 root root 22     27. zåŠ  10.00 index.html 
786688 -rwxr-xr-x. 1 root root 21     3. ĹĂ­j     08.47 info.php 
786899 -rwxr-xr-x. 1 root root 2765   3. ĹĂ­j     14.51 test.pl 
786834 -rwxr-xr-x. 1 root root 161    3. ĹĂ­j     15.36 test2.cgi 
786833 -rwxr-xr-x. 1 root root 259    4. ĹĂ­j     11.43 test2.pl
Borodin
  • 126,100
  • 9
  • 70
  • 144
user3456607
  • 67
  • 1
  • 6
  • Is test2.pl executable? Check the permissions. – Chankey Pathak Oct 04 '16 at 09:07
  • 1
    Also try using `print "Content-type:text/html\r\n\r\n";` – Chankey Pathak Oct 04 '16 at 09:09
  • -rwxr-xr-x. 1 root root 223 4. ĹĂ­j 11.10 test2.pl print "Content-type:text/html\r\n\r\n"; does not work – user3456607 Oct 04 '16 at 09:15
  • Is `/usr/bin/perl` the correct path for the server's perl executable? – Borodin Oct 04 '16 at 09:32
  • What operating system are you testing this on? What operating system did you write this program on? If the answers are "Linux" and "Windows" respectively, you may have line-ending problems, specifically on your shebang line. Try running `dos2unix` on the program file. – Dave Cross Oct 04 '16 at 09:35
  • Also worth checking if your server has a security error log. You might have `suexec` configured so that CGI programs need to be owned by the Apache user. – Dave Cross Oct 04 '16 at 09:37
  • 1
    2 Dave Cross, Both CentOs (linux) – user3456607 Oct 04 '16 at 09:37
  • 1
    I'd like to say that this is a very well-written question. The problem is clear, and all of the important information has been provided concisely. Well done. – Borodin Oct 04 '16 at 09:39
  • 2 Borodin. I am so sorry I am new in linux... this is my firts server.. How to check it? in directory /usr/bin/ is only file perl.. is ti correct? – user3456607 Oct 04 '16 at 09:40
  • I should point out that the `` element should properly be contained within a `` element, but most browsers won't mind, and it won't cause the issue you're seeing here. – Borodin Oct 04 '16 at 09:41
  • @user3456607: It sounds fine. Is this a private server? Try `which perl` just to confirm. – Borodin Oct 04 '16 at 09:42
  • I edited file but problem persists. I thin, there will be problem between httpd/apache and PERL but I have no idea where... – user3456607 Oct 04 '16 at 09:47
  • @Borodin which perl returns: /usr/bin/perl so it seems correct.. it is server in local network in my work – user3456607 Oct 04 '16 at 09:49
  • 1
    I suspect it's down to `suexec`. Look at other files in the www/html directory and ensure that your file is owner by the same user and group. – Dave Cross Oct 04 '16 at 09:51
  • `786875 -rwxr-xr-x. 1 root root 16 3. ĹĂ­j 10.54 incex.html 786669 -rwxr-xr-x. 1 root root 22 27. zĂĄĹ 10.00 index.html 786688 -rwxr-xr-x. 1 root root 21 3. ĹĂ­j 08.47 info.php 786899 -rwxr-xr-x. 1 root root 2765 3. ĹĂ­j 14.51 test.pl 786834 -rwxr-xr-x. 1 root root 161 3. ĹĂ­j 15.36 test2.cgi 786833 -rwxr-xr-x. 1 root root 259 4. ĹĂ­j 11.43 test2.pl` Root only.. html, php are correct, problem only with .pl a .cgi – user3456607 Oct 04 '16 at 09:56
  • 2
    Perhaps [edit the question](http://stackoverflow.com/posts/39848005/edit) to add information like this. It's hard to read in comments. – Dave Cross Oct 04 '16 at 09:57
  • And try looking in `/var/log/secure` for more hints. – Dave Cross Oct 04 '16 at 10:00
  • @DaveCross its empty (there is only info about connection via ssh2, nothine more) – user3456607 Oct 04 '16 at 10:05
  • @Borodin — The start and end tags for the head element are explicitly optional in the HTML spec. – Quentin Oct 04 '16 at 10:15
  • @Quentin: If you want to use those options then you must prefix your data with ` ` – Borodin Oct 04 '16 at 10:20
  • @Borodin — Browsers don't treat optional start and end tags for the head element any differently in quirks mode. – Quentin Oct 04 '16 at 10:22
  • @PerlDog: What is all of this about? I also said in the same comment *"it won't cause the issue you're seeing here"*. I was commenting on traditional well-formed HTML and no more. – Borodin Oct 04 '16 at 14:19
  • As I recall, and it's been a loooong time since I did anything like this, permissions can cause all sorts of problems in these circumstances. What are the permission on your directory (not the file, the directory)? What user and group are allowed in httpd.conf, and do they have access to the resources? Are your line endings (file line endings) correct for the platform you are using? Does test.pl run okay, and if so, what are the differences between that and test2.pl? What do you see in suexec logs? –  Oct 04 '16 at 10:34

1 Answers1

0

Thanks for your help.. now it works.. problem was very simple (and stupid). I did not see it in nano.. my script started on second line.. so it was not start with #!/usr/bin/perl

user3456607
  • 67
  • 1
  • 6