Linux 环境下编译安装 PHP 扩展

线上的生产环境通常为 LNMP,当因为业务需求需要用到某些扩展包,而那个扩展包需要相应版本的 PHP 扩展的时候,就需要安装对应的 PHP 扩展。

编译安装扩展的方式都大同小异,这里就以安装 Yaf 框架的扩展为例,来演示如何编译安装 PHP 的扩展。

1. 进入 PHP 扩展的官网 。


2. 找到右上角的搜索框,输入 Yaf,下载最新版本的压缩包到服务器的指定目录。



[root@localhost src]# wget
--2018-12-17 00:06:28--
正在解析主机 (
正在连接 (||:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:97140 (95K) [application/octet-stream]
正在保存至: “yaf-3.0.7.tgz”

100%[========================================================================================================>] 97,140      43.9KB/s 用时 2.2s   

2018-12-17 00:06:32 (43.9 KB/s) - 已保存 “yaf-3.0.7.tgz” [97140/97140])

[root@localhost src]# 
3. 解压缩软件包。
[root@localhost src]# tar -zxvf yaf-3.0.7.tgz
4. 配置、编译、安装
[root@localhost src]# cd yaf-3.0.7/
[root@localhost yaf-3.0.7]# /usr/local/php/bin/phpize 
Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012
[root@localhost yaf-3.0.7]# ./configure --with-php-config=/usr/local/php/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... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for PHP prefix... /usr/local/php
checking for PHP includes... -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib
checking for PHP extension directory... /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012
checking for PHP installed headers prefix... /usr/local/php/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking whether to enable yaf support... yes, shared
checking PHP version... 7.0.32, ok
checking for ld used by cc... /usr/bin/ld
checking if the linker (/usr/bin/ld) is GNU ld... yes
checking for /usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 1572864
checking command to parse /usr/bin/nm -B output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking if cc supports -fno-rtti -fno-exceptions... no
checking for cc option to produce PIC... -fPIC
checking if cc PIC flag -fPIC works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
[root@localhost yaf-3.0.7]# 
[root@localhost yaf-3.0.7]# make && make install


/bin/sh /usr/local/src/yaf-3.0.7/libtool --mode=install cp ./ /usr/local/src/yaf-3.0.7/modules
cp ./.libs/ /usr/local/src/yaf-3.0.7/modules/
cp ./.libs/yaf.lai /usr/local/src/yaf-3.0.7/modules/
PATH="$PATH:/sbin" ldconfig -n /usr/local/src/yaf-3.0.7/modules
Libraries have been installed in:

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
 - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
   during execution
 - add LIBDIR to the `LD_RUN_PATH' environment variable
   during linking
 - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
 - have your system administrator add LIBDIR to `/etc/'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and manual pages.

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/
[root@localhost yaf-3.0.7]# 
5. 修改 PHP 配置文件,验证配置

由上面可知,扩展已经被安装到了 /usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/ 这个目录里面,接下来需要在 PHP 的配置文件中添加 Yaf 这个扩展。

vim /etc/php.ini
847 ;;;;;;;;;;;;;;;;;;;;;;
848 ; Dynamic Extensions ;
849 ;;;;;;;;;;;;;;;;;;;;;;
851 ; If you wish to have an extension loaded automatically, use the following
852 ; syntax:
853 ;
854 ;   extension=modulename.extension
855 ;
856 ; For example, on Windows:
857 ;
858 ;   extension=msql.dll
859 ;
860 ; ... or under UNIX:
861 ;
862 ;
863 ;
864 ; ... or with a path:
865 ;
866 ;   extension=/path/to/extension/
867 ;
868 ; If you only provide the name of the extension, PHP will look for it in its
869 ; default extension directory.
870 ;
871 ; Windows Extensions
872 ; Note that ODBC support is built in, so no dll is needed for it.
873 ; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5+)
874 ; extension folders as well as the separate PECL DLL download (PHP 5+).
875 ; Be sure to appropriately set the extension_dir directive.
876 ;
877 ;extension=php_bz2.dll
878 ;extension=php_curl.dll
879 ;extension=php_fileinfo.dll
880 ;extension=php_ftp.dll



[root@localhost no-debug-non-zts-20151012]# service php-fpm restart 
Gracefully shutting down php-fpm . done
Starting php-fpm  done

在浏览器中查看 phpinfo,确认扩展是否安装成功。



 收藏 (0) 打赏




除特别注明外,本站所有文章均基于CC-BY-NC-SA 4.0原创,转载请注明出处。
文章名称:《Linux 环境下编译安装 PHP 扩展》
分享到: 更多 (0)


评论 抢沙发



忘记密码 ?