2010/08/04

Apache2 使用CGI程式(perl,python,bash shell)

CGI 介紹 http://ind.ntou.edu.tw/~dada/cgi/CGIintro.htm
CGI是Common Gateway Interface的縮寫,中文翻做『共通閘道介面』。
它是一種標準介面程式,能讓你的網頁跟WWW server溝通,達到跟使用者互動的效果。
而且透過CGI程式,可以讓你動態的產生網頁,秀出server上的最新資料。
當你link到一個CGI物件時,你取回的文件並非是一份靜態資料, 而是一個藉由
程式動態產生的HTML資料流。傳回來的資料也許分分秒秒都在更改,或者針對使用者
的特殊查詢而有不同的反應,例如股票市場行情等等。 簡單來說,CGI程式就是能夠
動態產生WWW網頁,並讓一般使用者經由WWW取用現存在傳統資訊系統內的資料。

但是現在 CGI 的功能大多以備PHP 直接嵌入的方式取代了,但是還是有些語言程式仍然用著這種方式存在者。

參考:http://jangmt.com/wiki/index.php?title=253_unit8#LAB7.EF.BC.9A.E4.BD.BF.E7.94.A8_CGI_.E7.A8.8B.E5.BC.8F

* 如果要再 Apache2 上面使用 CGI 程式,只要透過下列設定即可使用:

* 使用 CGI 程式:Dynamic Content with CGI

* 使用root修改 httpd.conf 加入下列敘述,讓一般使用者的目錄可以使用cgi程式。
<VirtualHost *:80>

    DocumentRoot /home/student/public_html

    ServerName station1.example.com



<Directory /home/student/public_html/cgi-bin/>

    Options ExecCGI

    SetHandler cgi-script

</Directory>

# 底下這行 alias 需要加入,否則在虛擬主機無法看到結果。

Alias /cgi-bin/ "/home/student/public_html/cgi-bin/"

</VirtualHost>

* 使用一般使用者 student 建立一個 test.cgi 做測試
[student@station1 ~]$ mkdir -p /home/student/public_html/cgi-bin/
# 切換目錄
[student@station1 ~]$ cd /home/student/public_html/cgi-bin/
# 編寫 test.cgi 檔案,結果是使用perl顯示電腦資訊
[student@station1 cgi-bin]$ vim test.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<b>CGI test ok<b>\n";
print "<b>Date: ", scalar localtime, "</b><br>\n";
print "<hr>It worked!\n";
print "This script runs under: ".$ENV{"GATEWAY_INTERFACE"}."<hr></n";
$ENV{"SERVER_NAME"}="(Hidden for security purposes)";
$ENV{"SERVER_ADMIN"}="(Hidden for security purposes)";
$ENV{"SCRIPT_FILENAME"}="(Hidden for security purposes)";
$ENV{"SERVER_SOFTWARE"}="(Hidden for security purposes)";
$ENV{"SERVER_PORT"}="(Hidden for security purposes)";
$ENV{"SERVER_SIGNATURE"}="Apache-AdvancedExtranetServer (Complete info hidden)";
$ENV{"PATH"}="(Hidden for security purposes)";
$ENV{"SERVER_ADDR"}="(Hidden for security purposes)";
$ENV{"DOCUMENT_ROOT"}="(Hidden for security purposes)";
$ENV{"MOD_PERL"}="(Hidden for security purposes)";
print "%ENV: <br>\n", map { "$_ = $ENV{$_} <br>\n" } keys %ENV;

# 賦予可執行權限
[student@station1 cgi-bin]$ chmod +x test.cgi

* 測試網頁 http://172.24.0.1/~student/cgi-bin/test.cgi 或 http://station1.example.com/cgi-bin/test.cgi (前提為 Alias /cgi-bin/ "/home/student/public_html/cgi-bin/" 這行加入設定檔才能夠執行否則只能以 user_dir 方式執行)

* python 也可是 cgi 的程式,test 程式碼如下:

#!/usr/bin/env python

import cgi
import cgitb; cgitb.enable() # for troubleshooting

print "Content-type: text/html"
print

print """
<html>

<head><title>Sample CGI Script</title></head>

<body>

<h3> Sample CGI Script </h3>
"""

form = cgi.FieldStorage()
message = form.getvalue("message", "(no message)")

print """

<p>Previous message: %s</p>

<p>form

<form method="post" action="index.cgi">
<p>message: <input type="text" name="message"/></p>
</form>

</body>

</html>
""" % message

* 參考:http://wiki.python.org/moin/CgiScripts

* bash shell 也可以用來寫 CGI 程式

#!/bin/bash
echo "Content-type: text/html"
echo ""
# OK, so we've sent the header... now send some content
echo "<html><title>Crack This Server</title><body>"

username=`who`
echo "<h1>Hello, $username</h1>"

echo "<h2>Users on `/bin/hostname`</h2>"
echo "</body></html>"

* bash shell cgi 參考 http://bashlib.sourceforge.net/
* 延伸閱讀:官網 http://httpd.apache.org/docs/2.0/howto/cgi.html

沒有留言: