在Apache权限问题下使用Perl运行CGI

我在Debian框中的apache2.conf下有以下条目。

 AddHandler cgi-script .cgi .pl Options +ExecCGI ScriptAlias /cgi-bin/ /var/www/mychosendir/cgi-bin/ <Directory /var/www/mychosendir/cgi-bin> Options +ExecCGI -Indexes allow from all </Directory> 

然后我有一个perl cgi脚本存储在这些目录和权限下:

 nvs@somename:/var/www/mychosendir$ ls -lhR .: total 12K drwxr-xr-x 2 nvs nvs 4.0K 2010-04-21 13:42 cgi-bin ./cgi-bin: total 4.0K -rwxr-xr-x 1 nvs nvs 90 2010-04-21 13:40 test.cgi 

但是,当我试图在Web浏览器中访问它时:

 http://myhost.com/mychosendir/cgi-bin/test.cgi 

他们给了我这个错误:

 [Wed Apr 21 15:26:09 2010] [error] [client 150.82.219.158] (8)Exec format error: exec of '/var/www/mychosendir/cgi-bin/test.cgi' failed [Wed Apr 21 15:26:09 2010] [error] [client 150.82.219.158] Premature end of script headers: test.cgi 

它出什么问题了?

更新

我在我的apache2.conf也有以下条目:

 <Files ~ "^\.ht"> Order allow,deny Deny from all </Files> 

test.cgi的内容是这样的:

 #!/usr/bin/perl -wT print "Content-type: text/html\n\n"; print "Hello, world!\n"; 

确保你有一个cgi-bin目录的<Directory>部分。 并确保“允许所有”在其中。

 <Directory /var/www/mychosendir/cgi-bin> Options +ExecCGI -Indexes allow from all </Directory> 

另外…你的ScriptAlias是/ cgi-bin /。 您的url是/ mychosendir / cgi-bin。 除非你有一些重写魔术,否则你的url可能是http://my.host.com/cgi-bin/test.cgi ,或者你需要改变你的ScriptAlias行

 ScriptAlias /mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin 

您在更新中发布的错误听起来像没有#! 在你的脚本的开始行。 你需要一个,它应该看起来像

 #!/path/to/your/perl 

你确定Apache用户/组已经读取并执行给定文件的访问吗? 通常这个用户被称为httpd或www-data。

“脚本标题提前结束”意味着CGI脚本没有打印出正确的HTTP标题。 至less需要打印出来

 Content-Type:text / html;

另外,你有

 ScriptAlias /cgi-bin/ /var/www/mychosendir/cgi-bin/ 

而你却在问

 http://myhost.com/mychosendir/cgi-bin/test.cgi 

应该是

 http://myhost.com/cgi-bin/test.cgi 

或者说

 ScriptAlias /mychosendir/cgi-bin/ /var/www/mychosendir/cgi-bin/