为什么我的 Perl CGI 脚本不起作用?
我真的不知道如何运行 Perl 文件。 我已将 .pl 上传到 cgi-bin,然后 chmod 到 755。然后,当我去运行该文件时,我只收到 500 内部服务器错误。
**/cgi-bin/helloworld.pl**
#!/usr/bin/perl
print 'hello world';
关于我做错了什么有什么想法吗?
I really do not get how to run a Perl file. I have uploaded my .pl to the cgi-bin then chmod to 755. Then when i go to run the file i just get a 500 internal server error.
**/cgi-bin/helloworld.pl**
#!/usr/bin/perl
print 'hello world';
Any ideas as to what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
阅读官方 Perl CGI 常见问题解答。
这将回答这个问题以及您可能有的许多其他问题。
例如: "我的 CGI 脚本从命令行运行,但不是从浏览器运行(500 服务器错误)"
希望这有帮助!
Read the official Perl CGI FAQ.
That'll answer this, and many other questions you may have.
For example: "My CGI script runs from the command line but not the browser. (500 Server Error)"
Hope this helps!
您可能需要
在打印语句之前进行类似的操作。 看看 http://httpd.apache.org/docs/ 2.0/howto/cgi.html#troubleshoot
这将有助于了解您正在使用的服务器以及服务器日志中显示的确切错误消息。 我猜想,如果您使用 Apache,您会看到类似“脚本头提前结束”的信息。
You probably need something like
before your print statement. Take a look at http://httpd.apache.org/docs/2.0/howto/cgi.html#troubleshoot
It would help to know what server you are using, and the exact error message that's showing up in the server's logs. I'd guess that, if you are using Apache, you'll see something like "Premature end of script headers".
考虑使用 CGI::Carp 向浏览器输出致命错误。
use CGI::Carp qw(fatalsToBrowser);
另外,请务必使用 CGI 模块 输出任何需要的信息,例如 headers/html/whatever。 全部打印出来是错误的方法。
编辑:您肯定还可以检查某种错误日志。
Look into using CGI::Carp to output fatal errors to the browser.
use CGI::Carp qw(fatalsToBrowser);
Also, please definitely do use the CGI module to output any needed information such as headers/html/whatever. Printing it all is the wrong way to do it.
EDIT: You will also definitely be able to check an error log of some sort.
也许您需要我的 Perl 故障排除CGI脚本
Perhaps you need my Troubleshooting Perl CGI scripts
首先,找出该系统上
perl
的路径,并确保 shebang 行正确。 提供有关系统和 Web 服务器的更多信息也将有助于其他人进行诊断。然后,尝试:
First, find out the path to
perl
on that system and make sure the shebang line is correct. Giving more information about the system and the web server would also help others diagnose.Then, try:
确保您可以从 shell 提示符运行该脚本,而无需通过 Perl 调用它。 换句话说,您应该能够转到 cgi-bin 目录并输入:
并获取输出。 如果这不起作用,请修复它。 在查看输出时,第一行必须是:(
或者
text/plain
或其他一些有效的 MIME 类型。)如果不是这种情况,请修复它。
然后在打印页面正文之前必须有一个空行。 如果没有空行,您的脚本将无法作为 CGI 脚本运行。 所以你的总输出应该是这样的:
如果你可以运行你的脚本并且这就是输出,那么就会发生一些奇怪的事情。 如果 Apache 没有将错误记录到某处的
error_log
文件中,那么可能存在一些问题。Make sure that you can run the script from a shell prompt, without invoking it through Perl. In other words, you should be able to go to your cgi-bin directory and type:
and get output. If that doesn't work, fix that. In looking at the output, the first line must be:
(Or
text/plain
or some other valid MIME type.)If that's not the case, fix that.
Then you must have an empty line before the body of your page is printed. If there's no empty line, your script won't work as a CGI script. So your total output should look like this:
If you can run your script and that's the output, then there's something weird going on. If Apache is not logging the error to an
error_log
file somewhere, then maybe there's some problem with it.您是否启用 Apache 将
.pl
文件作为 CGI 脚本提供服务? 检查您的 Apache 配置文件,或者(快速但不能保证测试)尝试将文件扩展名更改为.cgi
。 另外,请确保您的 shebang 行 (#!
) 位于最顶部。 最后,如果您的服务器是 Linux,请检查行结尾是否为 Unix。 是的,从命令行测试它,并使用 strict;
以获得对潜在错误的更好反馈。Did you enable Apache to server
.pl
files as CGI scripts? Check your Apache config file, or (quick but not guaranteed test) try changing the file extension to.cgi
. Also, make sure your shebang line (#!
) is at the very top. Finally, check the line endings are Unix if your server is Linux. And yes, test it from the command-line, anduse strict;
for better feedback on potential errors.