zoukankan      html  css  js  c++  java
  • apache配置cgi脚本

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

    打开此文件,有一行

    DocumentRoot "F:/xampp/htdocs"

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

    配置Apache以允许CGI

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

    1.ScriptAlias

    ScriptAlias指令使Apache允许执行一个特定目录中的CGI程序。当客户端请求此特定目录中的资源时,Apache假定其中所有的文件都是CGI程序并试图运行它。

    ScriptAlias指令形如:

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

    如果Apache被安装到默认位置,默认的配置文件httpd.conf中就会有上述配置。ScriptAliasAlias指令非常相似,都是定义了映射到一个特定目录的URL前缀,两者一般都用于指定位于DocumentRoot以外的目录,其不同之处是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.设置了以上映射,我们还需要设置存放文件的目录属性:

    <Directory "D:\dev\python">
            Options Indexes MultiViews
            AllowOverride None
            Order allow,deny
            Allow from all
            Options ExecCGI
    </Directory>

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

    <Directory "E:/ProgramFiles/Python2.7/SourceOwn">
    AllowOverride None
    Options None
    Require all granted
    </Directory>

    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
    <?php
        phpinfo();
    ?>

    综合以上,完整配置:

    <IfModule alias_module> (说明,在apache2.2中,scriptAlias是在 <IfModule alias_module>模块中的
    ScriptAlias /python/ "E:\ProgramFiles\Python2.7\SourceOwn"
    </IfModule>


    <Directory "E:\ProgramFiles\Python2.7\SourceOwn">
    AllowOverride None
    Options ExecCGI
    Order allow,deny
    Allow from all
    </Directory>

    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设置是这样的:

    <IfModule alias_module>
    #
    # 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 <Directory> 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/"

    </IfModule>

    <Directory "F:/xampp/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
    </Directory>

    把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 中。增加红色部分 ,注意,目录用 / 表示,最后一个目录一定要跟 / ,这是之前为什么失败的原因。

    <IfModule alias_module> 

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

    </IfModule>

    <Directory "F:/xampp/cgi-bin">
    AllowOverride All
    Options None
    Require all granted
    </Directory>

    <Directory "F:/pythonSource">
    AllowOverride All
    Options None
    Require all granted
    </Directory>

  • 相关阅读:
    leetCode21. 合并两个有序链表
    (flag)每日三道面试题(4.25)
    每日三加一面试题(4.21)
    按照顺序执行异步函数
    javascript Proxy 代理模式深度监听对象、数组变化
    数据结构与算法(二) 线性表一
    This关键字
    数据结构与算法(一)
    Git报错信息
    MakeDown语法学习
  • 原文地址:https://www.cnblogs.com/youxin/p/3073625.html
Copyright © 2011-2022 走看看