如何配置Apache 2运行Perl CGI脚本?

48
我想配置在Kubuntu上运行的Apache 2以执行Perl CGI脚本。我尝试了一些通过谷歌搜索得到的步骤,但似乎没有什么作用。如何正确实现这一点?

2
如果您提供您已经尝试过的链接并说明它们无法正常工作,那么这可能会有所帮助。 - Sam Hasler
2
你在错误日志中收到了哪些错误信息?通常它们会告诉你哪里出了问题。 - brian d foy
4
@cletus - 我有一堆来自工作的旧CGI脚本-它们运行良好,但将它们放在另一个Web服务器上却非常麻烦。我不打算随着洗澡水把孩子们一起扔掉。这是一篇好文章。 - capser
8个回答

22

本文旨在帮助那些在Ubuntu上无法正确设置Apache2和Perl而遭受困扰的人(特定于您的Linux机器的系统配置将在方括号中提及,例如[此处])。

未正确设置Apache 2可能会导致以下可能结果:

  1. 浏览器尝试下载.pl文件而不是执行并输出结果。
  2. 被禁止。
  3. 内部服务器错误。

如果按照下面描述的步骤进行,并具备合理的智力,就可以通过上述错误。

在开始这些步骤之前,请进入/etc/hosts文件,并添加IP地址/域名,例如:

127.0.0.1 www.BECK.com

步骤1:安装apache2

步骤2:安装mod_perl

步骤3:配置apache2

打开sites-available/default文件,并添加以下内容:

<Files ~ "\.(pl|cgi)$">
    SetHandler perl-script
    PerlResponseHandler ModPerl::PerlRun
    Options +ExecCGI
    PerlSendHeader On
</Files>

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory [path-to-store-your-website-files-like-.html-(perl-scripts-should-be-stored-in-cgi-bin] >
####(The Perl/CGI scripts can be stored out of the cgi-bin directory, but that's a story for another day. Let's concentrate on washing out the issue at hand)
####
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ [path-where-you-want-your-.pl-and-.cgi-files]

<Directory [path-where-you-want-your-.pl-and-.cgi-files]>
    AllowOverride None
    Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
    AddHandler cgi-script .pl
    Order allow,deny
    allow from all
</Directory>
<Files ~ "\.(pl|cgi)$">
    SetHandler perl-script
    PerlResponseHandler ModPerl::PerlRun
    Options +ExecCGI
    PerlSendHeader On
</Files>

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory [path-to-store-your-website-files-like-.html-(perl-scripts-should-be-stored-in-cgi-bin] >
####(The Perl/CGI scripts can be stored out of the cgi-bin directory, but that's a story for another day. Let's concentrate on washing out the issue at hand)
####
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ [path-where-you-want-your-.pl-and-.cgi-files]

<Directory [path-where-you-want-your-.pl-and-.cgi-files]>
    AllowOverride None
    Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
    AddHandler cgi-script .pl
    Order allow,deny
    allow from all
</Directory>

第四步:

将以下内容添加到你的/etc/apache2/apache2.conf文件中。

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

<IfModule mod_perl.c>
<IfModule mod_alias.c>
Alias /perl/ /home/sly/host/perl/
</IfModule>
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
</Location>
</IfModule>

<Files ~ "\.pl$">
Options +ExecCGI
</Files>

步骤5:

非常重要,至少我认为是这样的,只有在完成这一步后,我才使它成功了。

AddHandler cgi-script .cgi .pl

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

<IfModule mod_perl.c>
<IfModule mod_alias.c>
Alias /perl/ /home/sly/host/perl/
</IfModule>
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
</Location>
</IfModule>

<Files ~ "\.pl$">
Options +ExecCGI
</Files>

第六步

非常重要,或者至少我认为是这样的,只有在完成这一步之后,我才让它正常工作。

将以下内容添加到您的/etc/apache2/sites-enabled/000-default文件中。

<Files ~ "\.(pl|cgi)$">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI
PerlSendHeader On
</Files>

第七步:

现在,在之前第三步所提到的位置 [你想放置.pl和.cgi文件的路径],添加你的Perl脚本作为test.pl。

使用chmod.pl文件授予权限,然后在浏览器的地址栏中输入webaddress/cgi-bin/test.pl,就可以了。

(现在,这篇文章中许多内容可能已经重复了,请忽略它。)


16
运行 Perl 需要一百万行代码? - Dims
1
我不理解步骤5和6。您解释说这很重要。您给出一个命令 AddHandler cgi-script .cgi .pl。然后是一段代码块。我们应该把它放在哪里? - cpb2
有没有办法将 .cgi 部署到其他类型的服务器上?比如 puma :| - user4710450
哇...这看起来很复杂。难道没有更简单的方法吗?看起来像是重复造轮子 :( - gpwr

20

你需要查看Apache错误日志以了解"内部服务器错误"的原因。根据我的经验,最可能的四种情况是:

  1. CGI程序所在的目录没有启用CGI执行。解决方案:通过httpd.conf或.htaccess文件向该目录添加ExecCGI选项。

  2. Apache仅配置为从专用目录运行CGI。解决方案:将CGI程序移动到该目录中,或在httpd.conf中添加AddHandler cgi-script .cgi语句。

  3. CGI程序未设置为可执行文件。解决方案(假设使用*nix类型的操作系统):chmod +x my_prog.cgi

  4. CGI程序退出时未发送标头。解决方案:从命令行运行程序并验证a)它是否实际运行,而不是出现编译时错误,b)它生成正确的输出,其中应包括至少一个Content-Type标头和其最后一个标头之后的空行。


非常感谢您的提问。不过,我不得不在/etc/apache2/sites-enabled/default中进行更改,而不是httpd.conf文件,然后一切都正常了。httpd.conf是一个空文件。有没有什么原因我不需要在httpd.conf文件中进行更改呢? - undefined
处理配置文件的更“现代”的方式是将所有内容放入部分配置文件的群集中(在sites-enabled、mods-enabled和conf.d中),以便程序可以更轻松地操作它们。当您这样做时,httpd.conf就不再需要,可以使用或不使用。 - Dave Sherohman

19

(即使我没有询问perl,但Google搜索将我带到了这个问题)

我在运行脚本时遇到了问题(虽然不是用perl而是bash)。Apache的配置为ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/但是Apache错误日志显示File does not exist: /var/www/cgi-bin/test.html

尝试将脚本放置在/usr/lib/cgi-bin//var/www/cgi-bin/两个位置,但都没有起作用。

经过长时间的Google搜索后,sudo a2enmod cgi解决了我的问题,并使用/usr/lib/cgi-bin/一切正常。


1
谢谢,在Debian 7上你需要运行a2enmod cgi否则它不会工作。 - Andrea Gottardo
"a2enmod cgi" 对我也起了作用!这个答案真的值得更多的投票。 - いちにち
谢谢!我简直不敢相信我先前找到的十几页资料都没有提到这个简单、小巧、重要的事情。 - Stefan Vorkoetter
此外,在您的<Directory>中添加以下内容:Options +ExecCGI AddHandler cgi-script .cgi .pl也可以在这里看到:https://dev59.com/pWIj5IYBdhLWcg3whFa4#20473841 - Meetai.com

7
如果您已经成功安装了Apache Web服务器和Perl,请按照以下步骤在Ubuntu系统上使用Perl运行cgi脚本。
在开始CGI脚本之前,有必要配置Apache服务器,使其识别CGI目录(保存cgi程序的位置)并允许在该目录中执行程序。
  1. In Ubuntu cgi-bin directory usually resides in path /usr/lib , if not present create the cgi-bin directory using the following command.cgi-bin should be in this path itself.

     mkdir /usr/lib/cgi-bin
    
  2. Issue the following command to check the permission status of the directory.

     ls -l /usr/lib | less
    

检查权限是否显示为“drwxr-xr-x 2 root root 4096 2011-11-23 09:08 cgi-bin”,如果是,请执行第3步。

如果不是,请执行以下命令,以确保我们的cgi-bin目录具有适当的权限。

     sudo chmod 755 /usr/lib/cgi-bin
     sudo chmod root.root /usr/lib/cgi-bin
  1. Give execution permission to cgi-bin directory

     sudo chmod 755 /usr/lib/cgi-bin
    

你的cgi-bin目录已准备就绪。这是你放置所有可执行cgi脚本的地方。我们接下来需要配置Apache识别cgi-bin目录并允许其中的所有程序执行为cgi脚本。

配置Apache使用perl运行CGI脚本

  1. A directive need to be added in the configuration file of apache server so it knows the presence of CGI and the location of its directories. Initially go to location of configuration file of apache and open it with your favorite text editor

    cd /etc/apache2/sites-available/ 
    sudo gedit 000-default.conf
    
  2. Copy the below content to the file 000-default.conf between the line of codes “DocumentRoot /var/www/html/” and “ErrorLog $ {APACHE_LOG_DIR}/error.log”

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Require all granted
    

  3. Restart apache server with the following code

    sudo service apache2 restart
    
  4. Now we need to enable cgi module which is present in newer versions of ubuntu by default

    sudo a2enmod cgi.load
    sudo a2enmod cgid.load
    
  5. At this point we can reload the apache webserver so that it reads the configuration files again.

    sudo service apache2 reload
    

现在Apache的配置部分已经结束,让我们通过一个示例CGI Perl程序来检查它。

测试一下

  1. Go to the cgi-bin directory and create a cgi file with extension .pl

    cd /usr/lib/cgi-bin/
    sudo gedit test.pl
    
  2. Copy the following code on test.pl and save it.

    #!/usr/bin/perl -w
    print “Content-type: text/html\r\n\r\n”;
    print “CGI working perfectly!! \n”;
    
  3. Now give the test.pl execution permission.

    sudo chmod 755 test.pl
    
  4. Now open that file in your web browser http://Localhost/cgi-bin/test.pl

  5. If you see the output “CGI working perfectly” you are ready to go.Now dump all your programs into the cgi-bin directory and start using them.

请注意:不要忘记将cgi-bin中的新程序赋予chmod 755权限,以便成功运行它而不会出现任何内部服务器错误。

5

Ubuntu 12.04(Precise Pangolin)开始(也许是之前的一个或两个版本),只需通过Synaptic安装apache2mod-perl,并将您的CGI脚本放在/usr/lib/cgi-bin中即可。


2
处理CGI脚本有两种方式:SetHandlerAddHandler
SetHandler cgi-script

适用于给定上下文中的所有文件,无论它们的名称如何,甚至包括 index.htmlstyle.css

AddHandler cgi-script .pl

这类似于针对特定上下文中以.pl结尾的文件的应用程序。如果您愿意,可以选择其他扩展名或多个扩展名。

此外,必须加载CGI模块并配置Options +ExecCGI。要激活该模块,请输入:

a2enmod cgi

然后重新启动或重新加载Apache。最后,Perl CGI脚本必须可执行。因此,必须设置执行位。

chmod a+x script.pl

并且它应该以以下内容开头

#! /usr/bin/perl

作为其第一行。
当您在任何指令之外使用SetHandlerAddHandler(和Options +ExecCGI)时,它会全局应用于所有文件。但是,您可以通过将这些指令包含在内来将上下文限制为子集,例如Directory
<Directory /path/to/some/cgi-dir>
    SetHandler cgi-script
    Options +ExecCGI
</Directory>

现在,SetHandler 只适用于 /path/to/some/cgi-dir 内的文件,而不是整个网站的所有文件。当然,在 DirectoryLocation 指令内使用 AddHandler 也是一样的。它只适用于 /path/to/some/cgi-dir 内以 .pl 结尾的文件。请注意保留HTML标签。

1

对于像我这样的人,一直在摸索比你现在需要知道的更多的教程和文档,只是想看到东西起步运作,我发现我唯一需要做的就是添加:

AddHandler cgi-script .pl .cgi

到我的配置文件中。

http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler

对于我的情况而言,这是最好的方法,因为我可以将我的perl脚本放在任何地方,并且只需添加 .pl 或 .cgi 扩展名即可。
Dave Sherohman 的回答中也提到了 AddHandler 解决方案。
当然,你还必须确保脚本的权限/所有权设置正确,特别是脚本将被执行。请注意,从 http 请求运行时的“用户”是谁 - 例如,www 或 www-data。

1

我猜你已经看过mod_perl了吧?

你试过下面这个tutorial吗?

编辑:关于你的帖子,也许你可以在.cgi文件中包含一些代码示例。甚至是前几行?


是的,我已经安装了libapache2-mod-perl2,但是当我尝试使用http://localhost/cgi-bin/mypage.cgi访问我的页面时,仍然出现“内部服务器错误”。 - undefined
那么也许mypage.cgi有问题? - innaM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接