为什么我的 Perl CGI 程序返回服务器错误? [英] Why does my Perl CGI program return a server error?
问题描述
我最近开始学习 cgi,并在 vbox 中设置了一个 Ubuntu 服务器.我写的第一个程序是在 Python 中使用 vim 通过 ssh.然后我在我的 Windows 7 工作站上安装了 Eclipse 并创建了完全相同的 Perl 文件;只是一个简单的 hello world 交易.
我尝试运行它,结果是 500,而同一目录 (/usr/lib/cgi-bin) 中的 Python 代码显示正常.沮丧的是,我检查并三重检查了权限,它以#!/usr/bin/perl 开头.我还检查了 AddHandler 是否设置为 .pl.一切都设置得很好,我一时兴起决定使用 vim 在服务器中编写完全相同的代码,就像我在 Python 文件中所做的一样.
瞧,它奏效了.我比较了它们,以为我疯了,它们完全一样.那么,有什么关系呢?为什么在 Eclipse 上的 Windows 7 中制作的文件与使用 vim 在 Ubuntu 服务器中制作的文件不同?他们有不同的二进制头文件吗?这真的会影响我的开发环境.
#!/usr/bin/perl打印内容类型:文本/html\n\n";打印测试";
Apache 错误日志:
[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' 失败的[2012 年 8 月 7 日星期二 12:32:02] [错误] [客户端 192.168.1.8] 脚本头过早结束:test.pl[2012 年 8 月 7 日星期二 12:32:02] [错误] [客户端 192.168.1.8] 文件不存在:/var/www/favicon.ico
这是我得到的持续错误.
我认为在 Windows 中编写 Perl 脚本时,在第一行有一些虚假的 \r
字符.
>
例如我在 Windows 上创建了以下文件:
#!/usr/bin/perl代码在这里
使用 hexdump 查看时显示:
<前>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 |...代码去她|00000020 65 0d 0a |e..|00000023注意我在其中标记的 0d
- \r
.如果我尝试使用 ./test.pl
运行它,我会得到:
而如果我在 UNIX 机器上的 Vim 中编写相同的代码,我会得到:
<前>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 |.代码放在这里.|00000020您可以通过以下几种方式之一解决此问题:
- 您可能可以让您的编辑器保存UNIX 行尾"或类似内容.
- 您可以在保存文件后运行
dos2unix
或类似的文件 - 您可以使用 sed:
sed -e 's/\r//g'
或类似的.
您的 apache 日志应该能够确认这一点(如果他们没有在您的开发服务器上稍微加快日志记录).
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.
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.
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 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.
I think you have some spurious \r
characters on the first line of your Perl script when you write it in Windows.
For example I created the following file on Windows:
#!/usr/bin/perl
code goes here
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
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
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:
- You can probably make your editor save "UNIX line endings" or similar.
- You can run
dos2unix
or similar on the file after saving it - You can use sed:
sed -e 's/\r//g'
or similar.
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屋!