我们在部署 PHP 时,很多时候,在 make 完之后,会出现下图这样的信息:


出现这条信息,是因为我们在编译时没有禁用pear,一般情况下,我们在编译 PHP 的时候会有下面两个选项:

--without-pear:不安装 pear 扩展

--disable-phar:禁用 phar 支持

只有当这两个选项有存在时,才不会有上面这条信息,但是有时候,我们却需要安装 phar 支持,由于这时候,我们的 PHP 已经编译安装完成,在重新编译一次未免浪费时间,所以,我们就手动进行安装 phar 支持。

首先去官网下载 go-pear.phar 脚本

[root@weba ~]# wget –c https://pear.php.net/go-pear.phar

然后安装go-pear.phar

[root@weba ~]# php go-pear.phar

如图,整个安装过程一共有15个步骤,算是比较繁琐吧,因此我们想到一个偷懒的办法----自动交互。

实现自动交互,我们就不得不提起我们 shell 中的老朋友 expect 了。

expect 是 shell 中专门为实现交互而存在的,他的功能强大,语法简单,很适合我们这里 的场景。

首先,yum 安装 expect

[root@weba ~]# yum –y install expect

安装完之后编写 expect 脚本:

[root@weba ~]# vim pear.exp

#!/usr/bin/expect
set pear_dir /usr/local/pear
set php_dir /usr/local/php
set web_dir /wdata/http/www
set tmp_dir /tmp/pear/install

spawn php go-pear.phar
expect "'all' or Enter to continue:"
send "1\r"
expect "Installation*:"
send "$pear_dir\r"


expect "'all' or Enter to continue:"
send "2\r"
expect "Temporary directory for processing*:"
send "$tmp_dir\r"


expect "'all' or Enter to continue:"
send "3\r"
expect "Temporary directory for downloads*:"
send "$tmp_dir\r"


expect "'all' or Enter to continue:"
send "4\r"
expect "Binaries directory*:"
send "$pear_dir/bin\r"


expect "'all' or Enter to continue:"
send "5\r"
expect "PHP code directory*:"
send "$php_dir\r"


expect "'all' or Enter to continue:"
send "6\r"
expect "Documentation directory*:"
send "$pear_dir/docs\r"


expect "'all' or Enter to continue:"
send "7\r"
expect "Data directory*:"
send "$pear_dir/data\r"


expect "'all' or Enter to continue:"
send "8\r"
expect "User-modifiable configuration files directory*:"
send "$pear_dir/cfg\r"


expect "'all' or Enter to continue:"
send "9\r"
expect "Public Web Files directory*:"
send "$web_dir\r"


expect "'all' or Enter to continue:"
send "10\r"
expect "System manual pages directory*:"
send "$pear_dir/man\r"


expect "'all' or Enter to continue:"
send "11\r"
expect "Tests directory*:"
send "$pear_dir/test\r"


expect "'all' or Enter to continue:"
send "12\r"
expect "Name of configuration file"
send "$pear_dir/cfg/.pearrc\r"


expect "'all' or Enter to continue:"
send "\r"   
send "\r"   
send "\r"
expect eof

如上述代码,虽然比较 low,但是却能代替我们做手动操作,做为一个专业的懒人来说,可谓是方便至极。

最后,授权 pear.exp 脚本,并执行

[root@weba ~]# chmod 755 pear.exp

[root@weba ~]# expect pear.exp

执行完之后,pear 扩展就算是安装成功了,我们再把 pear 的相关命令复制到 /usr/bin ,或者将 pear 的命令路径添加到系统路径中去。

[root@weba ~]#  cp /usr/local/pear/bin/* /usr/bin

我们来验证以下 pear 的命令,直接在命令行敲 pear 即可:

[root@weba ~]# pear

出现如上图这样的画面就表示我们的安装是没有任何问题的。