一聚教程网:一个值得你收藏的教程网站

热门教程

php怎么安装模块?安装PHP模块的方法

时间:2022-06-24 16:15:49 编辑:袖梨 来源:一聚教程网

首先进行相关的说明
a.安装PHP时,有些模块是php自带的(如shmop,curl,mhash,mcrypt,iconv不需要另外下载这些模块就可以直接安装,php自身包含这些模块的源码,但是有可能需要下载依赖包,比如libiconv),有些不是php自带的(如eAccelerator,APC,memcache,这些模块则需要从网上下载后然后再安装,此时一般用DSO方式安装(phpize))

比如安装php时有个配置选项./configure --with-iconv-dir=/usr/local,这句话的意思是在/usr/local目录查找lib子目录及include子目录,因为要需要用到那两个目录下面的库文件(lib目录)及头文件(include目录)
[root@CentOS_Test_Server ext]# ls /usr/local/lib/*iconv*
/usr/local/lib/libiconv.la  /usr/local/lib/libiconv.so.2      /usr/local/lib/preloadable_libiconv.so
/usr/local/lib/libiconv.so  /usr/local/lib/libiconv.so.2.5.0

虽然在php的源代码目录下有iconv的子目录,但是仍然要依赖其它的库函数及头文件,如果没有,则必须提前安装
[root@CentOS_Test_Server ext]# pwd
/home/software/php-5.2.8/ext
[root@CentOS_Test_Server ext]# ll iconv/
total 324
-rw-r--r-- 1 1002 1002   4601 Mar  1  2007 config.m4
-rw-r--r-- 1 1002 1002    749 Nov 23  2004 config.w32
-rw-r--r-- 1 1002 1002     52 Feb  5  2003 CREDITS
-rw-r--r-- 1 1002 1002  73004 Dec 31  2007 iconv.c
-rw-r--r-- 1 1002 1002   4687 Jan 17  2004 iconv.dsp
-rw-r--r-- 1 root root    305 Apr 30 20:55 iconv.lo
-rw-r--r-- 1 root root 101304 Apr 30 20:55 iconv.o
-rw-r--r-- 1 root root      1 Apr 30 20:49 php_have_bsd_iconv.h
-rw-r--r-- 1 root root     27 Apr 30 20:49 php_have_glibc_iconv.h
-rw-r--r-- 1 root root      1 Apr 30 20:49 php_have_ibm_iconv.h
-rw-r--r-- 1 root root     21 Apr 30 20:49 php_have_iconv.h
-rw-r--r-- 1 root root      1 Apr 30 20:49 php_have_libiconv.h
-rw-r--r-- 1 1002 1002     95 Aug  5  2004 php_iconv.def
-rw-r--r-- 1 1002 1002   3631 Dec 31  2007 php_iconv.h
-rw-r--r-- 1 root root     31 Apr 30 20:49 php_iconv_supports_errno.h
-rw-r--r-- 1 root root     54 Apr 30 20:49 php_php_iconv_h_path.h
-rw-r--r-- 1 root root     31 Apr 30 20:49 php_php_iconv_impl.h
drwxr-xr-x 2 1002 1002   4096 Dec  8  2008 tests

下面的php手册上的相关说明
To use shmop you will need to compile PHP with the --enable-shmop parameter in your configure line.

To use PHP's cURL support you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories. In the "include" directory there should be a folder named "curl" which should contain the easy.h and curl.h files. There should be a file named libcurl.a located in the "lib" directory. Beginning with PHP 4.3.0 you can configure PHP to use cURL for URL streams --with-curlwrappers.

php的扩展模块目录是怎么规则的呢,我想应该是跟phpize命令有些关系,phpize命令的输出中有20060613,也就是执行phpize命令的时间,然后我们看到生成的扩展模块目录中含有20060613
[root@localhost memcache-2.2.5]# /usr/local/webserver/php/bin/phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519

Installing shared extensions:     /usr/local/webserver/php/lib/php/extensions/no                                              -debug-non-zts-20060613/

手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"
  修改为extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"

b.安装php前,如果这些依赖包没有,则必须先安装这些依赖包,例如,你要在php中安装mhash模块,要视情况安装mhash的依赖包,再安装php,否则配置php时会报错

c.php扩展模块一般在安装完php以后,再安装php扩展模块,扩展模块一般是编译成.so文件,动态加载,不编译到php自身,如果编译到php自身,则每次需要新增加一个新的模块时就必须对整个php进行编译一次,比较耽误时间。

下面来说说几种常见的安装方法
1.用--with-xx或--enable-xx安装
就是在配置的时候加上相应的模块即可,如
./configure --enable-shmop --with-curl --enable-xml
然后make && make install即可
通过这种方式安装的模块不会生成.so文件,因此也不需要在php.ini中添加extension = "extname.so"的配置

Typically an extension that does not require external libraries uses --enable(如果configure中是使用--enable开头的模块,则这些模块一般不需要另外的函数库)

2.直接用PECL命令安装
$pecl install extname
这条命令将自动下载扩展extname的源代码,然后编译安装extname.so你的目录extension_dir中(php.ini中有设置)
This will download the source for extname, compile, and install extname.so into your extension_dir. extname.so may then be loaded via php.ini

By default, the pecl command will not install packages that are marked with the alpha or beta state. If no stable packages are available, you may install a beta package using the following command: 
$ pecl install extname-beta

You may also install a specific version using this variant: 
$ pecl install extname-0.1

Note: After enabling the extension in php.ini, restarting the web service is required for the changes to be picked up.

然后在php.ini中加入一行extension = "extname.so",重启web服务器让改变生效。

3.phpize安装(DSO方式安装)
首先下载要安装的php模块,然后解压缩,然后再执行下面几步即可
$ cd extname
$ phpize
$ ./configure
$ make
$ make install
然后在php.ini中加入一行extension = "extname.so",重启web服务器让改变生效。

4.通过php的源代码包编译(静态编译)
php手册上面写得比较全面,就直接引用手册上面的内容了
Compiling PECL extensions statically into PHP
You might find that you need to build a PECL extension statically into your PHP binary. To do this, you'll need to place the extension source under the php-src/ext/ directory and tell the PHP build system to regenerate its configure script.

$ cd /your/phpsrcdir/ext
$ pecl download extname
$ gzip -d < extname.tgz | tar -xvf -
$ mv extname-x.x.x extname

This will result in the following directory: 
/your/phpsrcdir/ext/extname

From here, force PHP to rebuild the configure script, and then build PHP as normal: 
$ cd /your/phpsrcdir 
$ rm configure
$ ./buildconf --force
$ ./configure --help
$ ./configure --with-extname --enable-someotherext --with-foobar
$ make
$ make install

Note: To run the 'buildconf' script you need autoconf 2.13 and automake 1.4+ (newer versions of autoconf may work, but are not supported).

Whether --enable-extname or --with-extname is used depends on the extension. Typically an extension that does not require external libraries uses --enable. To be sure, run the following after buildconf: 
$ ./configure --help | grep extname
这种方法安装php模块时就不需要在php.ini里面添加extension = "extname.so"了,因为模块已经编译到php里面了,是静态编译。

上面说的是在Linux下面安装php模块,在windows下面安装模块主要有如下的方法
1.直接加载

如果是php自带的模块(也就是php的扩展目录下面已经存在相应的dll文件),则在php.ini中直接将模块前面的;去掉,重启web服务器即可
例如要启用gd的功能,则将;extension=php_gd2.dll修改为extension=php_gd2.dll即可

2.在pecl网站上面下载已经编译好的dll文件,然后在php.ini中加入一行即可
例如在要windows上面安装memcache的模块,则从网上下载memcache的dll文件php_memcache.dll,然后把它复制到php的扩展目录,例如我的电脑上php扩展目录的路径为;e:/www/php5/ext
Directory in which the loadable extensions (modules) reside.
extension_dir = "e:/www/php5/ext"

3.把模块编译到php里面(也就是通过源代码编译)
简单点说,就是用vc来进行编译
说细看此页面

4.编译一个独立的dll文件
简单点说,就是用vc来进行编译,可以是别人开发好的php扩展,或者是你自己开发的也可以
说细看windows下开发并编译PHP扩展(步骤说明),这篇文章写得比较清楚,我在此恕不多言了。

引用php手册上的一段话
Installing a PHP extension
On Windows, you have two ways to load a PHP extension: either compile it into PHP, or load the DLL. Loading a pre-compiled extension is the easiest and preferred way.

To load an extension, you need to have it available as a ".dll" file on your system. All the extensions are automatically and periodically compiled by the PHP Group (see next section for the download).

To compile an extension into PHP, please refer to building from source documentation.

To compile a standalone extension (aka a DLL file), please refer to building from source documentation. If the DLL file is available neither with your PHP distribution nor in PECL, you may have to compile it before you can start using the extension.

热门栏目