Linux-CentOS 环境下源码编译的 php 安装动态扩展

Linux-CentOS 环境下源码编译的 php 安装动态扩展


php 源码编译时配置的参数是属于静态扩展,无需额外加载。但有时 php 已经编译完成,又不能影响当前 php 的工作,这时候就需要动态扩展。需要利用 phpize 命令用于在编译好的 php 中扩展模块。

需要先找到 php 源码包 ,这里测试源码包位置位于 /home/centos/php-7.4.5 。此处以扩展 curl 为例。

首先要安装 curl 服务,去网页下载 curl 模块,地址如下:https://curl.haxx.se/download.html

[centos@localhost php-7.4.5]$ # 先建立一个临时保存 curl 源文件的文件夹
[centos@localhost php-7.4.5]$ mkdir curl-download
[centos@localhost php-7.4.5]$ cd curl-download/
[centos@localhost curl-download]$ # 下载源码包
[centos@localhost curl-download]$ wget https://curl.haxx.se/download/curl-7.70.0.tar.bz2
[centos@localhost curl-download]$ # 解压
[centos@localhost curl-download]$ tar -jxvf curl-7.70.0.tar.bz2
[centos@localhost curl-download]$ :<<!
[centos@localhost curl-download]$ 有的服务需要先安装,此处 curl 模块服务实际上可以不用执行以下操作
[centos@localhost curl-download]$ !
[centos@localhost curl-download]$ # 进入目录
[centos@localhost curl-download]$ cd curl-7.70.0/
[centos@localhost curl-7.70.0]$ #预编译(并指定安装位置)
[centos@localhost curl-7.70.0]$ sudo mkdir /usr/local/lib/curl
[centos@localhost curl-7.70.0]$ ./configure --prefix=/usr/local/lib/curl/
[centos@localhost curl-7.70.0]$ # 安装
[centos@localhost curl-7.70.0]$ sudo make && make install

此处存放 curl源码目录路径/home/centos/php-7.4.5/curl-download/curl-7.70.0 ,而 安装目录路径 位于 /usr/local/lib/curl/ 下。

然后进入 php 源码包位置,记住预编译的地址 --with-curl=(curl源码目录)请注意! ,有的扩展需要先安装服务,记住安装的目录后,预编译的地址就变为 --with-curl=(curl安装目录)

[centos@localhost curl-7.70.0]$ # 进入 php 源码扩展包位置
[centos@localhost curl-7.70.0]$ cd ../..
[centos@localhost php-7.4.5]$ cd ext/curl/
[centos@localhost php-7.4.5]$ :<<!
[centos@localhost php-7.4.5]$ 在扩展模块目录,运行 phpize 程序。(作用是检测 php 的内核版本,并为其扩展生成相应的编译配置,在当前目录生成 ./configure 文件)
[centos@localhost php-7.4.5]$ !
[centos@localhost curl]$ /usr/local/php-7.4.5/bin/phpize # 这是我这测试的 php 安装目录,这里需要命中到你要扩展的那个 php
[centos@localhost curl]$ ./configure --with-php-config=/usr/local/php-7.4.5/bin/php-config --with-curl=/usr/local/lib/curl # 此处已安装的 curl 目录为例
[centos@localhost curl]$ # 安装
[centos@localhost curl]$ make && make install

安装好后,会提示生成了 curl.so 文件。 ls 进行查看。

Installing shared extensions:     /usr/local/php-7.4.5/lib/php/extensions/no-debug-non-zts-20190902/
[centos@localhost curl]$ ls /usr/local/php-7.4.5/lib/php/extensions/no-debug-non-zts-20190902/
curl.so  opcache.a  opcache.so

php.ini 开启 curl 支持。

[centos@localhost curl]$ vim /usr/local/php-7.4.5/etc/php.ini
[centos@localhost curl]$ # 新增以下配置,若是第一次扩展配置,需要增加 extension_dir 配置项
extension_dir = "/usr/local/php-7.4.5/lib/php/extensions/no-debug-non-zts-20190902/"
extension=curl.so

重启服务器 nginx 或者 Apache ,并且重启 php-fpm

[centos@localhost curl]$ ps -ef | grep php-fpm
[centos@localhost curl]$ sudo pkill php-fpm
[centos@localhost curl]$ sudo /usr/local/php-7.4.5/sbin/php-fpm 

最后,查看是否已经加装 curl 扩展模块。

[centos@localhost curl]$ /usr/local/php-7.4.5/bin/php -m | grep curl

会看到 curl 模块正常扩展输出。


还有一种比较通用方法php 源码包实际上已经整合了常用的扩展模板,位于 ext 目录下,操作如下:

[centos@localhost php-7.4.5]$ cd ext/curl/
[centos@localhost curl]$ /usr/local/php-7.4.5/bin/phpize
[centos@localhost curl]$ ./configure --with-php-config=/usr/local/php-7.4.5/bin/php-config
[centos@localhost curl]$ make && make install

我这里在 预编译 的过程中,产生了以下错误:

configure: error: Package requirements (libcurl >= 7.15.5) were not met:

No package 'libcurl' found

解决方法请查看另一篇博客:点击查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值