2014-02-07 98 views
2

我試圖使用fastcgi_module在Apache2(Apache/2.4.6(Ubuntu))中運行cgi腳本。這是虛擬主機我成立使用fastcgi_module在Apache下永遠運行的CGI腳本(Perl)

<VirtualHost *:8080> 
     ServerName cgi.local 
     DocumentRoot /home/noobeana/CGI 
     <Directory /home/noobeana/CGI> 
       AllowOverride All 
       Order allow,deny 
       Allow from all 
       Require all granted 
       Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch 
       SetHandler fastcgi-script 
     </Directory> 
     ErrorLog ${APACHE_LOG_DIR}/error.log 
     CustomLog ${APACHE_LOG_DIR}/access.log combined 
</VirtualHost> 

這是Perl腳本(正常775'ed)我創建運行測試(/home/noobeana/CGI/test.pl):

#!/usr/bin/perl 
print "Content-type: text/html\n\n"; 
print "Hello there!<br />\n"; 

的路徑Perl可執行文件確實是/usr/bin/perl,其他一切看起來都不錯,但是當我在瀏覽器中打開http://cgi.local:8080/test.pl時,腳本永遠運行 - 我必須停止Apache來強制退出。此外,print是被輸出到Apache的錯誤日誌(不是瀏覽器),作爲腳本運行,顯示下面的線長的衆多:

[Fri Feb 07 10:24:54.059738 2014] [:warn] [pid 4708:tid 140365322880896] FastCGI: (dynamic) server "/home/noobeana/CGI/test.pl" started (pid 4771) 
Content-type: text/html 

Hello there!<br /> 
[Fri Feb 07 10:24:54.078938 2014] [:warn] [pid 4708:tid 140365322880896] FastCGI: (dynamic) server "/home/noobeana/CGI/test.pl" (pid 4771) terminated by calling exit with status '0' 
[Fri Feb 07 10:24:59.663494 2014] [:warn] [pid 4708:tid 140365322880896] FastCGI: (dynamic) server "/home/noobeana/CGI/test.pl" restarted (pid 4773) 
Content-type: text/html 

Hello there!<br /> 
[Fri Feb 07 10:24:59.665855 2014] [:warn] [pid 4708:tid 140365322880896] FastCGI: (dynamic) server "/home/noobeana/CGI/test.pl" (pid 4773) terminated by calling exit with status '0' 

我不知道是否這兩個問題(print不在瀏覽器中輸出,腳本沒有終止)是否相關。

+0

CGI和FastCGI真的是老派。你應該看看現代的Perl框架,例如[Dancer](http://perldancer.org/)和[Mojolicious](http://mojolicious.org/) – dolmen

+0

@dolmen:錯誤的二分法。 CGI和FastCGI是接口,而不是框架。 – duskwuff

回答

5

你想要做的是不可能的。 fastcgi_module只能運行實現FastCGI接口的腳本,這是您編寫的腳本不支持的腳本。相反,fastcgi_module會反覆嘗試啓動您的「FastCGI」腳本,看到它打印了一些內容並立即退出 - 哪些FastCGI腳本不應該這樣做 - 並且撓頭想知道它做錯了什麼。

一個簡單的腳本實現正確的接口可以使用CGI::Fast模塊實現:

#!/usr/bin/perl 
use strict; 
use CGI::Fast; 
while (my $CGI = CGI::Fast->new) { 
    print $CGI->header(); 
    print "Hello there\n"; 
} 

(FastCGI協議有些複雜,所以有實現它沒有合理的方式,而無需使用一個模塊)

+0

謝謝@duskwuff。其實我只用Perl來做一個簡單的測試,我不需要任何複雜的東西。我只需要在Apache下運行CGI腳本。也許我應該從FastCGI切換到普通的CGI?如果我這樣做了,你認爲我的假Perl腳本會按照我的預期運行嗎? – noobeana

+0

是的,如果是這樣,你應該切換到CGI模塊。 – duskwuff