apache配置cgi出错_apache配置cgi脚本

首先要了解apache配置文件httpd.conf。

打开此文件,有一行

DocumentRoot "F:/xampp/htdocs"

表示http根目录为F:/xampp/htdocs

配置Apache以允许CGI

要让CGI程序能正常运作,必须配置Apache以允许CGI的执行,其方法有多种。

1.ScriptAlias

ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/

如果Apache被安装到默认位置,默认的配置文件httpd.conf中就会有上述配置。ScriptAlias又多了一层含义,即URL前缀后面的任何文件都被视为CGI程序。所以,上述例子会指示Apache:任何以/cgi-bin/开头的资源都将映射到/usr/local/apache2/cgi-bin/目录中,且视之为CGI程序。

例如,如果有URL为http://www.example.com/cgi-bin/test.pl的请求,Apache会试图执行/usr/local/apache2/cgi-bin/test.pl文件并返回其输出。当然,这个文件必须存在而且可执行,并以特定的方法产生输出,否则Apache返回一个出错消息。

我们可以在这样定义:

ScriptAlias /python/ "D:\dev\python"

那么对http://localhost/python/test.py的请求会引导服务器执行D:\dev\python\test.py脚本。相当于目录映射。

2.设置了以上映射,我们还需要设置存放文件的目录属性:

Options Indexes MultiViews

AllowOverride None

Order allow,deny

Allow from all

Options ExecCGI

网上面大多说这个配置ok,但我这么配总是403错误,改为一下的就可以了:

AllowOverride None

Options None

Require all granted

Allow from all表示允许所有用户访问,deny from all禁止访问。

3.告诉服务器哪些是cgi文件:

AddHandler cgi-script  .py .sh .cgi  .pl

我们告诉apache扩展名为.py .sh .cgi  .pl  的是cgi文件。

我们再写一个测试文件放在我们设置的cgi目录里即D:python,文件内容如下:

test.py

#!C:\Python26\python.exe (必须,告诉解释器的路径)

print "Content-type: text/html;charset=utf-8\n\n"

print "hello world"

在浏览器中输入

http://localhost/python/test.py

就可以看到输出Hello world了。

再演示一个PHP版本吧:

#!/usr/local/php/bin/php -q

# File: File:/usr/local/apache/cgi-bin/i.php

phpinfo();

?>

综合以上,完整配置:

(说明,在apache2.2中,scriptAlias是在 模块中的)

ScriptAlias /python/ "E:\ProgramFiles\Python2.7\SourceOwn"

AllowOverride None

Options ExecCGI

Order allow,deny

Allow from all

AddHandler cgi-script .cgi .pl .py

我刚开始配置时,出现

403 Access forbidden!  You don't have permission to access the requested directory. There is either no index document or the directory is read-protected.

错误。

apache error log上写的是:

Client  Denied  ByServer  Configuration

去 这个网页 http://wiki.apache.org/httpd/ClientDeniedByServerConfiguration

看了下,我已经配置directory 目录可以访问了,不知道为什么报错。

但是我发现xampp 默认的cgi-bin目录可以访问,我自己设置的目录就不能访问,看了下,cgi-bin设置是这样的:

#

# Redirect: Allows you to tell clients about documents that used to

# exist in your server's namespace, but do not anymore. The client

# will make a new request for the document at its new location.

# Example:

# Redirect permanent /foo http://localhost/bar

#

# Alias: Maps web paths into filesystem paths and is used to

# access content that does not live under the DocumentRoot.

# Example:

# Alias /webpath /full/filesystem/path

#

# If you include a trailing / on /webpath then the server will

# require it to be present in the URL. You will also likely

# need to provide a section to allow access to

# the filesystem path.

#

# ScriptAlias: This controls which directories contain server scripts.

# ScriptAliases are essentially the same as Aliases, except that

# documents in the target directory are treated as applications and

# run by the server when requested rather than as documents sent to the

# client. The same rules about trailing "/" apply to ScriptAlias

# directives as to Alias.

#

ScriptAlias /cgi-bin/ "F:/xampp/cgi-bin/"

AllowOverride None

Options None

Require all granted

把Directory改为这样的设置就ok了。

参考:

http://www.phpchina.com/resource/manual/apache/howto/cgi.html

http://be-evil.org/post-130.html

参考:

http://www.cnblogs.com/mindsbook/archive/2009/12/30/cgi_python.html

过了n久后发现为什么配置失败,进行下面的配置就可以了。(模仿cgi-bin)

首先,在alias_module 中。增加红色部分 ,注意,目录用 / 表示,最后一个目录一定要跟 / ,这是之前为什么失败的原因。

ScriptAlias /cgi-bin/ "F:/xampp/cgi-bin/"

ScriptAlias /python/ "F:/pythonSource/"

AllowOverride All

Options None

Require all granted

AllowOverride All

Options None

Require all granted

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值