1. 程式人生 > >Perl CGI 學習之環境搭建-Windows7 64

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/ 這個需要個人進行配置,麻煩一點

安裝完成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:

另外這裡還有一篇文章提到cgi的效能,效能是硬傷呀,雖然我還沒真正使用起來。。。

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