重新寫一篇利用Apache進行FastCGI開發的文章,其中配置部分有所改進
以下操作均是基於Linux,Windows用戶可以參考配置
**********************************************************************
需要的軟件
Apache2.2.3 http://apache.justdn.org/httpd/httpd-2.2.3.tar.gz
FastCGI模塊 http://www.fastcgi.com/dist/mod_fastcgi-2.4.2.tar.gz
Perl的FastCGI模塊
http://search.cpan.org/CPAN/authors/id/S/SK/SKIMO/FCGI-0.67.tar.gz
***********************************************************************
步驟1:
安裝apache,我們用最簡單的方式來做,只裝apache和必要的模塊,cgi什麼的,先不
管,讓我們的apache先運行起來
程序代碼
tar xzvf httpd-2.2.3.tar.gz
cd http-2.2.3
./configure --prefix=/usr/local/apache2.2.3
make
make install
這樣,我們就將apache裝在了/usr/local/apache2.2.3目錄下,進入
/usr/local/apache2.2.3/bin目錄,執行
./apachectl start
訪問下http://localhost/看看是不是看到了 It Works!的頁面,如果沒有,那麼我想
你需要告訴我你看到了什麼,否則你就得去apache的站點來看看如何去配置了。
步驟2:
通過了步驟1,可以進行FastCGI的安裝了
首先要說明的一下是,FastCGI目前只支持到2.0,如果你用2.2版本以上的apache的話
,需要先運行一下patch,可以從下面的地址下載
點擊下載此文件
Patch下好了嗎?我們先把mod_fastcgi-2.4.2.tar.gz解壓縮
程序代碼
tar xzvf mod_fastcgi-2.4.2.tar.gz
其實安裝很容易,我們繼續..
程序代碼
cd mod_fastcgi-2.4.2
cp /soft/fcgi-patch . 將剛纔下的patch拷貝至當前目錄
patch -p1 < fcgi-patch
cp Makefile.AP2 Makefile
修改一下Makefile中的幾個目錄
top_dir 需要指定,用apache安裝的路徑,我們當前的是/usr/local/apache2.2.3
APXS 與 APACHECTL使用絕對路徑
程序代碼
APXS = /usr/local/apache2.2.3/bin/apxs
APACHECTL = /usr/local/apache2.2.3/bin/apachectl
好了,準備工作都好了,安裝只需幾秒,呵呵
程序代碼
make
make install
好了嗎?有沒有錯, 如果有錯的話, 請來告知
步驟3:
還沒有完成FastCGI與Apache的安裝呢,這步就是要配置我們的應用,來使用FastCGI。
在測試FastCGI之前,你先看下/usr/local/apache2.2.3/modules下有沒有
mod_fastcgi.so模塊,如果沒有,我想可能是見鬼了,也請告知
先配下我們的/usr/local/apache2.2.3/conf/http.conf吧
# Virtual hosts
Include conf/extra/httpd-vhosts.conf # 放開註釋
LoadModule fastcgi_module modules/mod_fastcgi.so
AddHandler cgi-script .cgi .pl # 傳統的CGI處理擴展名
AddHandler fastcgi-script .fcgi .fpl # FastCGI處理的擴展名
好了,配置的夠多的了,不過現在還得配置一個最重要的東西,
/usr/local/apache2.2.3/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
<Directory "/data/www/opensource">
DirectoryIndex index.htm index.html
</Directory>
Alias /cgi-bin/ "/data/www/opensource/cgi-bin/"
<Directory "/data/www/opensource/cgi-bin"
AllowOverride None
Options ExecCGI
Allow from all
</Directory>
DocumentRoot /data/www/opensource
ErrorLog /usr/local/apache2.2.3/logs/error_log
ServerName localhost
ServerAlias 127.0.0.1
</VirtualHost>
步驟3很長,但都是需要做的,看看普通的cgi有沒有問題吧。對了,你可能還沒有裝
cgi模塊吧,確認下,看看/usr/local/apache2.2.3/modules下有沒有mod_cgi.so模塊
,沒有的話,我們來裝下
cd /usr/local/apache2.2.3/bin
./apxs -c -i /apache源文件路徑/modules/generators/mod_cgi.c
好了,應該有了,將/usr/local/apache2.2.3/cgi-bin下的兩個文件copy到你的cgi目
錄中,我這裏的是/data/www/opensource/cgi-bin/ 訪問下看看
http://localhost/cgi-bin/printenv.pl
成功了嗎?如果沒有,告訴我發生了什麼
來寫個FCGI程序吧
步驟4:
得先裝下FCGI 對於perl的模塊
tar xzvf FCGI-0.67.tar.gz
cd FCGI-0.67
perl Makefile.PL
make
make test
make install
好了,寫個小東西測試下吧,你可以把下面的代碼直接進行copy
#!/usr/bin/perl -w
use FCGI;
my $count = 0;
while (FCGI::accept() >= 0) {
print "Content-Type:text/html\n\n";
print "Your are ";
print $count++;
print " Guess.";
}
記得文件保存爲.fpl結尾哦,存至cgi-bin目錄,訪問下看看
http://localhost/cgi-bin/xxx.fpl