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
解决方法请查看另一篇博客:点击查看