0%

Centos7中,PHP7下,扩展的安装

CentOS 下,PHP有多种方式来安装拓展, 主要有 包管理式的 yum 安装、pecl 安装, 以及源码编译安装。
包管理式的安装卸载尤为方便,而源码编译式的安装则方便参数调优。
一般搭建本机开发环境推荐包管理式的安装,节约时间。而线上部署环境则推荐编译安装, 方便调优。

环境和准备


  • 环境 : windows10
  • PHP版本:7.0.13
  • Nginx: 1.10.3
  • Centos: docker 创建的CentOS容器[CentOS Linux release 7.3.1611 (Core) ]
  • 全程墙外网

方式1: yum 安装扩展(mongodb)

yum方式安装能自动安装扩展的.so动态库,并配置好php.ini

yum search mongodb|grep php //搜索到拓展名为 php70w-pecl-mongodb

等待自动安装完成后,查看phpinfo

mongodb

到此 yum 安装扩展方法介绍完毕

pecl 安装(redis)


pecl 安装需要准备2个文件:phpize ,php-config

查找phpize的位置

1
2
[root@9e2c60482bdc conf.d]# whereis phpize
phpize: /usr/bin/phpize

查找php-config的位置

1
2
3
[root@9e2c60482bdc conf.d]# whereis php-config
php-config:[root@9e2c60482bdc conf.d]#

发现没有php-config,先测试运行下phpize

1
2
3
php-config:[root@9e2c60482bdc conf.d]# phpize
Can t find PHP headers in /usr/include/php
The php-devel package is required for use of this command.

直接报错,接下来安装php-devel解决上面报错yum install php70w-devel

再次运行phpize,没有报找不到的错误,出现了下面的报错:

1
2
3
[root@9e2c60482bdc bin]# phpize
Cannot find config.m4.
Make sure that you run /usr/bin/phpize in the top level source directory of the module

先不管他,进行下一步的操作

更新 pear(非必须)

我们需要先从pear官网下载 go-pear 工具,这个工具将帮我们同时安装 pecl包管理器(管理php的C拓展) pear包管理器(管理php类库);

wget http://pear.php.net/go-pear.phar

下载完成后安装工具,运行下面命令:php go-pear.phargo-pear

然后默认回车即可,暂时没搞懂这些参数的意思,估计是配置路经相关吧,先回车再说;

go-pear

安装扩展(1)

搜索扩展包:pecl search redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@9e2c60482bdc test]# pecl search redis

Warning: Invalid argument supplied for foreach() in Command.php on line 249

Warning: Invalid argument supplied for foreach() in /usr/share/pear/PEAR/Command.php on line 249

Warning: Invalid argument supplied for foreach() in Command.php on line 249

Warning: Invalid argument supplied for foreach() in /usr/share/pear/PEAR/Command.php on line 249

Warning: Invalid argument supplied for foreach() in Command.php on line 249

Warning: Invalid argument supplied for foreach() in /usr/share/pear/PEAR/Command.php on line 249

Warning: Invalid argument supplied for foreach() in Command.php on line 249

Warning: Invalid argument supplied for foreach() in /usr/share/pear/PEAR/Command.php on line 249

Warning: Invalid argument supplied for foreach() in Command.php on line 249

.
.
.
Warning: Invalid argument supplied for foreach() in PEAR/Command.php on line 249

Warning: Invalid argument supplied for foreach() in /usr/share/pear/PEAR/Command.php on line 249
XML Extension not found


刷刷的报错了,查询错误后贴出解决方法:

解决方法

1
vi /usr/bin/pecl  //文件最后一行去掉 -n 参数

再次搜索,

1
2
3
4
5
6
[root@9e2c60482bdc test]# pecl search redis
Retrieving data...0%
Matched packages, channel pecl.php.net:
=======================================
Package Stable/(Latest) Local
redis 3.1.2 (stable) PHP extension for interfacing with Redis

安装扩展包pecl install mongodb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@9e2c60482bdc test]# pecl install redis
downloading redis-3.1.2.tgz ...
Starting to download redis-3.1.2.tgz (199,041 bytes)
.................done: 199,041 bytes
20 source files, building
running: phpize
Configuring for:
PHP Api Version: 20151012
Zend Module Api No: 20151012
Zend Extension Api No: 320151012
building in /tmp/pear/install/pear-build-rootLPns3m/redis-3.1.2
running: /tmp/pear/install/redis/configure --with-php-config=/usr/bin/php-config
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... no
checking for gcc... no
configure: error: in /tmp/pear/install/pear-build-rootLPns3m/redis-3.1.2:
configure: error: no acceptable C compiler found in $PATH
See 'config.log' for more details
ERROR: '/tmp/pear/install/redis/configure --with-php-config=/usr/bin/php-config' failed

刷刷的报错了,查询错误后贴出解决方法:yun install gcc gcc+

安装扩展(2)

执行:pecl install monodb

等待自动安装完成后,显示如下

1
2
3
4
5
Build process completed successfully
Installing '/usr/lib64/php/modules/redis.so'
install ok: channel://pecl.php.net/redis-3.1.2
configuration option "php_ini" is not set to php.ini location
You should add "extension=redis.so" to php.ini


然后在php配置文件中稍作修改,将extension=redis.so添加到php.ini中,重启PHP,查看phpinfo

redis

到此 pecl 安装扩展方法介绍完毕

源码编译安装(Seaslog)


Seaslog文档

安装git

先安装git,克隆Seaslog源码

1
2
3
4
5
6
7
[root@9e2c60482bdc test]# git clone https://github.com/Neeke/SeasLog.git
Cloning into 'SeasLog'...
remote: Counting objects: 1094, done.
remote: Total 1094 (delta 0), reused 0 (delta 0), pack-reused 1094
Receiving objects: 100% (1094/1094), 1.04 MiB | 24.00 KiB/s, done.
Resolving deltas: 100% (628/628), done.

安装Seaslog

进入Seaslog目录,执行

1
2
3
$ /path/to/phpize  //更换自己对应的目录
$ ./configure --with-php-config=/path/to/php-config
$ make && make install

完成之后,修改php配置文件,将extension = seaslog.so添加到php.ini中,重启PHP,查看phpinfo
image

总结

至此Linux中,给PHP安装扩展的3种方式记录完毕,特此总结

  1. 参考1
  2. 参考2
  3. peal官网