1. 简介

Pear对PHP来说就类似于MacPort对MAC的用处,它可以用来安装和管理PHP的一系列第三方组件。因为我习惯于直接下载源码包进行组件的安装,所以一直都没装PHP的Pear。不过因为很多第三方的组件官方推荐的安装方式都是通过Pear,所以还是有必要安装了解下这个东西。

说实在的,当我看到这玩意的官方安装guide的时候我真的吓了一跳,日啊,居然要我下一个php脚本,然后用php命令行去跑这个脚本?你就不能给我源码包么?或者是bash脚本也行啊。我的OSX因为使用php5.2.17版本,所以导致了cli和cgi的版本脱节,貌似cgi的安装路径和cli还不一致。所以我不是很想通过cli脚本进行安装。后来发现这个问题他们也考虑到了,有web页面版本的安装脚本。

官方guide页面:http://pear.php.net/manual/en/installation.getting.php

2. 通过web脚本安装

那么首先就通过web页面进行安装,官方guide里有讲:

If you are running your site at a web hosting provider with no direct access to the server (via local logins, Telnet or SSH), you can use the PEAR Installer using the Web Frontend or (S)FTP.

Go to go-pear and save as go-pear.php. Copy go-pear.php to your server and open the corresponding URL in your browser, for example http://example.com/pear/go-pear.php.

当需要使用web页面进行安装的时候,只要按上面的步骤进行就可以了。

然后发现这玩意不行,因为项目转移到git的原因,web页面版本的脚本会出问题,问题的原因巨二无比,是因为需要的源代码下不到。报错:

Unknown location: /pear/pear-core/branches/PEAR_1_4/PEAR.php

源代码:http://svn.php.net/viewvc/pear/pear-core/branches/PEAR_1_4/PEAR.php 因为代码迁移的问题而不存在了。所以安装就因为缺少文件而失败了。好了,此路不通,我们换个思路。

3. 通过cli脚本安装

因为web脚本安装因为源代码的问题而不能进行下去,那么只能通过命令行cli的脚本进行安装。这里下载最新的cli安装脚本:http://pear.php.net/go-pear.phar,然后创建目录/Users/jonathan/prog/pear,将这个脚本扔到这个目录下。执行php go-pear.phar。

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 base ($prefix)                   : /Users/jonathan/prog/pear
 2. Temporary directory for processing            : /tmp/pear/install
 3. Temporary directory for downloads             : /tmp/pear/install
 4. Binaries directory                            : /Users/jonathan/prog/pear/bin
 5. PHP code directory ($php_dir)                 : /Users/jonathan/prog/pear/share/pear
 6. Documentation directory                       : /Users/jonathan/prog/pear/docs
 7. Data directory                                : /Users/jonathan/prog/pear/data
 8. User-modifiable configuration files directory : /Users/jonathan/prog/pear/cfg
 9. Public Web Files directory                    : /Users/jonathan/prog/pear/www
10. Tests directory                               : /Users/jonathan/prog/pear/tests
11. Name of configuration file                    : /Users/jonathan/prog/pear/.pearrc

1-11, 'all' or Enter to continue:
Beginning install...

Configuration written to /Users/jonathan/prog/pear/.pearrc...
Initialized registry...

Preparing to install...
installing phar:///Users/jonathan/prog/go-pear.phar/PEAR/go-pear-tarballs/Archive_Tar-1.3.7.tar...
installing phar:///Users/jonathan/prog/go-pear.phar/PEAR/go-pear-tarballs/Console_Getopt-1.3.0.tar...
installing phar:///Users/jonathan/prog/go-pear.phar/PEAR/go-pear-tarballs/PEAR-1.9.4.tar...
installing phar:///Users/jonathan/prog/go-pear.phar/PEAR/go-pear-tarballs/Structures_Graph-1.0.4.tar...
installing phar:///Users/jonathan/prog/go-pear.phar/PEAR/go-pear-tarballs/XML_Util-1.2.1.tar...
install ok: channel://pear.php.net/Archive_Tar-1.3.7
install ok: channel://pear.php.net/Console_Getopt-1.3.0
install ok: channel://pear.php.net/Structures_Graph-1.0.4
install ok: channel://pear.php.net/XML_Util-1.2.1
install ok: channel://pear.php.net/PEAR-1.9.4
PEAR: Optional feature webinstaller available (PEAR's web-based installer)
PEAR: Optional feature gtkinstaller available (PEAR's PHP-GTK-based installer)
PEAR: Optional feature gtk2installer available (PEAR's PHP-GTK2-based installer)
PEAR: To install optional features use "pear install pear/PEAR#featurename"

******************************************************************************
WARNING!  The include_path defined in the currently used php.ini does not
contain the PEAR PHP directory you just specified:
</Users/jonathan/prog/pear/share/pear>
If the specified directory is also not in the include_path used by
your scripts, you will have problems getting any PEAR packages working.

Current include path           : .:
Configured directory           : /Users/jonathan/prog/pear/share/pear
Currently used php.ini (guess) :
Press Enter to continue: 

** WARNING! Old version found at /Users/jonathan/prog/pear/bin, please remove it or be sure to use the new /Users/jonathan/prog/pear/bin/pear command

The 'pear' command is now at your service at /Users/jonathan/prog/pear/bin/pear

** The 'pear' command is not currently in your PATH, so you need to
** use '/Users/jonathan/prog/pear/bin/pear' until you have added
** '/Users/jonathan/prog/pear/bin' to your PATH environment variable.

Run it without parameters to see the available actions, try 'pear list'
to see what packages are installed, or 'pear help' for help.

For more information about PEAR, see:

http://pear.php.net/faq.php

http://pear.php.net/manual/

Thanks for using go-pear!

简单看下安装过程,还是有几个问题需要手动处理下。

首先需要理解下pear的工作原理,pear其实就是一个代码管理工具,它负责的就是将PHP第三方组件的代码下载下来,然后进行位置和版本的管理(PHP的代码不需要编译部署等步骤)。所以我在文章一开始的担心:cgi和cli的PHP不是一个版本,在这里是不需要的。因为只要php.ini配置将pear管理的代码库列入到include_path里去,无论cgi还是cli都能正确识别和执行所需要的PHP组件代码。所以我们要把代码管理库的位置(这里是/Users/jonathan/prog/pear/share/pear),写入到cgi的php.ini的include_path里。

接下来我们要处理的问题是pear的二进制命令行工具/Users/jonathan/prog/pear/bin/pear并不在系统的PATH里,所以你在命令行里直接打pear命令是没有效果的,这里我们需要修改/etc/profile,将/Users/jonathan/prog/pear/bin写入到PATH里。

OK,重启机器和php-cgi。