1. 程式人生 > >php安裝擴充套件的幾種方法

php安裝擴充套件的幾種方法

    轉自:http://doc3.workerman.net/appendices/install-extension.html

安裝擴充套件

注意

與Apache+PHP或者Nginx+PHP的執行模式不同,WorkerMan是基於PHP命令列 PHP CLI 執行的,使用的是不同的PHP可執行程式,使用的php.ini檔案也可能不同。所以在網頁中列印phpinfo()看到安裝了某個擴充套件,不代表命令列的PHP CLI也安裝了對應的擴充套件。

如何確定PHP CLI安裝了哪些擴充套件

執行 php -m 會列出命令列 PHP CLI 已經安裝的擴充套件,結果類似如下:

~# php -m
[PHP Modules]
libevent
posix
pcntl
...

如何確定PHP CLI 的php.ini檔案的位置

當我們安裝擴充套件時,可能需要手動配置php.ini檔案,把擴充套件加進去,所以要確認PHP CLI的php.ini檔案的位置。可以執行php --ini查詢PHP CLI的ini檔案位置,結果類似如下(各個系統顯示結果會有差異):

~# php --ini
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File:         /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed:      /etc/php5/cli/conf.d/apc.ini,
/etc/php5/cli/conf.d/libevent.ini,
/etc/php5/cli/conf.d/memcached.ini,
/etc/php5/cli/conf.d/mysql.ini,
/etc/php5/cli/conf.d/pdo.ini,
/etc/php5/cli/conf.d/pdo_mysql.ini
...

給PHP CLI安裝擴充套件(安裝memcached擴充套件為例)

方法一、使用apt或者yum命令安裝

如果PHP是通過 apt 或者 yum 命令安裝的,則擴充套件也可以通過 apt 或者 yum 安裝

debian/ubuntu等系統apt安裝PHP擴充套件方法(非root使用者需要加sudo命令)

1、利用apt-cache search查詢擴充套件包

~# apt-cache search memcached php
php-apc - APC (Alternative PHP Cache) module for PHP 5
php5-memcached - memcached module for php5

2、使用apt-get install安裝擴充套件包

~# apt-get install -y php5-memcached
Reading package lists... Done
Reading state information... Done
...

centos等系統yum安裝PHP擴充套件方法

1、利用yum search查詢擴充套件包

~# yum search memcached php
php-pecl-memcached - memcached module for php5

2、使用yum install安裝擴充套件包

~# yum install -y php-pecl-memcached
Reading package lists... Done
Reading state information... Done
...

說明:

使用apt或者yum安裝PHP擴充套件會自動配置php.ini檔案,安裝完直接可用,十分方便。缺點是有些擴充套件在apt或者yum中沒有對應的擴充套件安裝包。

方法二、使用pecl安裝

使用pecl install命令安裝擴充套件

1、pecl install安裝

~# pecl install memcached
downloading memcached-2.2.0.tgz ...
Starting to download memcached-2.2.0.tgz (70,449 bytes)
....

2、配置php.ini

通過執行 php --ini查詢php.ini檔案位置,然後在檔案中新增extension=memcached.so

方法三、原始碼編譯安裝(一般是安裝PHP自帶的擴充套件,以安裝pcntl擴充套件為例)

1、利用php -v命令檢視當前的PHP CLI的版本

~# php -v
PHP 5.3.29-1~dotdeb.0 with Suhosin-Patch (cli) (built: Aug 14 2014 19:55:20)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

2、根據版本下載PHP原始碼

3、解壓原始碼壓縮包

例如下載的壓縮包名稱是php-5.3.29.tar.gz

~# tar -zxvf php-5.3.29.tar.gz
php-5.3.29/
php-5.3.29/README.WIN32-BUILD-SYSTEM
php-5.3.29/netware/
...

4、進入原始碼中的ext/pcntl目錄

~# cd php-5.3.29/ext/pcntl/

5、執行 phpize 命令

~# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626

6、執行 configure命令

~# ./configure
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
...

7、執行 make 命令

~# make
/bin/bash /tmp/php-5.3.29/ext/pcntl/libtool --mode=compile cc ...
-I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend...
...

8、運 行make install 命令

~# make install
Installing shared extensions:     /usr/lib/php5/20090626/

9、配置ini檔案

通過執行 php --ini查詢php.ini檔案位置,然後在檔案中新增extension=pcntl.so

說明: 此方法一般用來安裝PHP自帶的擴充套件,例如posix擴充套件和pcntl擴充套件。除了用phpize編譯某個擴充套件,也可以重新編譯整個PHP,在編譯時用引數新增擴充套件,例如在原始碼根目錄執行

~# ./configure --enable-pcntl --enable-posix ...
~# make && make install

方法四、phpize安裝

如果要安裝的擴充套件在php原始碼ext目錄中沒有,那麼這個擴充套件需要到http://pecl.php.net 搜尋下載

以安裝libevent擴充套件為例(假設系統安裝了libevent-dev庫)

1、下載libevent擴充套件檔案壓縮包(在當前系統哪個目錄下載隨意)

~# wget http://pecl.php.net/get/libevent-0.1.0.tgz
--2015-05-26 21:43:40--  http://pecl.php.net/get/libevent-0.1.0.tgz
Resolving pecl.php.net... 104.236.228.160
Connecting to pecl.php.net|104.236.228.160|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9806 (9.6K) [application/octet-stream]
Saving to: “libevent-0.1.0.tgz”

100%[=======================================================>] 9,806       41.4K/s   in 0.2s

2、解壓擴充套件檔案壓縮包

~# tar -zxvf libevent-0.1.0.tgz
package.xml
libevent-0.1.0/config.m4
libevent-0.1.0/CREDITS
libevent-0.1.0/libevent.c
....

3、進入到原始碼目錄

~# cd libevent-0.1.0/

4、執行phpize命令

~# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626

5、執行configure命令

~# ./configure
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking whether the C compiler works... yes
...

6、執行make命令

~# /bin/bash /data/test/libevent-0.1.0/libtool --mode=compile cc  -I. -I/data/test/libevent-0.1.0 -DPHP_ATOM_INC -I/data/test/libevent-0.1.0/include
...

7、執行make install命令

~# make install
Installing shared extensions:     /usr/lib/php5/20090626/

8、配置ini檔案

通過執行 php --ini查詢php.ini檔案位置,然後在檔案中新增extension=libevent.so