【求助】Linux上使用CGI程式的問題



贊助商連結


Pikapi
2003-06-18, 03:59 PM
這幾天在弄cgi弄到頭快爆了!!!

說明一下我是怎麼用的吧
我先修改httpd.conf
1.在582行,我改成Option +ExecCGI (書上寫說ExecCGI,apache說明書寫+ExecCGI,不過都沒用)
2.在831行,增加Addhandler cgi-script .cgi .pl

<Directory "/var/www/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>

以上是最基本的修改,我增加了些東西(照apache說明書提的)
1.在583行,我增加handler cgi-script .cgi .pl
2.把581的<Directory> ~ </Directory>中Order 和 All from這兩個設定刪掉 (設定允許通過的設定)
ps...以下的檔案屬性都設成755了,應該沒有錯

但是cgi留言版和一些外面提供的程式都會出現以下的圖
http://tw.img.photo.yahoo.com/bsup.tw/ab13/p26.gif

但是!!!連htm的檔案都會發生錯誤@_@
http://tw.img.photo.yahoo.com/bsup.tw/ab13/p27.gif

再來,我照說明書寫的,寫了一個簡單的cgi程式(test.cgi)
竟然可以跑~~!!
也放了測試cgi模組的cgi(c.cgi)
也是可以跑.....唯有留言版不能
http://tw.img.photo.yahoo.com/bsup.tw/ab13/p28.gif
http://tw.img.photo.yahoo.com/bsup.tw/ab13/p29.gif

後來我裝了CGI.pm的模組,來增加cgi的支援度.....也是不行
但是openwebmail可以使用無礙~"~

以上結果......下了幾個結論
1.也許cgi的模組沒完全裝好---基本的cgi可以用,但是有關於其他cgi語法的似乎不能用,也許perl沒裝好也不一定,但是不知道是哪沒裝好=.=
2.cgi-bin資料夾可能被什麼設定給設定住了,連htm檔也都出不去

不知道遇到這問題該如何解決呢?謝謝>_<

ps...我附上了我的httpd.conf,簡單的cgi(test.cgi),測試perl的cgi(c.cgi)

贊助商連結


repsol
2003-06-19, 11:37 AM
看了一下你的 httpd.conf
這是你的 DocumentRoot
DocumentRoot /var/www/html

這是你的 cgi 目錄
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<IfModule mod_cgid.c>
Scriptsock run/httpd.cgid
</IfModule>
<Directory "/var/www/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
</Directory>



所以說你的 cgi 目錄並不是在 DocumentRoot 的目錄之下
所以說如果你在 /var/www/cgi-bin/ 下放置 html 的檔案,當然會有error出現。

網頁會出現 500

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [email protected] and inform them of the time the error occu-
rred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

但是 cgi 的程式是可以 run 的,放在 /var/www/cgi-bin/ 這個目錄下放置你的 c.cgi 和 test.cgi 都可以 run 出結果。

以下就是 apache 的說明,以下這段說明如果要將妳的 cgi 放在 apache 的 DocumentRoot 之下
的話就必須要在該目錄設上 Options +ExecCGI 的設定,如此一來,html可以出來,cgi也可以執行。

CGI outside of ScriptAlias directories

CGI programs are often restricted to ScriptAlias'ed directories for security reasons.
In this way, administrators can tightly control who is allowed to use CGI programs.H-
owever, if the proper security precautions are taken, there is no reason why CGIprog-
rams cannot be run from arbitrary directories. For example, you may wish to let user-
s have web content in their home directories with the UserDir directive. If they wan-
t to have their own CGI programs, but don't have access to the main cgi-bin directory
, they will need to be able to run CGI programs elsewhere.

Explicitly using Options to permit CGI execution
You could explicitly use the Options directive, inside your main server configuration
file, to specify that CGI execution was permitted in a particular directory:

<Directory /usr/local/apache/htdocs/somedir>
Options +ExecCGI
</Directory>

The above directive tells Apache to permit the execution of CGI files. You will also
need to tell the server what files are CGI files. The following AddHandler directive
tells the server to treat all files with the cgi or pl extension as CGI programs:

AddHandler cgi-script cgi pl


再來就是你的留言板的 cgi 問題,應該是 perl 的模組沒有裝到或是沒有裝好由於你提供的圖沒有
一張可以看的,所以不知道你出現的錯誤訊息是什麼,你可以看看你的 apache 的 log 看看或是貼上來看看。

Pikapi
2003-06-19, 05:18 PM
謝謝前輩的指教!
我把一些有關的資訊擷取出來:

這是之前測試的,這些訊息是不是說這程式有一些功能不能用?
不過這是3天前的訊息....不知道那時候做過哪些事~~>_<
沒注意到,真對不起
[Mon Jun 16 23:51:01 2003] [error] [client 61.219.36.31] Global symbol "$ver" requires explicit package name at /var/www/cgi-bin/book/ibbsr.cgi line 9.
[Mon Jun 16 23:51:01 2003] [error] [client 61.219.36.31] Global symbol "$myurl" requires explicit package name at /var/www/cgi-bin/book/ibbsr.cgi line 35.
[Mon Jun 16 23:51:01 2003] [error] [client 61.219.36.31] Global symbol "$met" requires explicit package name at /var/www/cgi-bin/book/ibbsr.cgi line 37.
[Mon Jun 16 23:51:01 2003] [error] [client 61.219.36.31] Global symbol "$backurl" requires explicit package name at /var/www/cgi-bin/book/ibbsr.cgi line 38.
[Mon Jun 16 23:51:01 2003] [error] [client 61.219.36.31] Global symbol "$pass" requires explicit package name at /var/www/cgi-bin/book/ibbsr.cgi line 39.
.
.
.
[Mon Jun 16 23:51:01 2003] [error] [client 61.219.36.31] Global symbol "$I" requires explicit package name at /var/www/cgi-bin/book/ibbsr.cgi line 1242.
[Mon Jun 16 23:51:01 2003] [error] [client 61.219.36.31] Global symbol "$I" requires explicit package name at /var/www/cgi-bin/book/ibbsr.cgi line 1244.
[Mon Jun 16 23:51:01 2003] [error] [client 61.219.36.31] Execution of /var/www/cgi-bin/book/ibbsr.cgi aborted due to compilation errors.
[Mon Jun 16 23:51:20 2003] [error] [client 61.219.36.43] Variable "@copy" is not imported at /var/www/cgi-bin/book/ibbsr.cgi line 1201.
[Mon Jun 16 23:51:20 2003] [error] [client 61.219.36.43] Variable "@copy" is not imported at /var/www/cgi-bin/book/ibbsr.cgi line 1206.
[Mon Jun 16 23:51:20 2003] [error] [client 61.219.36.43] Variable "@copy" is not imported at /var/www/cgi-bin/book/ibbsr.cgi line 1206.
[Mon Jun 16 23:51:20 2003] [error] [client 61.219.36.43] Variable "@copy" is not imported at /var/www/cgi-bin/book/ibbsr.cgi line 1208.


這是最近的,可以配合圖~
1.這是第一張圖的訊息,cgi程式放在cgi-bin底下
(error_log)
[Wed Jun 18 14:36:56 2003] [error] [client 127.0.0.1] Premature end of script headers: ibbsr.cgi
[Wed Jun 18 14:36:56 2003] [error] [client 127.0.0.1] Can't open perl script "!": No such file or directory

(access_log)
127.0.0.1 - - [18/Jun/2003:14:36:56 +0800] "GET /cgi-bin/book/ibbsr.cgi HTTP/1.1" 500 1086 "-" "Mozilla/5.0 (X11; U; Linux i686; zh-TW; rv:1.2.1) Gecko/20030225"

2.這是第二張圖的訊息,html在cgi-bin底下
(error_log)
[Wed Jun 18 14:34:29 2003] [error] [client 127.0.0.1] Premature end of script headers: ibbsr.htm

(access_log)
127.0.0.1 - - [18/Jun/2003:14:34:29 +0800] "GET /cgi-bin/ibbsr.htm HTTP/1.1" 500 1086 "-" "Mozilla/5.0 (X11; U; Linux i686; zh-TW; rv:1.2.1) Gecko/20030225"

3.這是我當時放在根目錄的時候出現的訊息
(error_log)
[Wed Jun 18 14:37:23 2003] [error] [client 127.0.0.1] Options ExecCGI is off in this directory: /var/www/html/ibbsr.cgi

(access_log)
127.0.0.1 - - [18/Jun/2003:14:37:23 +0800] "GET /ibbsr.cgi HTTP/1.1" 403 1030 "-" "Mozilla/5.0 (X11; U; Linux i686; zh-TW; rv:1.2.1) Gecko/20030225"

perl也許沒裝好,我記得在裝openwebmail的時候,還要求放入RedHat的第二片光碟@_@,好像安裝perl的什麼套件(一下就閃過了...沒看清楚)

注:輸入〝which perl〞有出現路徑〝/usr/bin/perl〞

附件:
我的logo檔

Pikapi
2003-06-19, 08:46 PM
我剛剛試出一點結果了!
我把cgi程式第一行加上〝-w〞
變成〝#!/usr/bin/perl -w〞
結果cgi可以跑了!:eek:
但是...沒有一個cgi跑成功
一個說建立暫存檔失敗,另一個一直都是〝正在開啟網頁〞,最後一個是缺了用cgi做出來的html
反正就都沒有一個跑成功>_<

請問還可以從哪些方向著手?
還有,在#!/usr/bin/perl後面+〝-w〞是有什麼用處?怎麼加了這個之後就突然跑出來@_@

謝謝指教^_^

repsol
2003-06-19, 09:20 PM
看1和2的access log 都是出現 HTTP/1.1" 500
http 的訊息代碼是500,代表的大都是程式出錯。
3.
Options ExecCGI is off in this directory: /var/www/html/ibbsr.cgi
/var/www/html/這個目錄下並沒有打開執行cgi的設定
所以不能run ibbsr.cgi

perl -w 的意思就是請 perl 倒出錯誤訊息以供 debug 用。

裝 openwebmail 最主要先詳讀一下那裡面有關安裝的 Document 和 FAQ,應該是有漏掉什麼步驟

這些網頁的步驟你可以參考看看

http://www.gocar.idv.tw/teachdata/openwebmail.htm
http://turtle.ee.ncku.edu.tw/openwebmail/download/doc/readme.txt
http://www.chshs.tpc.edu.tw/network/freebsd/openwebmail.htm

Pikapi
2003-06-19, 10:08 PM
謝謝repsol大哥的提示~
耶!可以了!
我寫一下我測試的結果:
1.我把cgi程式第一行改成〝#!/usr/bin/perl -wt 〞
2.修改httpd.conf---讓cgi-bin能讀的到cgi以外的檔案(留言版中用的到的圖片)
#ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
Alias /cgi-bin/ "/var/www/cgi-bin/"

<Directory "/var/www/cgi-bin/">

Options +ExecCGI
    AddHandler cgi-script .cgi .pl
 
</Directory>

如此留言版和日記本等等的cgi都可以跑了~~~~^__^:circle:
雖然...跟一般電腦的設定不一樣=_=|||

ps---問題?
1.後來加的〝t〞又是什麼意思?
我打perl -help有出現解釋,是那個解釋嗎...?(但是還是看不懂>_<)

2.我有找到這篇文章...但是看不懂他在說什麼

---以下是引文---

日志項:Premature end of script headers(腳本程序標題過早結束)
出錯的原因:這個出錯消息說明了這樣一種情況,即你的腳本程序在運行,而C G I模塊的h e a d e r函數輸出的C o n t e n t - Ty p e標題並不是腳本程序發出的第一個消息。有時在日志文件中的這個消息前面或后面還會出現一條輔助消息。這個輔助消息更有助於確定出錯的原因。你可以試用下面的方法來確定出錯的原因:
• 在調用h e a d e r函數前,務必不要輸出任何資訊,包括出錯消息。在h e a d e r函數之前輸出的任何東西都會導致這個錯誤。
在程序的開始處而不是在調用h e a d e r函數時,你會看到Perl CGI程序輸出“C o n t e n t - Ty p e:t e x t / h t m l \ n \ n”這條消息。顯然輸出這個消息和調用h e a d e r函數被認為是做同樣的事情,但實際並非如此。h e a d e r函數要考慮這樣一個問
題,即\ n \ n在每個服務器上並不總是表示相同的意思,它會為該服務器輸出相應的序列。

一個稱為輸出快取的問題會導致s y s t e m函數在h e a d e r函數輸出之前產生輸出,並在輸出中出現反引號( ` ` )。若要確保h e a d e r函數的輸出總是顯示在前面,可以將Perl CGI程序的開始部分重新編寫為下面的形式:

#!/usr/bin/perl -wt
use strict;
use CGI;

$|=1; #確保header 的輸出一直在最前端
print header;

---引文END---

repsol
2003-06-23, 06:28 PM
不會不一樣啊!! 你只是用了另外一各方式去達到你想要的目的
你用 alias 的方式去開一各目錄
那各目錄可以看 html 的檔案,也可以執行 cgi 的程式


Content-type 只是 CGI 回傳給 clinet 端的資料的格式

像這樣一各簡單的 cgi,執行下去,網頁只會出現 Hello World !!


#! /usr/bin/perl -w
print "Content-Type:Text/Html\n\n";
print "Hello World !! \n";



但是,如果沒有定義 Content-Type



#! /usr/bin/perl -w

print "Hello World !! \n";


這樣一執行下去,
程式會認為 Hello World !! 是 Content-Type
所以
apache 的 error log 會出現:
malformed header from script. Bad header=Hello World !!
client 端就會出現 http 代碼 500 的 Internal Server Error


Premature end of script headers
這個簡單的說就是 cgi 程式還沒送出 Content-Type header,程式就執行失敗了。


我不知道perl 有 "-t”這個參數,我如果放上去,
apache 的 error log 就會出現 Premature end of script headers 的訊息了


以上,如果我有說錯,請不吝指教
謝謝