如何配置 Apache 2 来运行 Perl CGI 脚本?
我想配置在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这篇文章的目的是拯救那些*无法在 Ubuntu 上为 Perl 正确设置 Apache2 的人们。 (特定于您的 Linux 机器的系统配置将在方括号内提及,如 [this])。
Apache 2 设置不当的可能结果:
如果一个人以合理的智慧遵循下面描述的步骤,他/她就可以克服上述错误。
在开始步骤之前。 转到
/etc/hosts
文件并添加 IP 地址/域名` 例如:步骤 1:安装
apache2
第 2 步:安装 mod_perl
步骤 3:配置
apache2
打开
sites-available/default
并添加以下内容,步骤 4:
将以下行添加到
/etc/apache2/apache2.conf
文件。第5步:
非常重要,或者至少我是这么认为,只有在完成这一步之后,我才能让它发挥作用。
AddHandler cgi-script .cgi .pl
步骤6
非常重要,或者至少我是这么认为的,只有在完成这一步之后,我才能让它工作。
将以下内容添加到您的
/etc/apache2/sites-enabled/000-default
文件中步骤 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:
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:Step 1: Install
apache2
Step 2: Install
mod_perl
Step 3: Configure
apache2
open
sites-available/default
and add the following,Step 4:
Add the following lines to your
/etc/apache2/apache2.conf
file.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
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
fileStep 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 usingchmod
and then, type thewebaddress/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.)
您需要查看 Apache 错误日志以了解“内部服务器错误”是什么。 根据我的经验,四种最可能的情况是:
CGI 程序位于未启用 CGI 执行的目录中。 解决方案:通过 httpd.conf 或 .htaccess 文件将
ExecCGI
选项添加到该目录。Apache 仅配置为从专用的
cgi-bin
目录运行 CGI。 解决方案:将 CGI 程序移至此处或将AddHandler cgi-script .cgi
语句添加到 httpd.conf。CGI 程序未设置为可执行文件。 解决方案(假设是 *nix 类型的操作系统):
chmod +x my_prog.cgi
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:
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.(即使我没有要求 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 showedFile 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/
.如果您已成功安装 Apache Web 服务器和 Perl,请按照以下步骤在 ubuntu 系统上使用 perl 运行 cgi 脚本。
在开始编写 CGI 脚本之前,必须配置 apache 服务器,使其能够识别 CGI 目录(保存 CGI 程序的位置)并允许执行该目录中的程序。
在 Ubuntu 中,cgi-bin 目录通常位于路径 /usr/lib 中,如果不存在,请使用以下命令创建 cgi-bin 目录。cgi-bin 应该位于此路径本身中。< /p>
发出以下命令来检查目录的权限状态。
<前><代码> ls -l /usr/lib | 较少的
检查权限是否为“drwxr-xr-x 2 root root 4096 2011-11-23 09:08 cgi-bin”,如果是,请转到步骤 3。
如果不是,请发出以下命令以确保我们的 cgi- 具有适当的权限bin 目录。
给cgi-bin目录赋予执行权限
<预><代码> sudo chmod 755 /usr/lib/cgi-bin
这样你的cgi-bin目录就准备好了。 这是您放置所有要执行的 cgi 脚本的地方。 我们的下一步是配置 apache 以识别 cgi-bin 目录并允许将其中的所有程序作为 cgi 脚本执行。
配置Apache使用perl运行CGI脚本
需要在apache服务器的配置文件中添加一条指令,以便它知道CGI的存在及其目录的位置。 首先转到 apache 配置文件的位置并使用您喜欢的文本编辑器打开它
将以下内容复制到文件 000-default.conf 代码行“DocumentRoot /var/www/html/”和“ErrorLog”之间$ {APACHE_LOG_DIR}/error.log”
使用以下代码重新启动apache服务器
现在我们需要启用默认情况下较新版本的 ubuntu 中存在的 cgi 模块
此时我们可以重新加载 apache Web 服务器,以便它再次读取配置文件。
apache 的配置部分已经结束,现在让我们用一个示例 cgi perl 程序来检查它。
测试
转到cgi-bin目录并创建一个扩展名为.pl的cgi文件
将以下代码复制到test.pl上并保存。
现在授予test.pl执行权限。
现在在网络浏览器中打开该文件http://Localhost/cgi-bin/test.pl< /a>
如果您看到输出“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.
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.
Issue the following command to check the permission status of the directory.
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.
Give execution permission to cgi-bin directory
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
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
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”
Restart apache server with the following code
Now we need to enable cgi module which is present in newer versions of ubuntu by default
At this point we can reload the apache webserver so that it reads the configuration files again.
The configuration part of apache is over now let us check it with a sample cgi perl program.
Testing it out
Go to the cgi-bin directory and create a cgi file with extension .pl
Copy the following code on test.pl and save it.
Now give the test.pl execution permission.
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.
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.
从 Ubuntu 12.04(Precise Pangolin)(也许还有之前的一两个版本)只需通过 Synaptic 安装
apache2
和mod-perl
并将 CGI 脚本放入 /usr/lib/cgi-bin 中即可。As of Ubuntu 12.04 (Precise Pangolin) (and perhaps a release or two before) simply installing
apache2
andmod-perl
via Synaptic and placing your CGI scripts in /usr/lib/cgi-bin is really all you need to do.处理CGI脚本有两种方法,
SetHandler
和AddHandler
。适用于给定上下文中的所有文件,无论它们的命名方式,甚至是
index.html
或style.css
。类似,但适用于给定上下文中以
.pl
结尾的文件。 如果您愿意,您可以选择另一个或多个扩展。此外,必须加载 CGI 模块并
Options +ExecCGI
已配置。 要激活该模块,请发出
并重新启动或重新加载 Apache。 最后,Perl CGI 脚本必须是可执行的。 因此必须设置执行位
开始
并且它应该从第一行
。 当您在任何指令之外使用
SetHandler
或AddHandler
(以及Options +ExecCGI
)时,它将全局应用于所有文件。 但是您可以通过将这些指令包含在内部来将上下文限制为一个子集,例如 < code>Directory现在,
SetHandler
仅适用于 /path/to/some/cgi-dir 内的文件,而不是网站的所有文件。 当然,Directory
或Location
指令中的AddHandler
也是如此。 然后它应用于 /path/to/some/cgi-dir 内的文件,以.pl
结尾。There are two ways to handle CGI scripts,
SetHandler
andAddHandler
.applies to all files in a given context, no matter how they are named, even
index.html
orstyle.css
.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, issueand restart or reload Apache. Finally, the Perl CGI script must be executable. So the execute bits must be set
and it should start with
as its first line.
When you use
SetHandler
orAddHandler
(andOptions +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
Now
SetHandler
applies only to the files inside /path/to/some/cgi-dir instead of all files of the web site. Same is withAddHandler
inside aDirectory
orLocation
directive, of course. It then applies to the files inside /path/to/some/cgi-dir, ending in.pl
.我猜您已经看过 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?对于像我这样一直在摸索远超你现在需要知道的教程和文档,并且只是想看看它对初学者有用的人来说,我发现了我唯一拥有的东西要做的就是添加:
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.