Perl CGI 学习之环境搭建-Windows7 64

CGI(公共网关接口)定义了web服务器与外部内容生成程序之间交互的方法,通常是指CGI程序或者CGI脚本,它是在网站上实现动态页面的最简单和常用的方法。

下面将讲述在windows上环境的配置

1 安装Active Perl会默认自带CGI 模块。

2 安装Apache Http server.

http://www.anindya.com/apache-http-server-2-4-2-x86-and-x64-windows-installers/

关于Apache HTTO Server
Apache HTTP Server(简称Apache),是Apache软件基金会的一个开放源代码的网页服务器,可以在大多数电脑操作系统中运行,由于其具有的跨平台性和安全性,被广泛使用,是最流行的Web服务器端软件之一。
它快速、可靠并且可通过简单的API扩展,Perl/Python 解释器可被编译到服务器中,可以创建一个每天有数百万人访问的Web服务器。
注意这个和Tomcat并不相同,不要混淆

见Tomcat:
Tomcat服务器是一个免费的开放源代码的Web应用服务器,它是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache、Sun和   其他一些公司及个人共同开发而成。由于有了Sun的参与和支持,最新的Servlet和JSP   规范总是能在Tomcat中得到体现,Tomcat5支持最新的Servlet 2.4和JSP 2.0规范。因为Tomcat技术先进、性能稳定,而且免费,因而深受Java爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web应用服务器。

在window7 64 位下面目前并没有官方release的版本,找到一个非正式的版本

http://www.anindya.com/apache-http-server-2-4-2-x86-and-x64-windows-installers/

或者:

http://www.apachelounge.com/download/win64/ 这个需要个人进行配置,麻烦一点

我是直接下载apache_2.4.2-x64-openssl-1.0.1c.msi这个。直接安装完成之后http://localhost/ 出现it works表示安装成功。


安装完成httpd服务器之后,现在来测试cgi的运行环境。在httpd的安装目录下有个cgi-bin 目录。在浏览器输入http://localhost/cgi-bin/printEnv.pl

看到打印出环境变量就表示成功了。


现在开始编写你的第一个cgi程序 hello world。

将一下保存到文本命名为test.pl

#!c:/Perl64/bin/perl.exe
# simple Hello World script
print "Content-type: text/plain\n\n";
print 'Hello World!';

保存在cgi-bin的目录下在浏览器键入http://localhost/cgi-bin/test.pl 就可以看到了。


注意第一行#!c:/Perl64/bin/perl.exe 非常重要,这个必须是你安装的perl的路径。有的写成#!/usr/local/bin/perl 这个也是不行的,这是linux环境下的。

不然会出现内部错误 500


下面将一些非常有用的配置:

1)将任何普通目录设置为cgi-bin目录而不是默认安装的。

找到httpd.conf文件这个文件位于apache的安装目录的子目录conf目录下,如我的是在C:\Program Files\Apache Software Foundation\Apache2.4\conf

  将ScriptAlias /cgi-bin/ 后面对应的路径修改为你想要的路径

# ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache2.4/cgi-bin/"
    ScriptAlias /cgi-bin/  "C:/WORK/work/Project/others/Perl/cgi-bin/"

同时还需要修改<Directory "C:/Program Files/Apache Software Foundation/Apache2.4/cgi-bin/">这个路径以及权限

<Directory "C:/WORK/work/Project/others/Perl/cgi-bin/">
    AllowOverride All
    Options All
    Require all granted
</Directory>
然后还是在url里面输入http://localhost/cgi-bin/test.pl 可以看到输出

Hello World! modified

上面所讲的还都只是perl程序的写法,并没有用到perl中真正的cgi模块。将上面的脚本改为以下使用cgi模块的

#!c:/Perl64/bin/perl.exe

use strict;
use CGI;

# instantiate a new CGI object

my $cgi = new CGI;
print

   $cgi->header .

   $cgi->start_html('Hello World!') .

   $cgi->h1('Hello World!') .

   $cgi->end_html;

exit (0);

很简单的cgi程序就结束了,开始你的cgi'之旅吧~~~

下面是一些学习资料

http://www.zzbaike.com/wiki/CGI

http://search.cpan.org/~leejo/CGI-4.13/lib/CGI.pm

http://www.tutorialspoint.com/perl/perl_cgi.htm

http://www.cgi101.com/book/ch5/text.html


话说世界真的是发展的比我想象得要快得多。刚要学习就发现cgi的cpan描述上有这么一段话:

啥意思呢,就是以后perl默认不会按照cgi模块,因为这个不认为是一个较为方便安全的开发web的最佳实践。

还好有个替代模块,不管怎么说技多不压身。先学着

If you upgrade to a new version of perl or if you rely on a system or vendor perl and get an updated version of perl through a system update, then you will have to install CGI.pm yourself with cpan/cpanm/a vendor package/manually. To make this a little easier the CGI::Fast module has been split into its own distribution, meaning you do not need acces to a compiler to install CGI.pm

The rationale for this decision is that CGI.pm is no longer considered good practice for developing web applications, including quick prototyping and small web scripts. There are far better, cleaner, quicker, easier, safer, more scalable, more extensible, more modern alternatives available at this point in time. These will be documented withCGI::Alternatives.

For more discussion on the removal of CGI.pm from core please see:

http://www.nntp.perl.org/group/perl.perl5.porters/2013/05/msg202130.html

另外这里还有一篇文章提到cgi的性能,性能是硬伤呀,虽然我还没真正使用起来。。。

http://developer.51cto.com/art/200509/2626.htm


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值