[英]Perl Apache : Perl script displayed as plain text
使用apache
和perl cgi scripts
配置时,不知道为什么index.cgi
/ index.pl
显示为纯文本而不是执行它们。 当我将http://localhost
放在浏览器中时,它会显示下面的代码,而不是执行它。
List item
#!C:/Dwimperl/perl/bin/perl.exe -w
print "Content-type: text/html\n\n";
print <<HTML;
<html>
<head>
<title>A perl web page</title>
</head>
<body>
<h3>A hello world form perl</h3>
</body>
HTML
exit;
这是我大部分时间编辑过的httpd.conf
文件的一部分(在阅读了各种在线参考资料、教程之后)
# This should be changed to whatever you set DocumentRoot to.
<Directory "D:\webserver">
Listen 80
ServerName localhost:80
LoadModule cgi_module modules/mod_cgi.so
# First, we configure the "default" to be a very restrictive set of
# features.
<Directory />
Options FollowSymLinks +ExecCGI
AllowOverride None
</Directory>
DirectoryIndex index.html index.html.var index.cgi index.pl
AccessFileName .htaccess
# To use CGI scripts outside of ScriptAliased directories:
# (You will also need to add "ExecCGI" to the "Options" directive.)
#
#AddHandler cgi-script .cgi .pl
ScriptAlias /cgi-bin/ "C:/Apache/Apache2/cgi-bin/"
当浏览器打印脚本代码时,这意味着它无法找到运行脚本的应用程序。 下面两行应该是你解决这个问题的第一步。 AddHandler 将确保以.cgi
和.pl
结尾的文件被视为 cgi 脚本。 +ExecCGI
选项将允许执行脚本。 还要确保您的脚本指向正确的 perl 二进制位置。
AddHandler cgi-script .cgi .pl Options FollowSymLinks +ExecCGI
您的 httpd.conf 中还有一些错误/错误配置点
ScriptAlias /cgi-bin/ "D:\\webserver\\cgi-bin"
httpd.conf
。 你应该用下面的替换你的<Directory "D:\\webserver">
部分。<Directory "D:\\webserver\\cgi-bin" /> AddHandler cgi-script .cgi .pl Options FollowSymLinks +ExecCGI AllowOverride None </Directory>
perl test.cgi
cgi-bin
目录和 cgi 脚本具有读写递归权限。 您还可以创建具有写入权限的目录或文件。 如果没有在其他地方创建一个cgi-bin
目录,在那里你可以拥有写权限,而是在httpd.conf
中的alias
和directory
属性中提供它的路径。此链接也应该对您有所帮助。
(额外评论,不是原始回答者:您可能还需要启用cgi
模块。对我来说,让 cgi 在全新安装的 Apache 2 上工作的最后一步是sudo a2enmod cgi
。在我这样做之前,网站只是向我展示了脚本的内容。)
sudo a2enmod cgi
目录/位置/文件没有与之关联的正确处理程序,或者没有启用ExecCGI
选项。 请参阅Apache 教程:使用 CGI 的动态内容。
更改新版本的 apache : Options +FollowSymLinks +ExecCGI
在 Mac 操作系统 x 10.8 上
我不得不这样做
<Directory />
Options FollowSymLinks +ExecCGI
AllowOverride None
</Directory>
和
取消注释这个
#AddHandler cgi-script .cgi .pl
# use types www.site.com/visible-in-url
# Apache serves /var/path/to/dir
ScriptAlias /visible-in-url/ /var/path/to/dir/
# Note the order of the aliases matters - first cgi than static content
# Note this dir is a symlink pointing to the desirable directory
<Directory "/var/path/to/dir">
AddHandler cgi-script .cgi .pl
AllowOverride Indexes
Options +ExecCGI +MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
<Files ~ "\.(pl|cgi)$">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI +SymLinksIfOwnerMatch
PerlSendHeader On
</Files>
当浏览器打印脚本代码时,这意味着它无法找到运行脚本的应用程序。
使用Apache 2.4
(在 OSX Yosemite,10.10.5 上),如果我使用带有错误路径的 shebang 行,我的浏览器会显示:
内部服务器错误
但即使使用有效的 shebang 行,我也无法按照已接受的答案中的建议执行我的 cgi 程序——Apache 只是将程序的文本提供给我的浏览器。 经过一些试验,我发现我需要对/usr/local/apache2/conf/httpd.conf
文件进行的唯一更改是取消注释该行:
LoadModule cgid_module modules/mod_cgid.so
我的 cgi 程序有 .pl、.py 和 .rb 扩展名,这取决于我用什么语言编程(并且 apache cgi-bin 目录包含一个没有扩展名的测试 cgi 脚本),并且它们都无需指定即可执行httpd.conf 文件中任何位置的有效扩展名。 我的默认httpd.conf
文件只有以下相关行:
<IfModule alias_module>
#Lots of comments here
ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
</IfModule>
...
...
<Directory "/usr/local/apache2/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
我使用的 shebang 行取决于我的 cgi 程序是用什么语言编写的:
#!/usr/bin/env perl
要么:
#!/usr/bin/env python
要么:
#!/usr/bin/env ruby
cgi 程序也必须是可执行文件,否则您将收到内部服务器错误:
$ chmod a+x myprog.pl
a+x
=> 全部 + 可执行文件。 换句话说,将可执行权限添加到所有者、组、其他中的每一个。
而且,在输出响应正文之前,cgi 程序至少必须生成一个Content-Type header
:
print "Content-Type: text/html\n\n";
print "<h1>Hello, World.</h1>";
(顺便说一句,那个确切的代码可以在 perl、python 或 ruby 中工作。)否则,您将再次收到内部服务器错误。
执行 cgi 脚本的 URL:
http://localhost:8080/cgi-bin/myprog.pl
这是我安装apache的方式:
~/Downloads$ tar xvfz httpd-2.4.18.tar.bz2
...
...
~/Downloads$ cd httpd-2.4.18
...
...
~/Downloads/httpd-2.4.18$ ./configure --help
...
...
--enable-so DSO capability. This module will be automatically
enabled unless you build all modules statically.
...
...
我不知道这到底是什么意思,但是php 文档说要使用该选项安装 apache,所以我继续这样做:
~/Downloads/httpd-2.4.18$ ./configure --enable-so
...
...
~/Downloads/httpd-2.4.18$ make
...
...
~/Downloads/httpd-2.4.18$ sudo make install
Apache DSO 文档在这里。
如果没有其他任何效果,请确保您正在显示从本地服务器调用可执行脚本的 HTML 页面,例如http://192.168.0.15/yourPage.html 。 如果您通过在浏览器中将 HTML 页面作为文件打开来调用它,它将始终将您的可执行脚本显示为文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.