设为首页 收藏本站
查看: 871|回复: 0

[经验分享] apache配置cgi脚本

[复制链接]

尚未签到

发表于 2015-8-1 13:31:53 | 显示全部楼层 |阅读模式
  首先要了解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中就会有上述配置。ScriptAlias与Alias指令非常相似,都是定义了映射到一个特定目录的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.设置了以上映射,我们还需要设置存放文件的目录属性:
  
        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

  综合以上,完整配置:
   (说明,在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.iyunv.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


  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-93047-1-1.html 上篇帖子: Unable to read TLD "META-INF/fmt.tld" from JAR file "file:/D:/…./.metadata/.plug 下篇帖子: windows下apache开启rewrite
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表