为什么我的Perl CGI程序返回的服务器错误? [英] Why does my Perl CGI program return a server error?

查看:458
本文介绍了为什么我的Perl CGI程序返回的服务器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我钻进学习CGI和我成立了一个VBOX Ubuntu的服务器。我写的第一个程序是用Python通过ssh使用vim。然后,我在我的Windows 7工作站上安装Eclipse和创建完全相同的Perl的文件;只是一个简单的Hello World的交易。

I recently got into learning cgi and I set up an Ubuntu server in vbox. The first program I wrote was in Python using vim through ssh. Then I installed Eclipse on my Windows 7 station and created the exact same Perl file; just a simple hello world deal.

我试图运行它,我得到它500,而Python的code在同一目录(/ usr / lib目录/ cgi-bin目录)是显示了罚款。沮丧,我检查和三重检查的权限,它开始与#!的/ usr / bin中/ perl的。我还检查的AddHandler是否被设置为特等。一切都被设定罚款,并一时兴起,我决定使用vim像我使用Python文件没有写在服务器中完全相同的code。

I tried running it, and I was getting a 500 on it, while the Python code in the same dir (/usr/lib/cgi-bin) was showing up fine. Frustrated, I checked and triple-checked the permissions and that it began with #!/usr/bin/perl. I also checked whether or not AddHandler was set to .pl. Everything was set fine, and on a whim I decided to write the same exact code within the server using vim like I did with the Python file.

你瞧,它的工作。我比较了他们,心想我已经疯了,他们是完全一样的。那么,这是怎么回事?为什么一个文件在Windows 7上的Eclipse比Ubuntu的服务器用vim做了一个文件,提出不同?他们有不同的二进制头还是什么?这真的会影响我的发展环境。

Lo and behold, it worked. I compared them, thinking I'd gone mad, and they are exactly the same. So, what's the deal? Why is a file made in Windows 7 on Eclipse different than a file made in Ubuntu server with vim? Do they have different binary headers or something? This can really affect my development environment.

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Testing.";

Apache的错误日志:

Apache error log:

[Tue Aug 07 12:32:02 2012] [error] [client 192.168.1.8] (2)No such file or directory:     exec of '/usr/lib/cgi-bin/test.pl' failed
[Tue Aug 07 12:32:02 2012] [error] [client 192.168.1.8] Premature end of script headers: test.pl
[Tue Aug 07 12:32:02 2012] [error] [client 192.168.1.8] File does not exist: /var/www/favicon.ico

这是持续的错误,我得到。

This is the continuing error I get.

推荐答案

我觉得你有你的Perl脚本的第一行,当你写一些虚假的 \\ r 字符它在Windows中。

I think you have some spurious \r characters on the first line of your Perl script when you write it in Windows.

例如我创建Windows上的以下文件:

For example I created the following file on Windows:

#!/usr/bin/perl

code goes here

在使用hexdump都可以看到它显示:

When viewed with hexdump it shows:


00000000  23 21 2f 75 73 72 2f 62  69 6e 2f 70 65 72 6c 0d  |#!/usr/bin/perl.|
00000010  0a 0d 0a 63 6f 64 65 20  67 6f 65 73 20 68 65 72  |...code goes her|
00000020  65 0d 0a                                          |e..|
00000023

注意 0D - \\ r ,我已经在标注出来。如果我尝试运行此使用 ./ test.pl 我得到:

Notice the 0d - \r that I've marked out in that. If I try and run this using ./test.pl I get:


zsh: ./test.pl: bad interpreter: /usr/bin/perl^M: no such file or directory

而如果我写Vim的同一code在UNIX机器上我得到:

Whereas if I write the same code in Vim on a UNIX machine I get:


00000000  23 21 2f 75 73 72 2f 62  69 6e 2f 70 65 72 6c 0a  |#!/usr/bin/perl.|
00000010  0a 63 6f 64 65 20 67 6f  65 73 20 68 65 72 65 0a  |.code goes here.|
00000020

您可以在以下几种方式之一解决这个问题:

You can fix this in one of several ways:


  1. 您或许可以让你的编辑保存UNIX行尾或类似。

  2. 您可以运行 DOS2UNIX的或保存后的文件类似

  3. 您可以使用SED:的sed -e'S / \\ r // G'或类似

  1. You can probably make your editor save "UNIX line endings" or similar.
  2. You can run dos2unix or similar on the file after saving it
  3. You can use sed: sed -e 's/\r//g' or similar.

您Apache日志应能证实这一点(如果他们不杀青记录了一下你的开发服务器上)。

Your apache logs should be able to confirm this (If they don't crank up the logging a bit on your development server).

这篇关于为什么我的Perl CGI程序返回的服务器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆