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

发布于 2024-07-14 07:50:54 字数 121 浏览 4 评论 0原文

我想配置在 Kubuntu 上运行的 Apache 2 来执行 Perl CGI 脚本。 我尝试了通过谷歌搜索找到的一些步骤,但似乎没有任何效果。

实现这一目标的正确方法是什么?

I would like to configure Apache 2 running on Kubuntu to execute Perl CGI scripts. I've tried some steps that I came across by googling, but nothing seems to work.

What is the right way of achieving this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

oО清风挽发oО 2024-07-21 07:50:54

这篇文章的目的是拯救那些*无法在 Ubuntu 上为 Perl 正确设置 Apache2 的人们。 (特定于您的 Linux 机器的系统配置将在方括号内提及,如 [this])。

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>

步骤 4:

将以下行添加到 /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>

步骤6

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

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

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

步骤 7:

现在将 Perl 脚本作为 test.pl 添加到您之前在步骤 3 中提到的位置 [ 您想要的-.pl-和-.cgi-文件的路径]。

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

(现在,这篇文章中的许多内容都是多余的。请忽略它。)

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]).

Possible outcome of an improperly setup Apache 2:

  1. Browser trying to download the .pl file instead of executing and giving out the result.
  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:

127.0.0.1 www.BECK.com

Step 1: Install apache2
Step 2: Install mod_perl
Step 3: Configure apache2

open sites-available/default and add the following,

<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:

Add the following lines to your /etc/apache2/apache2.conf file.

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:

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

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

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

Step 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].

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.

(Now, many of the things would have been redundant in this post. Kindly ignore it.)

一桥轻雨一伞开 2024-07-21 07:50:54

您需要查看 Apache 错误日志以了解“内部服务器错误”是什么。 根据我的经验,四种最可能的情况是:

  1. CGI 程序位于未启用 CGI 执行的目录中。 解决方案:通过 httpd.conf 或 .htaccess 文件将 ExecCGI 选项添加到该目录。

  2. Apache 仅配置为从专用的 cgi-bin 目录运行 CGI。 解决方案:将 CGI 程序移至此处或将 AddHandler cgi-script .cgi 语句添加到 httpd.conf。

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

  4. CGI 程序在不发送标头的情况下退出。 解决方案:从命令行运行程序并验证 a) 它确实运行而不是因编译时错误而终止,b) 它生成正确的输出,其中至少应包括 Content-键入 标头,并在最后一个标头后面输入一个空行。

You'll need to take a look at your Apache error log to see what the "internal server error" is. 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. Solution: Add the ExecCGI option to that directory via either httpd.conf or a .htaccess file.

  2. Apache is only configured to run CGIs from a dedicated cgi-bin directory. Solution: Move the CGI program there or add an AddHandler cgi-script .cgi statement to httpd.conf.

  3. The CGI program is not set as executable. Solution (assuming a *nix-type operating system): chmod +x my_prog.cgi

  4. The CGI program is exiting without sending headers. 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.

挖个坑埋了你 2024-07-21 07:50:54

(即使我没有要求 perl,Google 搜索也让我想到了这个问题)

我在运行脚本时遇到了问题(尽管是 bash 不是 perl)。 Apache 的配置为 ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ ,但是 Apache 错误日志显示文件不存在:/var/www/cgi-bin/test .html。

尝试将脚本放入 /usr/lib/cgi-bin//var/www/cgi-bin/ 但都不起作用。

经过长时间的谷歌搜索后,对我来说破解的是
sudo a2enmod cgi 一切都通过 /usr/lib/cgi-bin/ 就位。

(Google search brought me to this question even though I did not ask for perl)

I had a problem with running scripts (albeit bash not 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.

Tried putting the script in both /usr/lib/cgi-bin/ and /var/www/cgi-bin/ but neither were working.

After a prolonged googling session what cracked it for me was
sudo a2enmod cgi and everything fell into place using /usr/lib/cgi-bin/.

红焚 2024-07-21 07:50:54

如果您已成功安装 Apache Web 服务器和 Perl,请按照以下步骤在 ubuntu 系统上使用 perl 运行 cgi 脚本。

在开始编写 CGI 脚本之前,必须配置 apache 服务器,使其能够识别 CGI 目录(保存 CGI 程序的位置)并允许执行该目录中的程序。

  1. 在 Ubuntu 中,cgi-bin 目录通常位于路径 /usr/lib 中,如果不存在,请使用以下命令创建 cgi-bin 目录。cgi-bin 应该位于此路径本身中。< /p>

     mkdir /usr/lib/cgi-bin 
      
  2. 发出以下命令来检查目录的权限状态。

    <前><代码> ls -l /usr/lib | 较少的

检查权限是否为“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. 给cgi-bin目录赋予执行权限

    <预><代码> sudo chmod 755 /usr/lib/cgi-bin

这样你的cgi-bin目录就准备好了。 这是您放置所有要执行的 cgi 脚本的地方。 我们的下一步是配置 apache 以识别 cgi-bin 目录并允许将其中的所有程序作为 cgi 脚本执行。

配置Apache使用perl运行CGI脚本

  1. 需要在apache服务器的配置文件中添加一条指令,以便它知道CGI的存在及其目录的位置。 首先转到 apache 配置文件的位置并使用您喜欢的文本编辑器打开它

    cd /etc/apache2/sites-available/  
      sudo gedit 000-default.conf 
      
  2. 将以下内容复制到文件 000-default.conf 代码行“DocumentRoot /var/www/html/”和“ErrorLog”之间$ {APACHE_LOG_DIR}/error.log”

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 
      <目录“/usr/lib/cgi-bin”> 
      允许覆盖无 
      选项 +ExecCGI -MultiViews +SymLinksIfOwnerMatch 
      要求全部授予 
      

  3. 使用以下代码重新启动apache服务器

    sudo 服务 apache2 restart 
      
  4. 现在我们需要启用默认情况下较新版本的 ubuntu 中存在的 cgi 模块

    sudo a2enmod cgi.load 
      sudo a2enmod cgid.load 
      
  5. 此时我们可以重新加载 apache Web 服务器,以便它再次读取配置文件。

    sudo 服务 apache2 重新加载 
      

apache 的配置部分已经结束,现在让我们用一个示例 cgi perl 程序来检查它。

测试

  1. 转到cgi-bin目录并创建一个扩展名为.pl的cgi文件

    cd /usr/lib/cgi-bin/ 
      sudo gedit test.pl 
      
  2. 将以下代码复制到test.pl上并保存。

    #!/usr/bin/perl -w 
      打印“内容类型:text/html\r\n\r\n”; 
      打印“CGI 工作完美!!   \n”; 
      
  3. 现在授予test.pl执行权限。

    sudo chmod 755 test.pl 
      
  4. 现在在网络浏览器中打开该文件http://Localhost/cgi-bin/test.pl< /a>

  5. 如果您看到输出“CGI 工作完美”,那么您就可以开始了。现在将所有程序转储到 cgi-bin 目录中并开始使用它们。

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

If you have successfully installed Apache web server and Perl please follow the following steps to run cgi script using perl on ubuntu systems.

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.

  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
    

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.

If not issue the following command to ensure the appropriate permission for our cgi-bin directory.

     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
    

Thus your cgi-bin directory is ready to go. This is where you put all your cgi scripts for execution. Our next step is configure apache to recognize cgi-bin directory and allow execution of all programs in it as cgi scripts.

Configuring Apache to run CGI script using perl

  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
    

The configuration part of apache is over now let us check it with a sample cgi perl program.

Testing it out

  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.

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.

月光色 2024-07-21 07:50:54

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

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.

风为裳 2024-07-21 07:50:54

处理CGI脚本有两种方法,SetHandler AddHandler

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)时,它将全局应用于所有文件。 但是您可以通过将这些指令包含在内部来将上下文限制为一个子集,例如 < code>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 结尾。

There are two ways to handle CGI scripts, SetHandler and AddHandler.

SetHandler cgi-script

applies to all files in a given context, no matter how they are named, even index.html or style.css.

AddHandler cgi-script .pl

is similar, but applies to files ending in .pl, in a given context. You may choose another extension or several, if you like.

Additionally, the CGI module must be loaded and Options +ExecCGI configured. To activate the module, issue

a2enmod cgi

and restart or reload Apache. Finally, the Perl CGI script must be executable. 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. But you may restrict the context to a subset by enclosing these directives inside, e.g. 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. Same is with AddHandler inside a Directory or Location directive, of course. It then applies to the files inside /path/to/some/cgi-dir, ending in .pl.

黒涩兲箜 2024-07-21 07:50:54

我猜您已经看过 mod_perl 了?

您是否尝试过以下教程

编辑:关于您的发布 - 也许您可以在 .cgi 文件中包含代码示例。 也许甚至是前几行?

I'm guessing you've taken a look at 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. Perhaps even the first few lines?

小矜持 2024-07-21 07:50:54

对于像我这样一直在摸索远超你现在需要知道的教程和文档,并且只是想看看它对初学者有用的人来说,我发现了我唯一拥有的东西要做的就是添加:

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。

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

To my configuration file.

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.

Dave Sherohman's answer mentions the AddHandler solution also.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文