Linux安装PHP扩展模块详解

PHP扩展模块安装

编译httpd时,有涉及动态和静态模块,PHP也一样有静态与动态之分,之前所涉及到的PHP安装都全部为静态,并没有任何动态的模块,所谓动态,就是一个独立存在的.so文件,在httpd中PHP就是以动态模块的形式被加载的。PHP一旦编译完成后,要想再增加一个功能的话,要么重新编译PHP,要么直接编译一个扩展模块(生成一个.so文件),然后在php.ini中配置一下,就可以被加载使用了。

1.查看PHP加载了哪些模块

[root@linuxmi ~]# /usr/local/php/bin/php -m
[PHP Modules]
bz2
Core
ctype
date
dom
ereg
exif
fileinfo
filter
gd
hash
iconv
json
libxml
mbstring
mcrypt
mysql
mysqli
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
soap
sockets
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

2.安装一个PHP的redis扩展模块。

[root@linuxmi ~]# cd /usr/local/src/
[root@linuxmi src]# wget https://codeload.github.com/phpredis/phpredis/zip/develop 
[root@linuxmi src]# mv develop phpredis-develop.zip
[root@linuxmi src]# unzip phpredis-develop.zip  //yum install -y unzip
[root@linuxmi src]# cd phpredis-develop
[root@linuxmi phpredis-develop]# 
[root@linuxmi src]# cd phpredis-develop
[root@linuxmi phpredis-develop]# /usr/local/php7/bin/phpize //生成configure文件
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
[root@linuxmi phpredis-develop]# yum install -y autoconf
[root@linuxmi phpredis-develop]# /usr/local/php7/bin/phpize
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303
[root@linuxmi phpredis-develop]# ls
acinclude.m4       cluster_library.h  config.sub    CREDITS            library.c         mkdeb.sh         redis_array.h       redis_commands.c  serialize.list
aclocal.m4         cluster.markdown   configure     debian             library.h         mkinstalldirs    redis_array_impl.c  redis_commands.h  tests
arrays.markdown    common.h           configure.in  debian.control     ltmain.sh         package.xml      redis_array_impl.h  redis_session.c
autom4te.cache     config.guess       config.w32    install-sh         Makefile.global   php_redis.h      redis.c             redis_session.h
build              config.h.in        COPYING       ISSUE_TEMPLATE.md  missing           README.markdown  redis_cluster.c     rpm
cluster_library.c  config.m4          crc16.h       liblzf             mkdeb-apache2.sh  redis_array.c    redis_cluster.h     run-tests.php
[root@linuxmi phpredis-develop]# ./configure --with-php-config=/usr/local/php7/bin/php-config
[root@linuxmi phpredis-develop]# echo $?
0
[root@linuxmi phpredis-develop]# make
[root@linuxmi phpredis-develop]# echo $?
0
[root@linuxmi phpredis-develop]# make install
Installing shared extensions:     /usr/local/php7/lib/php/extensions/no-debug-zts-20160303/
[root@linuxmi phpredis-develop]# ls /usr/local/php7/lib/php/extensions/no-debug-zts-20160303/
opcache.so  redis.so
[root@linuxmi phpredis-develop]# /usr/local/php7/bin/php -m
[PHP Modules]
bz2
Core
ctype
date
dom
exif
fileinfo
filter
gd
hash
iconv
json
libxml
mbstring
mcrypt
mysqli
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
soap
sockets
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]
[root@linuxmi phpredis-develop]# /usr/local/php7/bin/php -m |grep redis
[root@linuxmi phpredis-develop]# /usr/local/php/bin/php -i |grep extension_dir  //查看扩展模块存放目录,我们可以在php.ini中去自定义该路径 
PHP Warning:  Unknown: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in Unknown on line 0
extension_dir => /usr/local/php/lib/php/extensions/no-debug-zts-20131226 => /usr/local/php/lib/php/extensions/no-debug-zts-20131226
sqlite3.extension_dir => no value => no value
[root@linuxmi phpredis-develop]# vim /usr/local/php7/etc/php.ini  //增加一行配置(可以放到文件最后一行)

定义如下:
extension = redis.so

[root@linuxmi phpredis-develop]# /usr/local/php7/bin/php -m |grep redis
redis
增加示例图:

Linux安装PHP扩展模块详解

报错:

缺少autoconf包,安装yum install -y autoconf

Linux安装PHP扩展模块详解

这个不影响使用,如果要去掉它,可以在php.ini里设置一下timezone那个参数

Linux安装PHP扩展模块详解

举例:

你想编译一个扩展模块的时候,如果它自带的源码包ext下有这个目录,那就可以直接在目录下编译它,如果没有那就需要去官网下载,下面是给大家举例一个zip模块实验操作如下:

相关推荐