3

我对 perl 和 apache 服务器都是新手。我正在尝试为 CGI 脚本创建一个基本的 Hello World。这是我的 hello world CGI 文件的代码:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<H1>Hello World</H1>\n";

当我在命令行上执行 CGI 脚本 ./hello.cgi 时,它可以工作,但是当我在浏览器中打开 hello.cgi 时,它只显示 cgi 文件的文本。

当我查看 /var/log/apache2/error_log 中的 error_log 时,我可以看到 mod_perl 正在运行:

Apache/2.2.21 (Unix) DAV/2 mod_perl/2.0.5 Perl/v5.12.3 configured -- resuming normal operations

但是当我运行以下 perl 程序时,似乎我没有“MOD_PERL”环境变量:

if (exists $ENV{"MOD_PERL"}) {
   print "YAY!\n";
 }
else{
    print"mod_perl is not working\n";
 }

顺便说一句,我的 hello.cgi 文件和上面的 perl 脚本位于 /Users/myusername/Sites/

有人可以帮我正确配置 mod_perl 以便我可以在浏览器中正确查看 hello.cgi 吗?我一直在阅读 mod_perl 文档和搜索论坛很多很多小时,但无济于事。提前致谢

@SebastianStumpf 我得到了你的第一个例子,但我似乎仍然无法让 mod_perl 脚本运行。我一直在阅读文档,但我无法弄清楚。顺便说一句,谢谢你的帮助。我非常感谢

更新:我相信我成功了!谢谢您的帮助!

4

1 回答 1

3

如果您使用 Mac OS X Lion 的 Apache/Perl并且不需要 mod_perl,那么您无需配置任何东西。只需使用 .cgi 扩展名创建文件并通过 .cgi/Library/WebServer/CGI-Executables调整权限sudo chmod 755 file.cgi。该脚本将通过 CGI(不是 mod_perl)执行。我试过了,效果很好:

$ sudo -s
# cat - > /Library/WebServer/CGI-Executables/test.cgi
#!/usr/bin/perl
use strict;
use warnings;
use CGI qw/:standard/;
use Data::Dumper;
print header, start_html, h1('works'), end_html;
^D

# sudo chmod 755 /Library/WebServer/CGI-Executables/test.cgi
# exit

测试它:

$ curl -i http://localhost/cgi-bin/test.cgi
HTTP/1.1 200 OK
Date: Mon, 11 Jun 2012 22:29:24 GMT
Server: Apache/2.2.21 (Unix) DAV/2
Transfer-Encoding: chunked
Content-Type: text/html; charset=ISO-8859-1

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>works</h1>
</body>
</html>

如果您需要 mod_perl,请查看文档。介绍的配置部分应该是运行 mod_perl 所需的全部内容。

编辑:

我在 中添加了以下几行/etc/apache2/httpd.conf,重新启动了 Web 服务器并且 mod_perl 工作:

LoadModule perl_module libexec/apache2/mod_perl.so
Alias /perl /Library/WebServer/perl
<Location /perl>
    SetHandler perl-script
    PerlResponseHandler ModPerl::Registry
    Options ExecCGI
    PerlSendHeader On
    Order allow,deny
    Allow from all
</Location>

如果脚本保存在/Library/WebServer/perl/您可以看到所有 mod_perl 标头现在都可用,但我宁愿设置 Apache/mod_perl 的私有安装。MacPorts让这一切变得更容易......

于 2012-06-11T22:17:45.840 回答