Rootop 服务器运维与web架构

Apache配置CGI运行环境

| 暂无评论

       CGI是通用网关接口Common Gateway Interface 的缩写,用于连接网页和web服务器应用程序的接口。

        CGI 是web服务器运行的一个可执行程序,由网页的一个超链接激活并调用,并对该程序的返回结果进行处理,显示在客户端的web浏览器上。

        用CGI程序可以实现处理网页的表单处理、数据库查询、发送电子邮件等工作。

        CGI使网页变得不是静态的,而是交互的。

        CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量,如Perl、C、C++、Java,其中Perl易于编译调试、移植性颇强,可以说在众多的CGI编程语言中是最好最容易上手的语言。Perl几乎成了CGI的标准或代言词

[root@rhel ~]# rpm -qa perl
perl-5.8.8-27.el5                   //默认情况下,perl语言解释器会被系统默认安装

编辑apache的配置文件,找到 Options Indexes FollowSymLinks 设置存放CGI文件的目录权限

# 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

#

如果不修改目录权限,会提示 forbidden

Forbidden

You don’t have permission to access /test.cgi on this server.


Apache/2.2.3 (Red Hat) Server at 172.17.1.113 Port 80

标明CGI程序的文件类型

AddHandler cgi-script .cgi .pl           //标明哪些拓展名为cgi
不注明文件类型的话,不会去调用perl去执行脚本,会直接显示出源代码。

保存退出,重启apache。

在网站根目录建立测试页:

[root@rhel ~]# vi /var/www/html/test.cgi
[root@rhel ~]#chmod o+x /var/www/html/test.cgi        //需要执行权限

test.cgi 代码如下:

#!/usr/bin/perl
print “Content-type: text/html\n\n”;
print “Network Questions\n”

然后通过浏览器访问: http://172.17.113/test.cgi

看到Network Questions 表明搭建成功。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/759.html

作者:Venus

服务器运维与性能优化

发表回复