500内部服务器错误(在Ubuntu上工作)

我将printenv.pl文件复制到/ usr / lib / cgi-bin并运行:

chmod +rx printenv.pl 

在我的apache2.conf文件中,我添加了以下几行:

 <Files ~ "\.pl$"> Options +ExecCGI </Files> AddHandler cgi-script .pl 

并重新启动Apache服务器:

 /etc/init.d/apache2 start 

但是,当我去http://localhost/cgi-bin/printenv.pl我得到以下错误:

 Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. Apache/2.2.17 (Ubuntu) Server at localhost Port 80 

这是我的错误日志文件:

 [Thu Jul 28 19:28:43 2011] [error] [client 127.0.0.1] (2)No such file or directory: exec of '/usr/lib/cgi-bin/printenv.pl' failed [Thu Jul 28 19:28:43 2011] [error] [client 127.0.0.1] Premature end of script headers: printenv.pl 

但是这是我的/ usr / lib / cgi-bin目录的样子:

 /usr/lib/cgi-bin$ ls -l total 8 -rwxr-xr-x 1 root root 331 2011-07-28 19:01 printenv.pl -rwxr-xr-x 1 root root 335 2011-07-28 18:50 printenv.pl~ 

你能帮我解决这个…谢谢!

仔细检查脚本的第一行。

如果该行不以#!开始#! 或者perl被安装在不同于#!的path的位置#! (例如, /usr/local/bin/perl/usr/bin/perl ),你会得到这个错误。

如果perl的path显示正确,并且已经从Windows机器上复制了该脚本,则可能会被行尾的回车符绊倒。

您可以使用cat -v /usr/lib/cgi-bin/printenv.pl | head -1 cat -v /usr/lib/cgi-bin/printenv.pl | head -1来validation在行尾没有^M

如果有的话,可以使用perl -pi.bak -e 's/\r+$//' /usr/lib/cgi-bin/printenv.pl将其删除。