总的来说,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
就只有这一个入口了。