我尝试在 html 目录中执行一个简单的 hello world cgi 脚本但是失败了。
错误日志:
[Tue May 17 14:56:15.744954 2016] [cgi:error] [pid 29997] [client 192.168.1.1:16151] End of script output before headers: hello.cgi
[Tue May 17 15:00:12.474229 2016] [cgi:error] [pid 29998] [client 192.168.1.1:16597] AH01215: (2)No such file or directory: exec of '/var/www/html/hello.cgi' failed
[Tue May 17 15:00:12.475135 2016] [cgi:error] [pid 29998] [client 192.168.1.1:16597] End of script output before headers: hello.cgi
[Tue May 17 15:00:13.176640 2016] [cgi:error] [pid 29999] [client 192.168.1.1:16600] AH01215: (2)No such file or directory: exec of '/var/www/html/hello.cgi' failed
[Tue May 17 15:00:13.177591 2016] [cgi:error] [pid 29999] [client 192.168.1.1:16600] End of script output before headers: hello.cgi
[Tue May 17 15:00:13.640420 2016] [cgi:error] [pid 30000] [client 192.168.1.1:16603] AH01215: (2)No such file or directory: exec of '/var/www/html/hello.cgi' failed
[Tue May 17 15:00:13.641393 2016] [cgi:error] [pid 30000] [client 192.168.1.1:16603] End of script output before headers: hello.cgi
httpd 配置:
# Further relax access to the default document root:
<Directory "/var/www/html">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options -Indexes +FollowSymLinks +ExecCGI
AddHandler cgi-script .cgi .pl
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
hello.cgi文件:
#!/usr/bin/perl
print "Content-type: text/html\n\n"; # This is mandatory.
print "<h2>Hello world!</h2>";
答案1
将脚本文件的第一行修改为以下内容,然后重试。
#!/usr/bin/perl -w
如果这解决了问题,那么您的脚本文件包含“\r\n”而不是“\n”作为换行符,这通常是在脚本未从 Windows 或 MAC 计算机以 ASCII 模式上传时出现的情况。