简体   繁体   English

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

[英]How do I configure Apache 2 to run Perl CGI scripts?

I would like to configure Apache 2 running on Kubuntu to execute Perl CGI scripts.我想配置运行在Kubuntu上的Apache 2来执行Perl CGI脚本。 I've tried some steps that I came across by googling, but nothing seems to work.我已经尝试了通过谷歌搜索遇到的一些步骤,但似乎没有任何效果。

What is the right way of achieving this?实现这一目标的正确方法是什么?

This post is intended to rescue the people who are suffering from *not being able to properly setup Apache2 for Perl on Ubuntu. (The system configurations specific to your Linux machine will be mentioned within square brackets, like [this]).这篇文章旨在拯救遭受*无法在 Ubuntu 上为 Perl 正确设置 Apache2 的人们。(特定于您的 Linux 机器的系统配置将在方括号中提及,例如 [this])。

Possible outcome of an improperly setup Apache 2:设置不当的可能结果 Apache 2:

  1. Browser trying to download the.pl file instead of executing and giving out the result.浏览器尝试下载 .pl 文件而不是执行并给出结果。
  2. Forbidden.禁止。
  3. Internal server error.内部服务器错误。

If one follows the steps described below with a reasonable intelligence, he/she can get through the errors mentioned above.如果一个人以合理的智慧遵循下面描述的步骤,他/她就可以克服上面提到的错误。

Before starting the steps.在开始这些步骤之前。 Go to /etc/hosts file and add IP address / domain-name` for example: Go 到/etc/hosts文件并添加 IP 地址/域名`例如:

127.0.0.1 www.BECK.com

Step 1: Install apache2 Step 2: Install mod_perl Step 3: Configure apache2第一步:安装apache2第二步:安装mod_perl第三步:配置apache2

open sites-available/default and add the following,打开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>

Step 4:第4步:

Add the following lines to your /etc/apache2/apache2.conf file.将以下行添加到您的/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>

Step 5:第 5 步:

Very important , or at least I guess so, only after doing this step, I got it to work.非常重要,或者至少我猜是这样,只有在完成这一步之后,我才能让它发挥作用。

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>

Step 6步骤 6

Very important , or at least I guess so, only after doing this step, I got it to work.非常重要,或者至少我猜是这样,只有在完成这一步之后,我才能让它发挥作用。

Add the following to you /etc/apache2/sites-enabled/000-default file将以下内容添加到/etc/apache2/sites-enabled/000-default文件

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

Step 7:第 7 步:

Now add, your Perl script as test.pl in the place where you mentioned before in step 3 as [ path-where-you-want-your-.pl-and-.cgi-files ].现在将您的 Perl 脚本作为 test.pl 添加到您之前在第 3 步中提到的位置 [ path-where-you-want-your-.pl-and-.cgi-files ]。

Give permissions to the .pl file using chmod and then, type the webaddress/cgi-bin/test.pl in the address bar of the browser, there you go, you got it.使用chmod授予.pl文件权限,然后在浏览器的地址栏中键入webaddress/cgi-bin/test.pl ,go,你知道了。

(Now, many of the things would have been redundant in this post. Kindly ignore it.) (现在,这篇文章中的许多内容都是多余的。请忽略它。)

You'll need to take a look at your Apache error log to see what the "internal server error" is.您需要查看 Apache 错误日志以了解“内部服务器错误”是什么。 The four most likely cases, in my experience would be:根据我的经验,四种最可能的情况是:

  1. The CGI program is in a directory which does not have CGI execution enabled. CGI 程序位于未启用 CGI 执行的目录中。 Solution: Add the ExecCGI option to that directory via either httpd.conf or a .htaccess file.解决方案:通过 httpd.conf 或 .htaccess 文件将ExecCGI选项添加到该目录。

  2. Apache is only configured to run CGIs from a dedicated cgi-bin directory. Apache 仅配置为从专用的cgi-bin目录运行 CGI。 Solution: Move the CGI program there or add an AddHandler cgi-script.cgi statement to httpd.conf.解决办法:将CGI程序移到那里或者在httpd.conf中添加一条AddHandler cgi-script.cgi语句。

  3. The CGI program is not set as executable. CGI 程序未设置为可执行。 Solution (assuming a *nix-type operating system): chmod +x my_prog.cgi解决方案(假设是 *nix 类型的操作系统): chmod +x my_prog.cgi

  4. The CGI program is exiting without sending headers. CGI 程序在不发送标头的情况下退出。 Solution: Run the program from the command line and verify that a) it actually runs rather than dying with a compile-time error and b) it generates the correct output, which should include, at the very minimum, a Content-Type header and a blank line following the last of its headers.解决方案:从命令行运行程序并验证 a) 它实际运行而不是因编译时错误而死掉,以及 b) 它生成正确的 output,其中至少应包括Content-Type header 和最后一个标题之后的空行。

(Google search brought me to this question even though I did not ask for perl) (谷歌搜索让我想到了这个问题,尽管我没有要求 perl)

I had a problem with running scripts (albeit bash not perl).我在运行脚本时遇到了问题(尽管 bash 不是 perl)。 Apache had a config of ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ however Apache error log showed File does not exist: /var/www/cgi-bin/test.html . Apache 的配置为ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/但是 Apache 错误日志显示File does not exist: /var/www/cgi-bin/test.html

Tried putting the script in both /usr/lib/cgi-bin/ and /var/www/cgi-bin/ but neither were working.尝试将脚本放在/usr/lib/cgi-bin//var/www/cgi-bin/中,但都没有用。

After a prolonged googling session what cracked it for me was sudo a2enmod cgi and everything fell into place using /usr/lib/cgi-bin/ .经过长时间的谷歌搜索 session 对我来说破解它的是sudo a2enmod cgi并且使用/usr/lib/cgi-bin/一切都到位了。

If you have successfully installed Apache web server and Perl please follow the following steps to run cgi script using perl on ubuntu systems.如果您已成功安装 Apache web 服务器和 Perl 请按照以下步骤在 ubuntu 系统上使用 perl 运行 cgi 脚本。

Before starting with CGI scripting it is necessary to configure apache server in such a way that it recognizes the CGI directory (where the cgi programs are saved) and allow for the execution of programs within that directory.在开始使用 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.在 Ubuntu 中,cgi-bin 目录通常位于路径 /usr/lib 中,如果不存在,请使用以下命令创建 cgi-bin 目录。 cgi-bin should be in this path itself. cgi-bin本身应该在这个路径中。

     mkdir /usr/lib/cgi-bin
  2. Issue the following command to check the permission status of the directory.发出以下命令来检查目录的权限状态。

     ls -l /usr/lib | less

Check whether the permission looks as “drwxr-xr-x 2 root root 4096 2011-11-23 09:08 cgi- bin” if yes go to step 3.检查权限是否为“drwxr-xr-x 2 root root 4096 2011-11-23 09:08 cgi-bin”,如果是 go 到第 3 步。

If not issue the following command to ensure the appropriate permission for our cgi-bin directory.如果没有发出以下命令以确保我们的 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赋予cgi-bin目录执行权限

     sudo chmod 755 /usr/lib/cgi-bin

Thus your cgi-bin directory is ready to go. This is where you put all your cgi scripts for execution.因此,您的 cgi-bin 目录已准备好到 go。这是您放置所有要执行的 cgi 脚本的地方。 Our next step is configure apache to recognize cgi-bin directory and allow execution of all programs in it as cgi scripts.我们的下一步是配置 apache 以识别 cgi-bin 目录并允许执行其中的所有程序作为 cgi 脚本。

Configuring Apache to run CGI script using perl配置 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.需要在 apache 服务器的配置文件中添加一个指令,以便它知道 CGI 的存在及其目录的位置。 Initially go to location of configuration file of apache and open it with your favorite text editor最初 go 到 apache 配置文件的位置,然后用你喜欢的文本编辑器打开它

    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”将以下内容复制到文件 000-default.conf 代码行“DocumentRoot /var/www/html/”和“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使用以下代码重启apache服务器

    sudo service apache2 restart
  4. Now we need to enable cgi module which is present in newer versions of ubuntu by default现在我们需要启用默认情况下出现在较新版本 ubuntu 中的 cgi 模块

    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.此时我们可以重新加载 apache 网络服务器,以便它再次读取配置文件。

     sudo service apache2 reload

The configuration part of apache is over now let us check it with a sample cgi perl program. apache 的配置部分现在已经结束,让我们用一个示例 cgi perl 程序来检查它。

Testing it out测试一下

  1. Go to the cgi-bin directory and create a cgi file with extension.pl Go 到cgi-bin目录下,创建一个扩展名为.pl的cgi文件

    cd /usr/lib/cgi-bin/ sudo gedit test.pl
  2. Copy the following code on test.pl and save it.将以下代码复制到 test.pl 并保存。

     #:/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.现在给 test.pl 执行权限。

     sudo chmod 755 test.pl
  4. Now open that file in your web browser http://Localhost/cgi-bin/test.pl现在在您的 web 浏览器中打开该文件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.如果您看到 output “CGI working perfectly”,您就可以使用 go。现在将所有程序转储到 cgi-bin 目录并开始使用它们。

NB: Don't forget to give your new programs in cgi-bin, chmod 755 permissions so as to run it successfully without any internal server errors.注意:不要忘记在 cgi-bin 中为您的新程序提供 chmod 755 权限,以便在没有任何内部服务器错误的情况下成功运行它。

As of Ubuntu 12.04 (Precise Pangolin) (and perhaps a release or two before) simply installing apache2 and mod-perl via Synaptic and placing your CGI scripts in /usr/lib/cgi-bin is really all you need to do.Ubuntu 12.04 (Precise Pangolin)(可能是之前的一两个版本)开始,只需通过 Synaptic 安装apache2mod-perl并将 CGI 脚本放在 /usr/lib/cgi-bin 中,这就是您真正需要做的全部。

There are two ways to handle CGI scripts, SetHandler and AddHandler .有两种方法可以处理 CGI 脚本, SetHandlerAddHandler

SetHandler cgi-script

applies to all files in a given context , no matter how they are named, even index.html or style.css .适用于给定上下文中的所有文件,无论它们如何命名,甚至是index.htmlstyle.css

AddHandler cgi-script .pl

is similar, but applies to files ending in .pl , in a given context.类似,但适用于在给定上下文中以.pl结尾的文件。 You may choose another extension or several, if you like.如果愿意,您可以选择另一个或多个扩展名。

Additionally, the CGI module must be loaded and Options +ExecCGI configured.此外,必须加载 CGI 模块并配置Options +ExecCGI To activate the module, issue要激活模块,发出

a2enmod cgi

and restart or reload Apache. Finally, the Perl CGI script must be executable.然后重新启动或重新加载 Apache。最后,Perl CGI 脚本必须是可执行的。 So the execute bits must be set所以必须设置执行位

chmod a+x script.pl

and it should start with它应该从

#! /usr/bin/perl

as its first line.作为它的第一行。


When you use SetHandler or AddHandler (and Options +ExecCGI ) outside of any directive, it is applied globally to all files.当您在任何指令之外使用SetHandlerAddHandler (和Options +ExecCGI )时,它会全局应用于所有文件。 But you may restrict the context to a subset by enclosing these directives inside, eg Directory但是您可以通过将这些指令包含在内部来将上下文限制为一个子集,例如Directory

<Directory /path/to/some/cgi-dir>
    SetHandler cgi-script
    Options +ExecCGI
</Directory>

Now SetHandler applies only to the files inside /path/to/some/cgi-dir instead of all files of the web site.现在SetHandler仅适用于 /path/to/some/cgi-dir 内的文件,而不适用于 web 站点的所有文件。 Same is with AddHandler inside a Directory or Location directive, of course.当然,在DirectoryLocation指令中使用AddHandler也是如此。 It then applies to the files inside /path/to/some/cgi-dir, ending in .pl .然后它适用于 /path/to/some/cgi-dir 中以.pl结尾的文件。

For those like me who have been groping your way through much-more-than-you-need-to-know-right-now tutorials and Docs, and just want to see the thing working for starters, I found the only thing I had to do was add:对于那些像我这样一直在摸索着通过比你现在需要知道的多得多的教程和文档的人来说,只是想看看对初学者有用的东西,我发现了我唯一拥有的东西要做的是添加:

AddHandler cgi-script.pl.cgi AddHandler cgi-script.pl.cgi

To my configuration file.到我的配置文件。

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

For my situation this works best as I can put my perl script anywhere I want, and just add the.pl or.cgi extension.对于我的情况,这最有效,因为我可以将我的 perl 脚本放在任何我想要的地方,并且只需添加 .pl 或 .cgi 扩展名。

Dave Sherohman's answer mentions the AddHandler solution also. Dave Sherohman 的回答也提到了 AddHandler 解决方案。

Of course you still must make sure the permissions/ownership on your script are set correctly, especially that the script will be executable.当然,您仍然必须确保正确设置脚本的权限/所有权,尤其是脚本是可执行的。 Take note of who the "user" is when run from an http request - eg, www or www-data.记下从 http 请求运行时“用户”是谁 - 例如,www 或 www-data。

I'm guessing you've taken a look at mod_perl ?我猜您已经看过mod_perl了吧?

Have you tried the following tutorial ?您是否尝试过以下教程

EDIT: In relation to your posting - perhaps you could include a sample of the code inside your .cgi file.编辑:关于你的帖子——也许你可以在你的.cgi文件中包含一个代码示例。 Perhaps even the first few lines?甚至前几行?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM