前些陣子聽酷學園所辦的PHP研討會時,主講人 Darkhero 有提到 PEAR 這個好用的 PHP 函式庫網站,有學 PHP 的人一定要玩看看它。


而今晚去書局買東西時,順手翻了一下這本 PHP PEAR 嚴選程式庫,大概翻了一下怎麼安裝在系統後,回來就手癢幫我的機器也裝上去好了!


安裝的流程大概是「裝PHP」->「告訴 PHP 要支援 PEAR」->「設定 php.ini」->「安裝 Lynx」->「安裝 PEAR 基本套件」->「Restart Apache」就好了!




1. 裝 PHP
這應該不需要多說了,下載 www.php.net 上面套件解開即可

2. 告訴 PHP 要支援 PEAR (--with-pear=DIR Install PEAR in DIR [PREFIX/lib/php])
在 configure 時,記得要加上 --with-pear 喔!在這邊要特別注意一下,若您所安裝的 PHP 版本是在 4.3.0 以後的版本,內建就有幫您安裝 PEAR 的套件管理指令囉 ~ (引用一下 PEAR 的安裝說明)

When using PHP >= 4.3.0, the PEAR Package Manager is already installed unless one has used the ./configure option --without-pear.

接下來 make;make install;make clean 就不多說了


3. 設定 php.ini

在 include_path 的地方改成 include_path = ".:/www/php/lib/php" 即可

4. 安裝 Lynx (這是一個文字模式的瀏覽器啦)

#cd /usr/ports/www/lynx
#make install clean


5. 安裝 PEAR 所需的基本套件


#lynx -source http://go-pear.org/ | php

在這邊要特別注意的是要將 php 這個執行檔的目錄加入到環境變數中!以我的例子來說,我的 php 裝在 /www/php 下,所以環境變數要加入 /www/php/bin 後才可以正常執行上面這行指令。
指令下完後會出現這些東西~

Welcome to go-pear!

Go-pear will install the 'pear' command and all the files needed by
it. This command is your tool for PEAR installation and maintenance.

Go-pear also lets you download and install the PEAR packages bundled
with PHP: MDB2.


If you wish to abort, press Control-C now, or press Enter to continue: 要安裝就按 Enter

HTTP proxy (http://user: password@proxy.myhost.com: port), or Enter for none:: 按 Enter # ( 問你有沒有 Proxy 囉 )

Below is a suggested file layout for your new PEAR installation. To
change individual locations, type the number in front of the
directory. Type 'all' to change all of them or simply press Enter to
accept these locations.

1. Installation prefix : /www/php
2. Binaries directory : $prefix/bin
3. PHP code directory ($php_dir) : $prefix/share/pear
4. Documentation base directory : $php_dir/docs
5. Data base directory : $php_dir/data
6. Tests base directory : $php_dir/tests
7. Temporary files directory : $prefix/temp

1-7, 'all' or Enter to continue: 按 Enter 就開始裝了

The following PEAR packages are bundled with PHP: MDB2.
Would you like to install these as well? [Y/n] : y


6. Restart Apache
#/www/bin/apachectl restart


接下來我們隨便安裝一個套件來測試看看 PEAR 是否安裝成功,首先我們安裝 Benchmark 這個計算程式執行時間的套件

#pear install Benchmark
pear/Benchmark can optionally use PHP extension "bcmath"
downloading Benchmark-1.2.6.tgz ...
Starting to download Benchmark-1.2.6.tgz (7,915 bytes)
.....done: 7,915 bytes
install ok: channel://pear.php.net/Benchmark-1.2.6

然後利用 pear list 指令來看看剛才裝的 Benchmark 是否有被裝上了 (嗯,果然有!!)
Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.1 stable
Benchmark 1.2.6 stable
Console_Getopt 1.2 stable
MDB2 2.2.2 stable
PEAR 1.4.11 stable
PEAR_Info 1.6.1 stable
XML_RPC 1.5.0 stable

那裝好的套件都會放在 /www/php/lib/php 這個目錄裡面,每個套件的目錄裡都有說明程式,想要提升自己程式能力的話,可以花一些時間來研究別人的程式碼喔!
這時我們寫一個小程式來測試看看,就取名為 benchmark.php,內容如下
<?php

require_once 'Benchmark/Timer.php';



$timer = new Benchmark_Timer();

$timer->start();



echo "Hello World!";



$timer->stop();

$totaltime=$timer->timeElapsed();

echo "<br>This Page Show Time:".$totaltime."Sec";

?>

若程式可以正常執行的話,那就恭喜你完成 PEAR 的安裝囉 :D
以後再來玩看看有哪些有趣的套件!

[1] Apache20 + MySQL50 + PHP5 + PEAR
[2] PEAR
arrow
arrow
    全站熱搜

    Bojack 發表在 痞客邦 留言(5) 人氣()