使用apache
和perl cgi scripts
configuration时,不知道为什么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:选项+ FollowSymLinks + ExecCGI
# 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>
在Mac OS X 10.8上
我必须这样做
<Directory /> Options FollowSymLinks +ExecCGI AllowOverride None </Directory>
和
取消这个评论
#AddHandler cgi-script .cgi .pl
当浏览器正在打印脚本代码时,意味着无法找到运行该脚本的应用程序。
使用Apache 2.4
(在OSX Yosemite,10.10.5),如果我使用错误路径的shebang行,我的浏览器将显示:
内部服务器错误
但即使是有效的shebang行,我也无法通过遵循接受的答案中的建议来执行我的cgi程序 – Apache只是将程序的文本提供给我的浏览器。 经过一番尝试,我发现我需要对/usr/local/apache2/conf/httpd.conf
文件进行的唯一修改是取消注释:
Loadmodulee cgid_module modules/mod_cgid.so
我的cgi程序的扩展名为.pl,.py和.rb,具体取决于我正在编程的语言(并且apache cgi-bin目录包含一个没有扩展名的测试cgi脚本),并且它们都执行而不必指定在httpd.conf文件的任何地方有效的扩展。 我的默认httpd.conf
文件只有以下相关行:
<Ifmodulee alias_module> #Lots of comments here ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/" </Ifmodulee> ... ... <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
=> all +可执行文件。 换句话说,将可执行权限添加到每个owner,group,other。
而且,至少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文档在这里 。