Linux 如何配置 Apache 2 以运行 Perl CGI 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/560749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I configure Apache 2 to run Perl CGI scripts?
提问by
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.
我想配置运行的 Apache 2Kubuntu
以执行 Perl CGI 脚本。我已经尝试了通过谷歌搜索遇到的一些步骤,但似乎没有任何效果。
What is the right way of achieving this?
实现这一目标的正确方法是什么?
回答by toolkit
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. Perhaps even the first few lines?
编辑:关于您的发布 - 也许您可以在.cgi
文件中包含代码示例。也许甚至是前几行?
回答by Dave Sherohman
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:
您需要查看 Apache 错误日志以了解“内部服务器错误”是什么。根据我的经验,最可能的四种情况是:
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.Apache is only configured to run CGIs from a dedicated
cgi-bin
directory. Solution: Move the CGI program there or add anAddHandler cgi-script .cgi
statement to httpd.conf.The CGI program is not set as executable. Solution (assuming a *nix-type operating system):
chmod +x my_prog.cgi
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.
CGI 程序位于未启用 CGI 执行的目录中。解决方案:
ExecCGI
通过 httpd.conf 或 .htaccess 文件将选项添加到该目录。Apache 仅配置为从专用
cgi-bin
目录运行 CGI 。解决方案:将 CGI 程序AddHandler cgi-script .cgi
移到那里或在 httpd.conf 中添加一条语句。CGI 程序未设置为可执行文件。解决方案(假设是 *nix 类型的操作系统):
chmod +x my_prog.cgi
CGI 程序正在退出而不发送标头。解决方案:从命令行运行程序并验证 a) 它实际运行而不是因编译时错误而死亡,以及 b) 它生成正确的输出,其中至少应包括一个
Content-Type
标题和一个空行在它的最后一个标题之后。
回答by beck03076
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 的可能结果:
- Browser trying to download the .pl file instead of executing and giving out the result.
- Forbidden.
- Internal server error.
- 浏览器试图下载 .pl 文件而不是执行并给出结果。
- 禁止。
- 内部服务器错误。
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:
在开始步骤之前。转到/etc/hosts
文件并添加 IP 地址/域名`,例如:
127.0.0.1 www.BECK.com
Step 1: Install apache2
Step 2: Install mod_perl
Step 3: Configure apache2
第 1 步:安装apache2
第 2 步:安装mod_perl
第 3 步:配置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:
第四步:
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
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].
现在,在您之前在步骤 3 中提到的 [ path-where-you-want-your-.pl-and-.cgi-files]的位置添加您的 Perl 脚本作为 test.pl。
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.
授予.pl
文件使用权限chmod
,然后webaddress/cgi-bin/test.pl
在浏览器的地址栏中键入,你去,你明白了。
(Now, many of the things would have been redundant in this post. Kindly ignore it.)
(现在,这篇文章中的许多内容都是多余的。请忽略它。)
回答by Biff
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)(可能还有一两个版本)开始,只需安装apache2
并mod-perl
通过 Synaptic 并将您的 CGI 脚本放在 /usr/lib/cgi-bin 中,这真的是您需要做的全部。
回答by Allasso
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。
回答by northern-bradley
(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). 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
.
我在运行脚本时遇到了问题(尽管 bash 不是 perl)。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/
.
经过长时间的谷歌搜索会话后,对我来说破解它的是
sudo a2enmod cgi
一切都使用/usr/lib/cgi-bin/
.
回答by Olaf Dietsche
There are two ways to handle CGI scripts, SetHandler
and AddHandler
.
有两种处理 CGI 脚本的方法,SetHandler
和AddHandler
.
SetHandler cgi-script
applies to all files in a given context, no matter how they are named, even index.html
or style.css
.
适用于给定上下文中的所有文件,无论它们如何命名,甚至index.html
或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.
类似,但适用.pl
于给定上下文中以,结尾的文件。如果您愿意,您可以选择另一个或多个扩展名。
Additionally, the CGI module must be loaded and Options +ExecCGI
configured. To activate the module, issue
此外,必须加载和Options +ExecCGI
配置CGI 模块。要激活模块,请发出
a2enmod cgi
and restart or reload Apache. Finally, the Perl CGI script must be executable. So the execute bits must be set
并重新启动或重新加载Apache。最后,Perl CGI 脚本必须是可执行的。所以必须设置执行位
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
当您在任何指令之外使用SetHandler
or AddHandler
(and Options +ExecCGI
) 时,它会全局应用于所有文件。但是您可以通过将这些指令包含在内部来将上下文限制为一个子集,例如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
.
现在SetHandler
仅适用于 /path/to/some/cgi-dir 中的文件,而不是网站的所有文件。当然,AddHandler
在Directory
orLocation
指令内部也是如此。然后它适用于 /path/to/some/cgi-dir 中的文件,以.pl
.
回答by Aparna Asok
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 程序的位置)并允许在该目录中执行程序。
In Ubuntu cgi-bin directory usually resides in path /usr/lib , if not present create the cgi-bin directory using the following command.cgi-binshould be in this path itself.
mkdir /usr/lib/cgi-bin
Issue the following command to check the permission status of the directory.
ls -l /usr/lib | less
在 Ubuntu 中,cgi-bin 目录通常位于路径 /usr/lib 中,如果不存在,则使用以下命令创建 cgi-bin 目录。cgi-bin应该在这个路径中。
mkdir /usr/lib/cgi-bin
发出以下命令以检查目录的权限状态。
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”,如果是,则转到步骤 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
Give execution permission to cgi-bin directory
sudo chmod 755 /usr/lib/cgi-bin
赋予 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. Our next step is configure apache to recognize cgi-bin directory and allow execution of all programs in it as cgi scripts.
这样你的 cgi-bin 目录就可以使用了。这是您放置所有 cgi 脚本以供执行的地方。我们的下一步是配置 apache 以识别 cgi-bin 目录并允许将其中的所有程序作为 cgi 脚本执行。
Configuring Apache to run CGI script using perl
使用 perl 配置 Apache 以运行 CGI 脚本
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
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
Restart apache server with the following code
sudo service apache2 restart
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
At this point we can reload the apache webserver so that it reads the configuration files again.
sudo service apache2 reload
需要在 apache 服务器的配置文件中添加一个指令,以便它知道 CGI 的存在及其目录的位置。最初转到apache的配置文件所在的位置并使用您喜欢的文本编辑器打开它
cd /etc/apache2/sites-available/ sudo gedit 000-default.conf
将以下内容复制到文件 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
使用以下代码重启apache服务器
sudo service apache2 restart
现在我们需要启用默认情况下存在于较新版本的 ubuntu 中的 cgi 模块
sudo a2enmod cgi.load sudo a2enmod cgid.load
此时我们可以重新加载 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
测试一下
Go to the cgi-bin directory and create a cgi file with extension .pl
cd /usr/lib/cgi-bin/ sudo gedit test.pl
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”;
Now give the test.pl execution permission.
sudo chmod 755 test.pl
Now open that file in your web browser http://Localhost/cgi-bin/test.pl
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 目录并创建一个扩展名为 .pl 的 cgi 文件
cd /usr/lib/cgi-bin/ sudo gedit test.pl
将以下代码复制到 test.pl 上并保存。
#!/usr/bin/perl -w print “Content-type: text/html\r\n\r\n”; print “CGI working perfectly!! \n”;
现在授予 test.pl 执行权限。
sudo chmod 755 test.pl
现在在您的网络浏览器中打开该文件http://Localhost/cgi-bin/test.pl
如果您看到输出“CGI 工作完美”,您就可以开始了。现在将所有程序转储到 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 中授予您的新程序权限,以便成功运行它而不会出现任何内部服务器错误。