首先安裝sudo apt-get install libperl-dev
去http://httpd.apache.org/download.cgi#apache22下載httpd
安裝方法:./configure make make install
這樣你的apache2就安裝到了/usr/local/apache2下.
cd /usr/local/apache2/bin
sudo ./apachectl start
在地址欄輸入127.0.0.1,發現“It works!",頁面的內家取決於htdocs/index.html的內容。
任何地方都可以調用apachectl
sudo vim /root/.bashrc
export PATH="$PATH:/usr/local/apache2/bin"
vim ~/.bashrc
export PATH="$PATH:/usr/local/apache2/bin"
設置開機自動啓動Apache
在/etc/init.d下建立一個shell文件apache
#!/bin/bash
/usr/local/apache2/bin/apachectl start
$chmod +x apache
在/etc/rc5.d下建立一個指向apache的軟鏈接
$ln -s /etc/init.d/apache S20apache
因爲rc(0--6,S).d下的文件是在系統啓動後自動執行的嘛,另外rc(0--6,S).d下文件名的命名規則:以S開頭是啓動,以K開關不是啓動。注意rc.d下建立的軟件鏈接指向的文件必須是/etc/init.d下的文件。
最後執行update-rc.d apache defaults
詳細內容參加rc5.d下面的README文件。
實際上開機自動啓動還有另外一種簡單的方式:System==>Preference==>Startup Applications
在/usr/local/apache2/cgi-bin下已經給了個測試程序test-cgi,我們在地址欄是輸入127.0.0.1/cgi-bin/test-cgi發現“Internal Server Error",出現這個問題有三種原因:
找不到腳本解釋器。shell的話第一行就用#!/bin/sh,Perl的話第一行就用#!/usr/bin/perl
腳本語法有誤
權限問題。從瀏覽器中訪問apache服務器文件的用戶默認組別是"Nobody",執行sudo chmod 755 test-cgi就解決問題了。
sudo chmod 777 /usr/local/apache2/cgi-bin
在cgi-bin下新建一個文件hello.pl
#!/usr/bin/perl
print "Content-type: text/html";
print "Hello, World.";
$chmod 755 hello.pl
在瀏覽器地址欄輸入http://localhost/cgi-bin/hello.pl測驗一下。
下面給一個正式的CGI代碼,也是我做的第一個搜索引擎的demo,具有劃時代的意義!
#!/usr/bin/perl use CGI qw(:standard) ; use Search::Xapian; print header; print start_html( -title => 'ISee Search Engine' , -align => 'center' ); print start_form, textfield( 'terms' ), submit( 'Search' ), p, end_form; print hr; if (param()){ my $db = Search::Xapian::Database->new( "/home/orisun/master/db2" ); my $term =param( 'terms' ); my $enq = $db ->enquire( $term ); ##printf "Running query '%s'\n", $enq->get_query()->get_description(); my @matches = $enq ->matches(0, 10); print scalar ( @matches ) . " results found" ; print br; foreach my $match ( @matches ) { my $doc = $match ->get_document(); printf "ID=%d Percent=%d%%<br>%s" , $match ->get_docid(), $match ->get_percent(), $doc ->get_data(); print p; } } print end_html; |
運行效果:
徹底刪除Apache
首先停止服務:
service httpd stop
apchectl stop
再刪除
1、sudo apt-get --purge remove apache2
2、sudo apt-get --purge remove apache2.2-common
3、sudo apt-get autoremove (此命令會自動卸載PHP)
最後手動刪除Apache的所有文件