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

[经验分享] Apache下cgi服务配置部署

[复制链接]

尚未签到

发表于 2017-1-6 10:14:05 | 显示全部楼层 |阅读模式
  总的来说,apache 默认支持.cgi的服务,开启支持.cgi服务有两种方式:
  1.落霞满天飞
  cgi程序不限制部署目录, 可以在web根目录下到处放置。那么apache如何来识别你的服务是cgi的呢?
  没错,通过服务的后缀名.cgi 识别。如何配置?
  只需要两步:
  假设根目录为:
  DocumentRoot "/var/www"
  A:则找到以下段,在options后加上ExecCGI
  <Directory "/var/www">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.2/mod/core.html#options
    # for more information.
    #
    Options -Indexes FollowSymLinks ExecCGI

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Order allow,deny
    Allow from all

</Directory>
  B: 找到类似下面这段,开启.cgi的支持,见标红处:
  <IfModule mime_module>
    #
    # TypesConfig points to the file containing the list of mappings from
    # filename extension to MIME-type.
    #
    TypesConfig conf/mime.types

    #
    # AddType allows you to add to or override the MIME configuration
    # file specified in TypesConfig for specific file types.
    #
    #AddType application/x-gzip .tgz
    #
    # AddEncoding allows you to have certain browsers uncompress
    # information on the fly. Note: Not all browsers support this.
    #
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    #
    # If the AddEncoding directives above are commented-out, then you
    # probably should define those extensions to indicate media types:
    #
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    AddType application/x-httpd-php .php 
   
 
    #
    # AddHandler allows you to map certain file extensions to "handlers":
    # actions unrelated to filetype. These can be either built into the server
    # or added with the Action directive (see below)
    #
    # To use CGI scripts outside of ScriptAliased directories:
    # (You will also need to add "ExecCGI" to the "Options" directive.)
    #
    AddHandler cgi-script .cgi

    # For type maps (negotiated resources):
    #AddHandler type-map var

    #
    # Filters allow you to process content before it is sent to the client.
    #
    # To parse .shtml files for server-side includes (SSI):
    # (You will also need to add "Includes" to the "Options" directive.)
    #
    #AddType text/html .shtml
    #AddOutputFilter INCLUDES .shtml
</IfModule>
  重启apache服务:
  #service httpd restart
  访问(必须加.cgi,否则运行不了):http://localhost/helloworld.cgi
  2.圈养
  这种方式主要是利用ScriptAlias配置将所有cgi服务部署在一个目录里,从而cgi服务可以以.cgi后缀或者没有后缀的方式运行,因为这种方式只要是在配置的目录里,则认为是CGI服务,当然该目录下的文件必须能被apache识别这是前提条件。这种方式与上面的配置无关,完全从新的配置文件开始配置。
  如果其他目录部署了php或者其他服务。
  假设cgi服务 (helloworld.cgi 与 helloworld)部署在 /var/www/cgi-bin下面
  设置虚拟主机配置,域名www.testcgi.com 指向/var/www/cgi-bin 目录
  apache配置httpd.conf里默认设置了cgi目录为/usr/local/apache2/cgi-bin/
  红色的为修改地方,我们将它改成:
  ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
  <Directory "/var/www/cgi-bin/">
    AllowOverride None
  Options None
    Order allow,deny
    Allow from all
</Directory>
  修改后重启apache服务,这时运行:
  http://www.testcgi.com/cgi-bin/helloworld.cgi 与 http://www.testcgi.com/cgi-bin/helloworld
  都能正常访问,但这里有一个问题,因为httpd-vhosts.conf里配置的这个虚拟站点的根目录是指向/var/www/cgi-bin 的,根本下面就没有cgi-bin 这个目录,这时如果有人直接输入:
  http://www.testcgi.com/helloworld.cgi
  就会导致文件被直接下载了,而不是被解析,避免这种情况(有多个入口,导致服务访问不一致),有同学想到了这样修改:
A:ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" ==> ScriptAlias / "/var/www/cgi-bin/"
  没错,这样修改后,可以直接http://www.testcgi.com/helloworld.cgi 或者http://www.testcgi.com/helloworld访问了,但如果部署了多个服务,比如还有其他的php站点,那么会导致其他的站点不能正常访问,因为ScriptAlias在这里是公共的。
  B:在<Directory "/var/www/cgi-bin/">目录里的Options选项后面加上ExecCGI, 并加上 AddHandler cgi-script .cgi 配置, 这就又变成了第一种了,必须加.cgi才能访问,而没有.cgi后缀的服务又同样是弹出下载框来。

  这两种方式似乎都不太理想。那么能否将ScriptAlias加入到这个有cgi服务的<Directory>目录下呢?
  apache是不支持在<Directory>里增加ScriptAlias的,会报错。
  于是我们想到了,可以加在虚拟站点的配置里:
  <VirtualHost *:80>
   ServerAdmin root@localhost
   DocumentRoot "/var/www/cgi-bin"
    ScriptAlias / "/var/www/cgi-bin/"
 
 ServerName www.testcgi.com
  
   CustomLog "|/usr/local/cronolog/sbin/cronolog /var/www/logs/cgi-bin/testcgi.com-access_%Y-%m-%d_log" combined
   ErrorLog "|/usr/local/cronolog/sbin/cronolog /var/www/logs/cgi-bin/testcgi.com-error_%Y-%m-%d_log"

</VirtualHost>

  这样的话,我们就可以很方便的控制了范围,并且做到了隔离使其他的服务不受影响,达到圈养的方式.
  当然有同学会说了, 我们编写与部署CGI服务的时候,可以规定只使用加.cgi后缀的一种,当然这是最省事的一种了, 但这取决于我们的规范以及执行力。 :)
  这里还引申出另外一种方式:
  可以直接在/var/www/cgi-bin/目录下再建立一个cgi-bin目录,将所有cgi服务部署在这里面,或者如果你觉得两个有点别扭的话可以在上层建立一个根目录比如testcgi包含cgi-bin目录, 这样适当修改一下httpd-vhosts.conf里的DocumentRoot指向上层目录:DocumentRoot "/var/www/testcgi"
  这样 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 也不用修改了
  这样访问 http://www.testcgi.com/cgi-bin/helloworld.cgi 或者 http://www.testcgi.com/cgi-bin/helloworld
  就只有这一个入口了。

运维网声明 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-324581-1-1.html 上篇帖子: 安裝 Apache Flex SDK 4.9 下篇帖子: Apache Apusic集成配置负载均衡
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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