Apache 2 的 CGI(Common Gateway Interface) 是一种标准化的方式,让 web 服务器(这里是 Apache)可以将收到的 HTTP 请求转发给外部的可执行程序处理,然后把这些程序的输出再返回给浏览器。也就是说,CGI 充当了“网关”角色,让网站可以执行动态脚本(例如 Python、Perl、Shell 脚本、甚至编译后的 C 程序),从而生成动态内容。
举个简单例子:
hello.py 脚本,输出 HTMLcgi-bin 目录下
当用户访问 http://yourserver/cgi-bin/hello.py
hello.py,
Apache 2 对 CGI 的支持是通过 mod_cgi 或 mod_cgid 模块实现的:
mod_cgi 适用于 Unix 系统单进程模式(prefork)mod_cgid 适用于多进程、多线程(worker 或 event 模式)在配置上,常见的 CGI 目录是:
ScriptAlias /cgi-bin/ "/usr/lib/cgi-bin/"
并且需要为该目录配置可执行权限,例如:
<Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI Require all granted </Directory>
缺点: